7

Having read some chapters of the texbook, I know tex eats input lines from command line or files, then converts them into tokens. When these tokens go into its gastro-intestinal tract, tex prepares to digest them by converting them into boxes, glues and other whatsit, then construts some lists.

With regard to macros, in which stage tex save its macro definition, in which stage tex do the macro expansion for tex replacement?

2 Answers 2

8

That's easy! TeX always does macro expansion, except when it doesn't.

On page 215, second double dangerous paragraph, we read

Expansion is suppressed at the following times:

  • When tokens are being deleted during error recovery (see Chapter 6).
  • When tokens are being skipped because conditional text is being ignored.
  • When TeX is reading the arguments of a macro.
  • When TeX is reading a control sequence to be defined by \let, \futurelet, \def, \gdef, \edef, \xdef, \chardef, \mathchardef, \countdef, \dimendef, \skipdef, \muskipdef, \toksdef, \read, and \font.
  • When TeX is reading argument tokens for \expandafter, \noexpand, \string, \meaning, \let, \futurelet, \ifx, \show, \afterassignment, \aftergroup.
  • When TeX is absorbing the parameter text of a \def, \gdef, \edef, or \xdef.
  • When TeX is absorbing the replacement text of a \def or \gdef or \read; or the text of a token variable like \everypar or \toks0; or the token list for \uppercase or \lowercase or \write. (The token list for \write will be expanded later, when it is actually output to a file.)
  • When TeX is reading the preamble of an alignment, except after a token for the primitive command \span or when reading the after \tabskip.
  • Just after a token such as $3 that begins math mode, to see if another token of category 3 follows.
  • Just after a `12 token that begins an alphabetic constant.

Not unreasonable, is it? When doing definitions, we want that nothing is expanded (except for \edef and \xdef) and the fourth, fifth, sixth and seventh bullets deal with this. Similarly if we want to store a token list in a register or in \write.

Similarly, the token after \expandafter, \noexpand, \afterassignment or \aftergroup must not be expanded for obvious reasons; it will later, when TeX examines it again at the appropriate time.

The last bullet has a technical reason: if you want to refer to an alphabetic constant that corresponds to a character with \catcode 0, 5, 9, 13, 14 or 15 it can be “escaped” with a backslash in front of it, but this actually doesn't form a control sequence. So you can do `\^^M if you want to refer to the constant 13 or do \chardef\%=`\%

The second bullet can be supplemented by an important remark: notwithstanding that TeX does no expansion when skipping conditional text, it does examine the tokens in order to match conditionals with their \else or \fi. Any token that is \let to a primitive conditional, to \else or \fi counts under this respect.

1
  • 1
    Let me absorb this for a moment :) I am reading this chapter at page 204.
    – Lucas
    May 14 at 10:17
1

In the text TeX in a Nutshell the commands and their parameters are written in blue color, but if a parameter is unexpanded, then it is written in red color. Parameters written here in blue color are read after expansion. See this text from page 9 where the summary of TeX primitive constructs begins.

1
  • thanks for the resource. A few days ago, I emailed the author, asked him if his books have an English version :) he said ``unfortunately ...''
    – Lucas
    May 15 at 11:15

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.