[Tex/LaTex] Why \usebox (is it vestigial)

boxes

Despite my inflammatory title, I understand that \usebox is employed to display a predefined box. But this question concerns when [the use of the box register, rather than the box content] becomes absolutely essential to a LaTeX operation (for example, as arguments to \wd, \ht, and \dp). [EDITS above and below in brackets, to reflect proper terminology]


Clarifying, based on Werner's comments, given the syntax

\newsavebox{\xyz}
\sbox{\xyz}{...}
\def\xyzbox{\usebox{\xyz}}

are there useful things I can perform only upon [the register] \xyz that I cannot perform on [the box content] \xyzbox?

(If not, then maybe \usebox is vestigial).


For example, consider the LaTeX syntax

\newsavebox{\xyz}
\sbox{\xyz}{...}
\usebox{\xyz}

Why couldn't LaTeX have been constructed so that the last line could have been issued as \xyz, without the \usebox (saving me a few characters of typing)? I am so frustrated [an exaggeration] with the \usebox syntax, that I work around it in both the verbatimbox package and the \stackengine package. In the former, I allow this syntax:

\begin{myverbbox}[]{\boxname}
...
\end{myverbbox}
\boxname% <--NOTE: THIS IS NOT \usebox{\boxname}

Internally, \boxname actually points to \usebox{boxnamecontent}

Likewise, in stackengine, I allow:

\savestack{\mystack}{VARIOUS STACKING COMMANDS}
\mystack% <---NOTE: THIS IS NOT \usebox{\mystack}

Internally, \mystack actually points to \usebox{\mystackcontent}

So the crux of my question is this? When is the [use of a box register] essential to a LaTeX operation??

I realize things like \wd, \ht, and \dp act on the un-rendered box name itself (without the \usebox); however, even these are replaceable by calc's \widthof, \heightof, and \depthof, which act upon the (rendered) \usebox version of the box.

So what other TeX/LaTeX operations act upon [the box register, rather than the content]? In essence (rhetorically), why do we still need \usebox syntax if everything that acts upon a box acts upon the rendered \usebox form of the box?

[So far, the answers have revealed that I need the box register to set the box, and to use the box. And I can, but don't need to, use the register to measure the box. And in nebulous places where I "need a number, not… a box".

Any other usages of the box register?]

Best Answer

TeX provides us with macros and registers. Macros are (simply) replaced by their definition, and so are used 'raw'. LaTeX wraps this concept up in \newcommand, with the result that

\newcommand{\foo}{....}

can then be used as simply \foo. On the other hand, registers have to be correctly accesses, at least in places where TeX does not 'expect' them. Again, LaTeX provides interfaces, for example

\newcounter{foo}
\value{foo}

which does the same as the TeX construct

\newcount\c@foo
\the\c@foo

Notably, using the register here needs an accessor function.

Boxes are registers, which while they don't quite work in the same way as other ones do broadly follow the same rules. In particular, they are not macros, so

\newsavebox{\foo}
\sbox{\foo}{...}
\foo

would be inconsistent: \foo looks like a command (macro), but cannot be set using \newcommand, etc. There are also places where TeX 'expects' a reference to a box, not the typeset content of a box. As you almost point out, those are not LaTeX constructs (\wd is not a LaTeX command but a TeX primitive not moved 'out of the way' by LaTeX2e). However, in general the LaTeX2e kernel does leave us able to use plain TeX constructs along with LaTeX ones as a lot of the programming for LaTeX2e is done that way. Thus breaking

\newsavebox{\foo}% or \newbox\foo in TeX syntax
\wd\foo

would be problematic. (So would having \newsavebox and \newbox work fundamentally differently.)

Worth noting in this context is that

\ifdim\wd\somebox=...

is an expandable construct, while \widthof and similar are not expandable (they typeset the material in a temporary box for measurement). As such, while the \ifdim\wd syntax is TeX, not LaTeX, there are advantages to using it (e.g. works inside a \dimexpr). Thus there are things that are much easier/clearer to do using the register number rather than the content of the box.

Related Question