TeX expansion (source code)

= TeX expansion
{c}

\b[TeX expansion] replaces expandable <TeX tokens> with other tokens according to their current meanings and the scanning context. <TeX macros> provide replacement lists; some <TeX primitives> also expand. Subsequent processing can execute commands or build <typesetting boxes>.<TeX by Topic by Victor Eijkhout>[§§1.3–1.4 and12.1–12.2: expansion and execution processors]{ref}

For example, `\def\firstdemo{AB}\def\seconddemo{\firstdemo}` establishes two macros. Expanding `\seconddemo` first produces the token for `\firstdemo`; expanding that yields the character tokens `A` and `B`. The intermediate result is a token stream. The printed shapes are determined later by <computer fonts> and layout.

Meaning can change between definition and use. With `\def\value{A}\def\callvalue{\value}`, redefining `\value` to expand to `B` changes the later expansion of `\callvalue` to `B`. The stored <TeX control sequence> still identifies the same name, while its current definition supplies the replacement. This makes the distinction between token identity and current meaning practically useful.