# Why get control sequences expanded in the process of writing them to an auxiliary file?

I was looking at LamsTeX, an old TeX format, because it has some interesting macros. I found this article about it. In the article we can read

\bitem Many \LaTeX\ difficulties just now being addressed have simply never existed in \LamSTeX{}. For example, there are no such things as fragile' commands in \LamSTeX{}, and no worries about long |\write|'s and undesired expansions in |\write|'s to auxiliary files (like the |.toc| file). Arbitrarily long literal mode material can split across pages.

So I got interested and made some experiments with LamsTeX. Looking at the .toc auto-generated file I found that LamsTeX saves all control sequences without expansion, like if they were all "protected" using LaTeX or OPmac terminology. This brought me up a question: what's the need of expanding a control sequence before writing it? Wouldn't it be better to simply write it without expansion?

I have looked at other questions related to this argument, but I can't find the answer to this question.

• One reason may be that writing control sequences to the .aux files may occur to the time when the sequence is defined in the document body (speaking of LaTeX right now), but this command is not available then at the time when the auxiliary files is read – user31729 Mar 11 '16 at 21:14
• also many commands need to be expanded to work, pagenumbers written to the aux file for cross references would not work if they were all written as \thepage but in general it is true that making things not expand would probably be the default if etex or more memory had been available in 1980s – David Carlisle Mar 11 '16 at 21:22
• etex \protected attaches an extra flag to each control sequence that determines whether it expands in a write, however the clasic latex \protect mechanism makes a command robust via defining \def\foo{\protect\internalfoo}, \def\internalfoo{real stuff} so making all commands robust would double the number of csnames used. when latex2e launched in 93 then latex+amsmath left around 50 csnames free in emtex, so using twice as many csnames in the format wasn't an option. – David Carlisle Mar 11 '16 at 21:33
• possibly (using tokens registers) but asking what might have been in Leslie Lamports head last century in an alternate universe is hard to answer:-) – David Carlisle Mar 11 '16 at 21:45
• leslie lamport and michael spivak obviously had very different ideas of how to do things. i haven't looked at lamstex for a long time, but it did implement referencing that wasn't included in amstex, apparently in a much different way than did latex, and it didn't have any more memory to work with. so that's not a good argument. – barbara beeton Mar 11 '16 at 21:49

There are typically four fields with information about section when saving to temporary file:

• The control sequence prefixed before all others, executed when the data are read again using \input. It must be no expanded during writing process.

• The number of section, chapter etc. It must be expanded immediately.

• The title of the section. It should be no expanded during writing.

• The number of the page. It must be expanded during \shipout.

LaTeX manages the third field as: "expand it during \shipout but use \noexpand expansion of so called robust commands". OPmac does something similar, only control sequences marked as "protected" are set to \relax during \shipout. But there exists another approach which definitely does not expand the section title during \shipout. For example, this can be done like this (classical TeX is expected, no eTeX).

\newcount\secnum
\newwrite\outfile
...
\def\meaninga#1{\expandafter\meaningb\meaning#1\relax}
\def\meaningb#1->#2\relax{#2}

First field is \secX here protected by \string. Second field is \the\secnum here expanded during \edef\tmp. Third field (section title) is protected using \meaning primitive, i.e. it is detokenized immediately when \edef\tmp is processed. Fourth field is \the\pageno and it is expanded during \shipout. Maybe something similar is done in LamsTeX (I don't know exactly).
Of course, if we have eTeX then we can do the detokenization more straightforward using \detokenize primitive. And the alternative "protecting" approach can be done more straightforward too: using \protected\def.
Note that you cannot use \the\toks for expansion protecting of section title because this doesn't do catcode changing and you cannot use \the\toks in \shipout` time because there can be more than one section at one page.