5

This exercise asks how to store expansion of \chapno at the moment when \write is processed, but \the\count0 must be expanded at the moment of \shipout. Here is a full example:

\def\chapno{5}
\newwrite\cont \openout\cont=out.txt
\edef\next{\write\cont{\chapno,\noexpand\the\count0}}\next
\def\chapno{6}
\count0=100
\bye

A curious technique is used in the answer:

{\let\the=0\edef\next{\write\cont{<token list>}}\next}

Is using \noexpand equivalent to the \let-trick? And why \noexpand is not used in the answer - just to show the possibilities?

3
  • It's not the same. \noexpand\the actually expands to \the, whereas by using \let\the=0 you make \the an unexpandable token. Dec 2, 2019 at 6:33
  • @HenriMenke will the token list in \write after both methods be the same? (in all possible cases - not just in this example) Dec 2, 2019 at 6:40
  • In this case the token lists will be the same, but the \let\the=0 makes all \the tokens inside the group unexpandable, whereas \noexpand can be applied selectively. Dec 2, 2019 at 6:54

1 Answer 1

6

This is essentially how the latex \protected@write works as well:

\long\def \protected@write#1#2#3{%
      \begingroup
       \let\thepage\relax
       #2%
       \let\protect\@unexpandable@protect
       \edef\reserved@a{\write#1{#3}}%
       \reserved@a
      \endgroup
      \if@nobreak\ifvmode\nobreak\fi\fi
}

Typically in such a construct you can use \noexpand in front of tokens that you have control of while writing the macro but the <token list> in your example will usually come as #1 from some section title argument in the document, but you want to suppress the expansions of all registers until \shipout and the text may refer to any of them. so making \the non expandable (it doesn't matter much what you \let it to, 0 or \relax or any other non expandable token then it will stay as itself in the \edef but have its normal expandable definition in the \write.

Note the exercise has the easy case where the chapter number is an expandable macro. In most formats chapters would be automatically numbered so using a count register and also perhaps ultimately accessed by \the in which case suppressing all \the expansion might be too much. (Note LaTeX just suppresses \thepage letting other counters expand at the point of use, not the final shipout).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .