5

I frequently try out stuff and do a little introspection to find out why things (don't) work the way they do. Using boxes and obtaining their width, height and depth is an integral part of this.

As this is temporary hacky stuff I wonder, do I have go the long way of \newsavebox{\blah}\setbox{\blah}{content}. IIRC boxes are like registeres and they are numbered. So I think I can skip \newsavebox and just \savebox{16}{content}.

Is the above guess correct?

If it does work this way, what's a relatively save number to use, i.e.

How does latex give out box numbers?

0-255 or 255-0. I noticed the other day that boxes 0 and 255 have some special meaning and are better left alone.

Are there predefined boxes for such purposes, where it is never save to put content you want to reuse later on?

  • Just looking at the title I'd say \def\declarebox#1#2{\newsavebox{#1}\setbox{#1}{#2}}? But then suddenly you want something else in the main question? Heh. * Or \long\def*. Anyway, \newsavebox can be seen in the LaTeX source: \def\newsavebox#1{\@ifdefinable{#1}{\newbox#1}} - \newbox still has to be used seperately, e.g. \newbox\blah \savebox\blah{content} (see tex.stackexchange.com/questions/76224/…), so if I understand you correctly I'd say the answer is: no. (to that question) – 1010011010 Sep 25 '14 at 7:40
  • If you want to use registry boxes, use \setbox0={content}. These boxes are seperate from \newsavebox in the sense that they are already allocated and they have a numerical ID, rather than alphabetic. – 1010011010 Sep 25 '14 at 7:41
  • @1010011010 It's \setbox0=\hbox (or \vbox or ...) – Joseph Wright Sep 25 '14 at 7:45
  • @JosephWright Thanks. Didn't know that was mandatory. :-) – 1010011010 Sep 25 '14 at 7:55
  • 1
    @1010011010 \savebox?{...} is essentially \setbox?=\hbox{...} plus some other irrelevant things. Here ? is any number or control sequence representing a number (like what happens when you say \newsavebox{\foo}. No difference. – egreg Sep 25 '14 at 7:56
8

LaTeX's box allocation system is inherited from plain TeX, so the same rules apply in both cases. Boxes, like other registers, have numbers and as you observe can be addressed in that way. Really this is primitive syntax, not LaTeX syntax, so

\setbox0=\hbox{content}

is usably at a low level, but

\savebox{0}{content}

is really not to be encouraged!

Knuth laid out a scheme for making a certain number of registers available as scratch space which can be accessed by number. By convention, even numbered registers from 0 to 9 can be used as scratch space and set locally, while odd numbered registers in the same range are global scratch space. However, except at a very low level it's much more usual to use named scratch space: \@tempboxa is available in LaTeX, for example.

You can use any register directly by number, but you will potentially get into trouble as other code may have an allocated name for the same register and won't know what you are up to. I'd strongly suggest defining your own 'pool' boxes if you want some flexibility in saving material without creating new box names.

Note that the entire point of the higher level LaTeX syntax here is to provide tools for document authors, where normally having a dedicated named space for material is by far the best approach.

  • A point that's often neglected: after \newsavebox\foo, \foo simply represents a number. – egreg Sep 25 '14 at 7:58
  • 1
    @egreg Yes, I know that :-) However, it's also a single token and properly terminated, whereas an explicit number isn't necessarily one token. In any case, the LaTeX document level syntax seems clear enough: it's supposed to be a name. (Also, of course, the same is not true for other register types.) – Joseph Wright Sep 25 '14 at 8:00
  • @egreg The reason the one-token business is important is that if you reimplement \savebox or whatever, you don't necessary allow for or indeed want to allow 'raw' numbers. – Joseph Wright Sep 25 '14 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.