# When should I use a \box register rather than \def? [duplicate]

\def is sometimes used as an abbreviation of a box construction, then its output is sometimes the same as the output with a \box register instead of \def.

Here is the question. When should I use a \box register rather than \def? Is there any specific situation one is better than the other one?

# EDITED (09/12/09 UTC 2:45)

## Edited question

Is there any cases that \setbox and its output commands pair (not only \box0, but also \copy0, \unhbox0, and so on) can be replaced a macro definition which is eventually expanded as a box?

If there was such a case, is there any specific situation one is better than the other one?

## Examples I thought

Case 1:

\def\excuse{I should have made my question more clearer before the post.}
\setbox0=\hbox{I should have made my question more clearer before the post.}


I think the above case is the same as long as I call the box register with \unhcopy0. However, I wonder whether first one is better than second one because it can save memory.

Case2:

\def\excuse{\hbox{I should have made my question more clearer before the post.}}
\setbox0=\hbox{I should have made my question more clearer before the post.}


I think the above case is also the same as long as I call the box register with \copy0.

Case3:

\def\myhrulefill{\line{\leaders\hrule\hfill}}


in this case, \hbox contains commands and which I suspected whether I can say those are the same or not.

Unfortunatelly, I cannot show you a sufficientlly complex example which contains some commands. Parhaps, if above examples contained conditional syntax like \if, repetion syntax like \loop...\repeat, one cannot be an alternative of the other one. I especially like to know such a case.

• They are completely orthogonal concepts. A \def expands to input, whereas a \box stores typeset output. – Henri Menke Dec 8 '19 at 4:14

The only thing in common between a box and a macro (defined with \def) is that they store stuff. Period.

A macro stores arbitrary TeX code to be reused in the document for a plethora of reasons. With macros you can make anything from simplifying writing Arnold Schwarzenegger's name to emulating TeX itself, given you have enough macros doing the right things.

A box, on the other hand, stores typeset text. Just that.

For simple enough usages, both can seem to do the same. For example:

\setbox0=\hbox{Arnold Schwarzenegger}
\def\arnold{Arnold Schwarzenegger}
Here's \box0{} and \arnold{} again.


will typeset:

however the basic difference between \box0 and \arnold, in the example above, is that \box0 had already typeset the name in the box, and the command \box0 just laid it out in the page. \arnold, on the other hand, got expanded into a sequence of characters which was then typeset and happened to have the same output.

To see the difference, suppose I change the font after setting the box, but before writing it out:

\setbox0=\hbox{Arnold Schwarzenegger}
\def\arnold{Arnold Schwarzenegger}
\bf Here's \box0{} and \arnold{} again.


the output will be:

See how the contents of the box didn't use the bold font. That's because it was already typeset at that time, and the box was only used.

Another difference, is that once the text is typeset in a box you can measure it:

\setbox0=\hbox{Arnold Schwarzenegger}
Here's \copy0{} typeset in a \the\wd0{} box.


you can try and do that with a macro, but it won't work.

With a macro, however, you can have “dynamic content”, while with the box, as explained in the font change example above, everything is fixed at the time you use \setbox. For (a very simple) example:

\def\arnold#1{Arnold #1}
Here's \arnold{Schwarzenegger} and another \arnold{Foo}.


which cannot be done with boxes.

So when choosing one or another you have to look at what type of data you are trying to store and then choose. Only for trivial cases, like the first example, the choice could be ambiguous, and yet if you change the example by a tiny bit (like adding a font change), then one way or the other will be wrong.

In short, macros are for TeX code, and boxes are for typeset text. Choose whatever is right for the specific situation.