[Tex/LaTex] Why are box dimension assignments both local and global

boxesdimensionsgrouping

Consider the following code.

\setbox0 \hbox{XXX}
\fbox{\copy0}

{
        \setbox0\hbox{ZZZ}
        {\wd0 0pt}
        \fbox{\copy0}
}

\fbox{\box0}

This produces three ruled boxes. The two XXXs are inside the rules, the ZZZ is not.

After \wd0 0pt, box 0 has width 0 and this assignment is global which explains why ZZZ is not entirely inside the rules. At the same time, the assignment is local in that it doesn't affect the box 0 which contains the XXX.

What is the purpose of this behavior? Alternatively, what is the utility of being able to set the dimensions of boxes outside the current group?

Edit: To be clear, this was not a question about what the behavior is, it was a question about why Knuth gave TeX this behavior. I thought there might be a use for it that I didn't see since TeX by Topic explicitly mentions it. My suspicion now is that Taco's comment about it being merely an implementation detail, not a design goal, is the right answer.

Best Answer

I am just guessing here, but I believe box dimensions are associated with each box. You don't have special dimen registers for these. This is what I believe happens here. After you enter the first group, at the moment you assign \setbox0\hbox{ZZZ}, TeX assigns a local box register. You then enter the second group, but your box register 0 is still the same \hbox{ZZZ}, TeX will not create a local copy of the box. So when you assign 0pt to \wd0, you modify the \hbox{ZZZ} from the previous group.

Try to modify your code like this:

\setbox0 \hbox{XXX}
\fbox{\copy0}

{
   \wd0 0pt
   \setbox0\hbox{ZZZ}
   {\wd0 0pt}
   \fbox{\copy0}
}

\fbox{\box0}

and see what happens.

Edit: I think the following behavior is related to this:

\setbox0 \hbox{XXX}
\fbox{\copy0}

{
   \fbox{\box0}
}

\fbox{\box0}

Notice that the \box0 inside the group empties the box register, it does not get restored at the end of the group.

Related Question