棚が手前と奥とに2つあって
How does と+に function here.
And, what does it mean.
In my opinion, I think it's same as
棚が手前と奥、2つあって、
(I think there's no need to add とに)
Full sentence : 棚が手前と奥とに2つあって、手前のほうの棚を横にスライドさせれば、奥の棚が見えるようになっています。
There are three variations:
A. 棚が手前と奥に2つある
B. 棚が手前と奥とに2つある
These two are basically the same - 手前 and 奥 refer to locations so they have -に. In general, XとY and XとYと are usually interchangeable. The former is simpler (and probably more common), while the latter can make it less ambiguous, regarding where the second part (Y) ends. Also, the former may have a more collective reading, while the latter may have a more distributive meaning, but the difference rarely matters.
C. 棚が手前と奥、2つある
This construction is slightly different from the two above. Without -に, 手前 and 奥 do not (necessarily) refer to locations, they characterize 棚. In fact, the sentence will still make sense if you replace the two words with other words that can characterize 棚, like
棚が白と黒、2つある
The same cannot be said for the versions with -とに and -に because they require the preceding nouns to be locations.