[Tex/LaTex] difference between \box and \unvbox or between \box and \unhbox

boxestex-core

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

Best Answer

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

enter image description here

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.

Related Question