# Can a TeX box's visual appearance change after the box is closed?

Is a Tex box like a bitmap image in the sense that the moment it is closed the TeX code inside it is as much as rendered, and its visual appearance can no longer change from then on, even if some registers, parameters or macros that the code inside the box depends on change their values later on, and the only things you can do with the TeX box is move it around, hide it, and maybe apply various effects that change the box's appearance uniformly, e.g. change the transparency?

• Generally, a box is a fixed entity, with some exceptions. This question and answer may help: tex.stackexchange.com/questions/31430/…. There is \unhbox and \unvbox, but they have severe restrictions (are not expandable). – Steven B. Segletes Aug 1 '17 at 20:02
• Try \documentclass{article} \begin{document} \newcounter{foo} \setcounter{foo}{17} \newbox\mynicebox \setbox\mynicebox=\hbox{\stepcounter{foo}} \unhcopy\mynicebox \thefoo \unhcopy\mynicebox \thefoo \end{document} – user31729 Aug 1 '17 at 20:05
• @ChristianHupfer: I'm very new to TeX boxes. Would you please walk me through your example and tell me what it is that I'm supposed to learn from it? – Evan Aad Aug 1 '17 at 20:27

From the TeXbook, page 279 (Summary of Vertical Mode):

}. This one is harder, because it depends on the current group. TeX should now be working on a group that began with {; and it knows why it started that group. So it does the appropriate finishing actions, undoes the effects of non-global assignments, and leaves the group. At this point TeX might leave its current mode and return to a mode that was previously in effect.

The TeXbook denotes with } an explicit or implicit character token of category 2 (and { is the same, but category code 1).

When TeX “leaves a mode”, it always transforms the corresponding list into its inner representation (not available to the users) consisting of nodes. This is the case, for instance, when a box was being built. The simplest example is \hbox{...} or \setbox<number>=\hbox{...} that are essentially equivalent, with the only difference that in the latter case the transformed list is stored in memory, while in the former case it is appended to the current list as a box item.

Building a box is, in some way, the same as typesetting a page, so the result is essentially unmodifiable: macros have been expanded and primitive commands executed. So, for instance, fonts have been selected, implicit kerns between letters have been inserted, glue has been set.

However, a box stored in memory can by unboxed. This procedure removes one level of boxing. In particular, glue nodes at the top level regain their stretchability or shrinkability and penalty nodes will be evaluated in the list being built.

Here's a simple example:

\hsize=4cm

\setbox0=\hbox{a\hfill b\penalty-10000 c\hfill d}

\copy0

\unhbox0

\bye


In the first line, no indent appears, because \copy0 is not a horizontal command, so the box is appended to the main vertical list. After that, the box is unboxed, so the glue nodes and the penalty node will have an effect.

Try adding \it in front of \unhbox0: the font would not change.

However, with something like

\newcount\foo
\foo=1000
\setbox0=\hbox{a\foo=100 b}

\unhbox0


the value stored in \foo will still be 1000 after \unhbox0, because the assignment \foo=100 does not produce a node in the horizontal list being built (no assignment does). The (local) value 100 can be used for building the box, but the outer value will be restored at the end of the group.

Transparency is not something TeX knows about. Effects on boxes are obtained with \special commands and whatsits whose role is understood by the device driver (for instance, the PDF distiller incorporated in pdftex). A stored box can be modified by the device driver, if it is able to, but, again, this is outside the scope of TeX (the program).

The content of a box (1st paragraph) is fixed. One can see from paragraph 2 that attempted reformatting has no effect. However, that box's content (as a whole) can be manipulated by formatting, if one applies an \unhbox to it (see paragraphs 3 and 4). However, the content itself cannot change, even via \unhbox, because these \un... commands are not expandable, but TeX primitives. That is to say, you cannot apply an \unhbox and then recover the individual tokens that had been in the box.

EDITED to add one important point. Kerning and ligatures are lost across box boundaries, which is another indicator that the box is a complete entity, rather than a collection of tokens. This is true even when the box is unboxed, as shown in the revised MWE.

\documentclass{article}
\usepackage{lipsum}
\parindent 0in
\parskip 1em
\begin{document}
\setbox0=\hbox{\lipsum[1]}
\copy0

\parbox{3in}{\copy0}

\parbox{3in}{\unhcopy0}

\parbox{4in}{\unhbox0}

\setbox0=\hbox{a}
T\unhbox0

Ta

\setbox0=\hbox{i}
f\unhbox0

fi
\end{document}


• Thank you. So barring an \un... command, the box is as much as typeset the moment it is closed, right? And, in a sense, it is like an enclosure that captures the settings of the TeX environment at the moment the box was closed. For instance, if I drew a line inside the box, and at the time the color settings were red, this line will render red whenever I use this box, even if at the time of use the color is blue, correct? – Evan Aad Aug 1 '17 at 20:46
• @EvanAad That is correct. The box captures the settings at the moment of its creation, regardless of how the settings change later. – Steven B. Segletes Aug 2 '17 at 0:38
• @EvanAad See revision regarding kerning and ligatures. – Steven B. Segletes Aug 2 '17 at 17:50
• @EvanAad But a box can change the colour of stuff outside the box when it is used. This is true even if the box does not contain any colour changes and if no colour changes are active when the box is set. – cfr Aug 3 '17 at 0:26
• @cfr: Can you please elaborate? Give an example? – Evan Aad Aug 3 '17 at 5:11

Yes, a box's visual appearance can change after the box is created. Consider the following example.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\color{red}%
%
\newbox\mybox%
\setbox\mybox\hbox{Hello, world!}%
\copy\mybox%
%
\color{blue}%
\copy\mybox%
\end{document}


resulting in the following pdf:

The example creates a box when the document's default color is red. It then inserts a copy of the box into the document. The text is rendered red. The default color is then changed to blue, and another copy of the same box is inserted to the document. This time the text is rendered blue.

To see why this happens, add the following lines before the \end{document} command:

\showboxbreadth=2147483647%
\showboxdepth=2147483647%
\showbox\mybox%


The third line "dumps" the box's raw typesetting information to the log file, and causes the compilation to stop with an error message. The first two lines set parameters for \showbox that determine the level of detail that the dump should include. By assigning the highest possible integer that TeX can handle, we indicate that we desire the maximum level of detail.

The resulting dump can now be found near the end of the log file:

\hbox(6.94444+1.94444)x55.58344
.\OT1/cmr/m/n/10 H
.\OT1/cmr/m/n/10 e
.\OT1/cmr/m/n/10 l
.\OT1/cmr/m/n/10 l
.\OT1/cmr/m/n/10 o
.\OT1/cmr/m/n/10 ,
.\glue 3.33333 plus 2.08331 minus 0.88889
.\OT1/cmr/m/n/10 w
.\kern-0.27779
.\OT1/cmr/m/n/10 o
.\OT1/cmr/m/n/10 r
.\OT1/cmr/m/n/10 l
.\OT1/cmr/m/n/10 d
.\OT1/cmr/m/n/10 !


The important thing to note is that the dump doesn't contain color information. This implies that the text color will be inferred from the graphics state of the pdf document at the time the box's contents is rendered by the pdf viewer. This aspect of the graphics state can be set by the \color command.

If you change the above example by including a color specification inside the box:

\setbox\mybox\hbox{\color{red}Hello, world!}


the color information will be included in the raw typesetting directives emitted by the box:

\hbox(6.94444+1.94444)x55.58344
.\pdfcolorstack 0 push {1 0 0 rg 1 0 0 RG}
.\OT1/cmr/m/n/10 H
.\OT1/cmr/m/n/10 e
.\OT1/cmr/m/n/10 l
.\OT1/cmr/m/n/10 l
.\OT1/cmr/m/n/10 o
.\OT1/cmr/m/n/10 ,
.\glue 3.33333 plus 2.08331 minus 0.88889
.\OT1/cmr/m/n/10 w
.\kern-0.27779
.\OT1/cmr/m/n/10 o
.\OT1/cmr/m/n/10 r
.\OT1/cmr/m/n/10 l
.\OT1/cmr/m/n/10 d
.\OT1/cmr/m/n/10 !


and therefore the box's text color will not be affected by the default document color:

That said, as egreg and Steven B. Segletes pointed out, the raw typesetting directives emitted by the box are essentially fixed upon the box's creation. Moreover, these directives are always written in terms of literals; they will never refer to macros, registers, variables or any other TeX element whose value may change.

• The box's appearance hasn't changed in any way. – egreg Aug 7 '17 at 8:22