Why does \boxN clear the box?
At the time TeX was written, conserving memory was important. Typically, when a box has been used the content is no longer needed in the original (i.e. it will be moved to some other box), so memory can conveniently be reclaimed with the behaviour that \box
clears the box. When this is not the case, \copy
is available. Notably, a box is not a fixed item, unlike say count registers, so the need to avoid build-up is there.
Notably, the behaviour of LaTeX's \usebox
is built on \copy
not \box
, i.e. treating a box like any other 'variable' at the cost of memory usage. Certainly with a modern TeX system there really is no need to use \box
routinely for memory reasons (though if one knows that the content is not required, there is no particular reason not to reclaim it).
The box in TeX is implemented (roughly speaking) as a pointer to the vertical/horizontal list of typesetting material included in such box and this data structure can be large and it resides in main TeX memory. When you say \box0
at some place of outer material then only the pointer is placed here and the pointer of box0 is newly set to NULL. The contents of previous \box0 saved in main memory is untouched. This "only-pointer" operation is very simple and inexpensive from machine point of view. On the other hand, \copy0
creates the second copy of the same data structure in main memory, because first instance is pointed at the place of \copy0 and second instance keeps the contents of box0.
Boxes are removed from memory after \shipout
. Imagine, that \copy0
is used somewhere in a \shipout
ed box. This content is removed. This is the reason why you need the second instance of data structure if you need to keep the contents of box0.
Joseph Wright's answer is to the point: conserving memory was Knuth's main concern when defining the primitives dealing with box registers.
There are however some subtle points.
A box register can be void or it can contain a horizontal box (from \hbox
) or a vertical box (from \vbox
or \vtop
).
If you do \box0
(zero can be any other valid register number) and the register is void, nothing happens. Otherwise the box is delivered, not its contents. The contents of the box can be delivered with \unhbox
or \unvbox
, depending on the type of box stored in the register.1
The fact is that a box may take a huge amount of memory (huge in comparison with computer memory available in the years TeX was being developed, of course), so reclaiming this memory after delivering a box built with \setbox
was most important.
There is also another peculiar aspect of boxes. We know that opening a group and setting a variable activates the mechanism for restoring the value of the variable when the group will end. This also holds for boxes, but, in order to conserve memory, if you have built a box outside the group and deliver it inside, the box register will be destroyed at the upper level!
\setbox0=\hbox{ABC}
\begingroup\setbox0=\box0\endgroup
\box0
will produce no output. Precisely, the most recent incarnation of \box0
will be destroyed. So if you do
\setbox0=\hbox{ABC}
\begingroup\setbox0=\hbox{DEF}\box0\endgroup
\box0
the last command will deliver the same as \hbox{ABC}
. In other words, TeX will not start the mechanism for restoring box registers when ending a group unless it has to, that is, \setbox
is used inside the group.
There are six primitives to deal with box registers, in addition to \setbox
:
\box \copy
\unhbox \unhcopy
\unvbox \unvcopy
The ones in the right column don't destroy the register's contents.
LaTeX's \sbox
builds a horizontal box, because it is defined by
% latex.ltx, line 4796:
\long\def\sbox#1#2{\setbox#1\hbox{%
\color@setgroup#2\color@endgroup}}
Similarly \savebox
stores a horizontal box. Note the protection against color trickling (they're activated if color
is loaded). The macro \usebox
always does \copy
:
% latex.ltx, line 4819:
\def\usebox#1{\leavevmode\copy #1\relax}
Final note: if \setbox
(or macros built upon it) is done inside a group, the register will be restored to its previous state when the group ends (unless, as said earlier, some \box
command has
1 If the box register is void, TeX possibly switches mode, if it has to, depending on whether \unhbox
or \unvbox
is used. This is how \leavevmode
works: it does \unhbox\v@idbox
, under the assumption that the box register \v@idbox
is permanently void.