# What is difference between \box and \unvbox or between \box and \unhbox

What is difference between \box and \unvbox or between \box and \unhbox.

• I suspect there must be something answering this in the site. I'm no expert but in a way I think that \box puts the box as it is (“fixed” in a way), and \un(h|v)box leaves some flexibility to be calculated from the surroundings (for instance if there's \hskip 1cm plus 2cm minus 0.5cm inside the box, which is flexible, it would be calculated having in account the other flexible spaces that are around that “unhbox” in the same line). In the second case the box is not “fixed” but the contents are “reflowed” to some extent. – Manuel Jun 26 '18 at 17:36
• Start here: TeX by Topic – Werner Jun 26 '18 at 17:36
• This might help. From The TeXBook page 120: An unboxing operation “unsets” any glue that was set at the box’s outer level. – Andreas Storvik Strauman Jun 26 '18 at 18:08
• ... also from the TeXBook on the subject: "[In an example case in the book] it is best to use \unhbox or \unvbox [...] otherwise you use more of TEX’s memory space, and you might even obtain boxes inside boxes nested to such a deep level that hardware or software limits are exceeded." Heh :p – Andreas Storvik Strauman Jun 26 '18 at 18:11
• \box required a numeric suffix; it can't stand alone. \box255 is by definition the place where the current page is compiled as defined by the tex program. – barbara beeton Jun 26 '18 at 18:24

If you go \setbox0\hbox{abc} then box 0 is a box register with an hbox that contains three items, the character nodes for a, b and c.

So if you use \box0 it adds a single item to the current list, an hbox.

However if you use \unhbox0 then you add the list that was contained in the box, not the box itself, so adds the nodes for a, b and c.

See

\documentclass{article}

\showoutput

\begin{document}

\setbox0\hbox{abc}
1 xxx \box0 zzz

\setbox0\hbox{abc}
2 xxx \unhbox0 zzz

\end{document}


the first paragraph is

....\hbox(0.0+0.0)x15.0
....\OT1/cmr/m/n/10 1
....\glue 3.33333 plus 1.66666 minus 1.11111
....\OT1/cmr/m/n/10 x
....\OT1/cmr/m/n/10 x
....\OT1/cmr/m/n/10 x
....\glue 3.33333 plus 1.66666 minus 1.11111
....\hbox(6.94444+0.0)x15.27782
.....\OT1/cmr/m/n/10 a
.....\OT1/cmr/m/n/10 b
.....\kern0.27779
.....\OT1/cmr/m/n/10 c
....\OT1/cmr/m/n/10 z
....\OT1/cmr/m/n/10 z
....\OT1/cmr/m/n/10 z
....\penalty 10000
....\glue(\parfillskip) 0.0 plus 1.0fil
....\glue(\rightskip) 0.0


with an hbox following the space after xxx, but the second paragraph is

....\hbox(0.0+0.0)x15.0
....\OT1/cmr/m/n/10 2
....\glue 3.33333 plus 1.66666 minus 1.11111
....\OT1/cmr/m/n/10 x
....\OT1/cmr/m/n/10 x
....\OT1/cmr/m/n/10 x
....\glue 3.33333 plus 1.66666 minus 1.11111
....\OT1/cmr/m/n/10 a
....\OT1/cmr/m/n/10 b
....\kern0.27779
....\OT1/cmr/m/n/10 c
....\OT1/cmr/m/n/10 z
....\OT1/cmr/m/n/10 z
....\OT1/cmr/m/n/10 z
....\penalty 10000
....\glue(\parfillskip) 0.0 plus 1.0fil
....\glue(\rightskip) 0.0


with a, b, c added directly with no containing box.

adding the list directly means that any glue can take part in glue stretching and linebreaking can occur (almost) as if the contents had never been boxed.

Note that a box can always be placed in a vertical or horizontal list, it just affects how the box is stacked, but you can only unbox a box into the right kind of list.

If you change the example to

\documentclass{article}

\showoutput

\begin{document}

\setbox0\hbox{abc}
\box0 zzz

\setbox0\hbox{abc}
\unhbox0 zzz

\end{document}


You get

as the \box0 goes directly on to the vertical list without starting a paragraph, so the following zzz starts a new paragraph underneath. Conversely the \unhbox0 starts a horizontal list so starts a paragraph so comes after a paragraph indentation with the following zzz in the same horizontal list.