Here's an example of some code from LaTeX's \DeclareFontFamily:

\expandafter\let\csname #1+#2\expandafter\endcsname
       \ifx \reserved@a\@empty
       \else \reserved@a

I could be wrong, but to me this looks identical to:


Now, the source for this code comments:

We compare \reserved@a with \@empty If these two are the same we \let the ‘extra’ macro equal to \@empty which is not the same a doing a \let to \reserved@a — the latter would blow one extra memory location rather then reusing the one from \@empty.

But if we're not re-using the contents of \reserved@a after this point (which we're not, as far as I can see), there's no reason to go this roundabout route, right?


For a full answer you'd need to ask one of the people who might have written this (possible Frank Mittelbach or even Leslie Lamport - I wonder if this has come all the way from 2.09). However, what I think is happening is that when \reserved@a is not equal to \@empty then there is no saving, as at some later stage \reserved@a will be needed again. On the other hand, when \reserved@a is equal to \@empty then a memory position is saved, as \@empty never changes and so the two macros always have the same definition as each other. When this code was written, such small savings were probably really important (they all add up).

  • 1
    Do I read that correctly that TeX uses some form of copy-on-write with macros defined using \let? This is very interesting. – Martin Scharrer Feb 23 '11 at 9:37
  • 6
    @Martin: Macros \let to each other have only one definition in memory (the hash table entries point to the same definition), whereas \def creates independent definitions even if the two are identical. – Joseph Wright Feb 23 '11 at 10:06
  • I think I now have an alternative explanation: \def\a{}\def\b{} takes more (or twice as much) memory as \def\a{}\let\b\a. (I still think \reserved@a isn't reused.) Hence my first suggestion wouldn't be a good idea (back then when memory was scarce). WDYT? More importantly, does it matter these days? – Will Robertson Feb 23 '11 at 13:33
  • @Will: That's what I've tried to say - \reserved@a isn't the key here, it's the \@empty branch that does the saving. I'd say today it doesn't matter at all: one of the problems with the current kernel is that these tricks make maintenance much harder. – Joseph Wright Feb 23 '11 at 13:37
  • @Will. Isn't \reserved@a used in e.g. environments, and everywhere in LaTeX? – Bruno Le Floch Feb 23 '11 at 13:41

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.