> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/in-depth-articles/18-how-does-expandafter-work-a-detailed-study-of-consecutive-expandafter-commands.md).

# How does \expandafter work: A detailed study of consecutive \expandafter commands

&#x20;[Part 1](/latex/in-depth-articles/19-how-does-expandafter-work-an-introduction-to-tex-tokens.md)   [Part 2](/latex/in-depth-articles/22-how-does-expandafter-work-the-meaning-of-expansion.md)   [Part 3](/latex/in-depth-articles/21-how-does-expandafter-work-tex-uses-temporary-token-lists.md)   [Part 4](/latex/in-depth-articles/20-how-does-expandafter-work-from-basic-principles-to-exploring-tex-s-source-code.md)   [Part 5](/latex/in-depth-articles/17-how-does-expandafter-work-a-detailed-macro-case-study.md)   [Part 6](/latex/in-depth-articles/18-how-does-expandafter-work-a-detailed-study-of-consecutive-expandafter-commands.md)&#x20;

## Case study: Understanding \expandafter\expandafter\expandafter...

Advanced macros, such as those found in LaTeX packages, often make extensive use of multiple consecutive `\expandafter` commands to perform sophisticated token “juggling”. For most of us, such macros can be difficult to understand or daunting to write. In this section we’ll review the mechanics underlying TeX’s processing of consecutive `\expandafter` commands:

```
\expandafter\expandafter\expandafter...
```

To assist our explanation we’ll add subscripts to each `\expandafter`—to indicate which one we are referring to:

```
        \expandafter1\expandafter2\expandafter3...
```

In addition, we’ll extend the notation for tokens processed by each `\expandafter` to use $$\mathrm{T^i\_1}$$ and $$\mathrm{T^i\_2}$$ representing the tokens $$\mathrm{T\_1}$$ and $$\mathrm{T\_2}$$ read-in by `\expandafter` with subscript `i`: `\expandafter`<sub>i</sub> $$\mathrm{T^i\_1T^i\_2}$$. We’ll also assume that two tokens $$\mathrm{T\_X}$$ and $$\mathrm{T\_Y}$$ follow after `\expandafter<sub>3</sub>` so that our input looks like this: `\expandafter<sub>1</sub>\expandafter<sub>2</sub>\expandafter<sub>3</sub>` $$\mathrm{T\_{X}T\_{Y}}$$.

When TeX starts to process this input, for `\expandafter<sub>1</sub>` it will see

* $$\mathrm{T^1\_1} =$$`\expandafter<sub>2</sub>` which is saved for later *re-insertion* back into the input
* $$\mathrm{T^1\_2} =$$`\expandafter<sub>3</sub>`, which is expanded

If we refer back to our earlier discussion of the [code inside TeX](/latex/in-depth-articles/20-how-does-expandafter-work-from-basic-principles-to-exploring-tex-s-source-code.md) which implements `\expandafter`, it is with expansion of `\expandafter<sub>3</sub>` that we see recursion taking place. To process `\expandafter<sub>1</sub>` TeX has already called its internal function `expand()`, so to process (expand) `\expandafter<sub>3</sub>` TeX is making a *second* call to `expand()`—from within the `expand()` function itself.

For `\expandafter<sub>3</sub>` we have

* $$\mathrm{T^3\_1 = T\_X}$$, which is saved for later *re-insertion* back into the input
* $$\mathrm{T^3\_2 = T\_Y}$$, which we’ll assume is expandable

Let’s further assume that the expansion of $$\mathrm{T\_Y}$$ yields the following sequence of tokens: $$\mathrm{{T^1\_Y}{T^2\_Y}{T^3\_Y}}\cdots\mathrm{T^N\_Y}$$. We’ve now reached the end of the expansion process started by the command sequence `\expandafter<sub>1</sub>\expandafter<sub>2</sub>\expandafter<sub>3</sub>` $$\mathrm{T\_{X}T\_{Y}}$$ and TeX proceeds to “unwind” the process of recursion which started with `\expandafter<sub>1</sub>`.

After processing `\expandafter<sub>3</sub>` TeX has, in its memory, a token list containing tokens from the expansion of $$\mathrm{T\_Y\text{: }{T^1\_Y}{T^2\_Y}{T^3\_Y}}\cdots\mathrm{T^N\_Y}$$. TeX now starts to re-insert the tokens it saved whilst processing `\expandafter` commands:

1. TeX begins with re-inserting token $$\mathrm{T\_X}$$ saved by `\expandafter<sub>3</sub>`. $$\mathrm{T\_X}$$ is re-inserted *in front* of the expansion of $$\mathrm{T\_Y}$$, which results in a token sequence: $$\mathrm{{{T\_X}T^1\_Y}{T^2\_Y}{T^3\_Y}}\cdots\mathrm{T^N\_Y}$$.
2. However, we still need to complete the process started by `\expandafter<sub>1</sub>` which saved the token representing `\expandafter<sub>2</sub>`
3. The final sequence of tokens assembled by TeX, ready for reading in the next stage of TeX’s processing is `\expandafter<sub>2 (token)</sub>` $$\mathrm{T\_{X}T^1\_{Y}T^2\_{Y}T^3\_{Y}\cdots T^N\_Y}$$
4. TeX has now finished the “first round” of processing and switches to reading the sequence of token lists it has generated—that sequence starts with `\expandafter<sub>2 (token)</sub>` which TeX proceeds to process. For `\expandafter<sub>2</sub>` we have
   * $$\mathrm{T^2\_1} =\ \mathrm{T\_X}$$ which is saved for later *re-insertion* back into the input
   * $$\mathrm{T^2\_2} = \mathrm{T^1\_Y}$$ which is the first token arising from expansion of $$\mathrm{T\_Y}$$; if expandable, it is expanded
5. If we assume token $$\mathrm{T^1\_Y}$$, the first token from expansion of $$\mathrm{T\_Y}$$, expands to $$\mathrm{{T^A\_{Y1}}{T^B\_{Y1}}{T^C\_{Y1}}}$$ then, after TeX has re-inserted $$\mathrm{T\_X}$$, the resulting token sequence to be re-processed by TeX would be: $$\mathrm{{T\_X}{T^A\_{Y1}}{T^B\_{Y1}}{T^C\_{Y1}}{T^2\_Y}{T^3\_Y}\cdots{T^N\_Y}}$$

   which we can re-state as

   $$\mathrm{T\_X}\text{\<expansion of the first token in }\mathrm{T\_Y}\text{>\<remaining tokens in }\mathrm{T\_Y}\text{>}$$

The following diagram illustrates the token lists created by the TeX code

```
    \expandafter1\expandafter2\expandafter3TXTY
```

![Image showing TeX processing multiple \expandafter commands](/files/wpIZORrWNrTnYVJ19Fi0)

### Theory into practice

By way of an example, we’ll define the following macros to serve as $$\mathrm{T\_X}$$ and $$\mathrm{T\_Y}$$

* $$\mathrm{T\_X}=$$`\foo` where we define `\foo` as `\def\foo#1{\textbf{#1}}`
* $$\mathrm{T\_Y}=$$`\bar` where we first define `\def\abc{Hello}`, `\def\xyz{, World!}` and then `\def\bar{\abc\xyz}`

We’ll use the following code fragment to demonstrate our previous analysis:

\expandafter\expandafter\expandafter\foo\bar

From our discussion, the result of `\expandafter<sub>1</sub>\expandafter<sub>2</sub>\expandafter<sub>3</sub>` $$\mathrm{T\_X}\mathrm{T\_Y}$$ produces a sequence of tokens of the form:

$$\mathrm{T\_X}\text{\<expansion of the first token in }\mathrm{T\_Y}\text{>\<remaining tokens in }\mathrm{T\_Y}\text{>}$$

where the exact sequence depends on the nature of token $$\mathrm{T\_Y}$$. If we insert our example commands using $$\mathrm{T\_X}=$$`\foo` and $$\mathrm{T\_Y}=$$`\bar`, which is defined as `\def\bar{\abc\xyz}`, we see:

* the first token in $$\mathrm{T\_Y}$$ is `\abc` and its expansion is a sequence of character tokens: `Hello`
* the remaining tokens in $$\mathrm{T\_Y}$$ is the single token representing `\xyz`.

If we plug this information into our “analysis” we get

$$\begin{align\*} &\mathrm{T\_X}\text{\<expansion of the first token in }\mathrm{T\_Y}\text{>\<remaining tokens in }\mathrm{T\_Y}\text{>}\ &=\text{foo}*{\text{token}}\text{\<expansion of \abc>}*\text{token list (characters)}\text{xyx}*\text{token}\ &=\text{foo}*\text{token}\text{Hello}*\text{token list (characters)}\text{xyx}*\text{token}\\\[10pt] \end{align\*}$$

Note that subscripts <sub>token</sub> and <sub>token list (characters)</sub> are used to emphasize (remind us) that TeX is reading *integer token values*, not *text characters*, hence there is no need to show any space characters or other delimiters after `\foo`: such delimiters have long since been processed or discarded; here we are firmly in TeX’s inner world of token lists and integer token values.

When TeX processes the token-list sequence produced by our `\expandafter` commands it will typeset `**H**ello, World!`—only `H` is typeset in bold. We could achieve the same result by writing the equivalent TeX code `\foo Hello\xyz`. Observe that the definition of `\foo` used a single parameter; consequently, `\foo` absorbs the single `H` character token for its argument, leaving the remaining character tokens (`ello`) untouched.

**Notes:**

* writing `\foo\bar` produces very different output: the token for `\bar` would be used as the argument for `\foo` which results in typesetting **`Hello, World`**—everything is typeset in bold.
* writing `\expandafter\foo\bar` causes `\bar` to be expanded, which produces two tokens: $$\text{abc}*\text{token}\text{xyz}*\text{token}$$. Then, after the $$\text{foo}*\text{token}$$ is re-inserted by `\expandafter` TeX process the token sequence $$\text{foo}*\text{token}\text{abc}*\text{token}\text{xyz}*\text{token}$$ which typesets `**Hello**, World`—only **`Hello`** is typeset in bold. Here, the single token $$\text{abc}*\text{token}$$ is processed as the argument for the macro token $$\text{foo}*\text{token}$$, leaving the token $$\text{xyz}\_\text{token}$$ untouched and its content typeset in the current font.

## Note on \expandafter and macros with arguments

When using `\expandafter` to force expansion of macros it’s worth knowing how macro expansion works—particularly for macros that take arguments. Before TeX can run a macro—i.e., read and process tokens contained within the macro’s definition—TeX needs to get the macro “ready to run” by performing the initial macro-expansion process. If a macro’s definition includes the use of parameters (`#1`, `#2`, ... `#9`), part of the macro-expansion process requires TeX to scan the input looking for tokens comprising the argument(s) provided by the user: those argument tokens are absorbed (removed) from the input. During macro expansion TeX reads and absorbs tokens from the input to create mini token-lists, one list per argument; those token lists will subsequently be inserted into the appropriate location within the body of the macro—when TeX executes it. The final step in macro-expansion involves TeX locating the macro definition stored in memory and arranging for that location to become the source from which TeX will read its next set of input tokens. Macro execution commences when TeX starts to read and process those tokens, feeding-in the token lists previously created to store the arguments.

### Delimiter tokens are also absorbed

If the original macro definition also used tokens acting as delimiters, TeX would also need to compare the original macro definition with the user’s use (call) of that macro, looking to find and match delimiter tokens. Once matched/located, delimiter tokens are subsequently ignored because their sole purpose is to act as “punctuation”, helping TeX pick-out and identify the actual tokens destined to form each argument.

&#x20;[Part 1](/latex/in-depth-articles/19-how-does-expandafter-work-an-introduction-to-tex-tokens.md)   [Part 2](/latex/in-depth-articles/22-how-does-expandafter-work-the-meaning-of-expansion.md)   [Part 3](/latex/in-depth-articles/21-how-does-expandafter-work-tex-uses-temporary-token-lists.md)   [Part 4](/latex/in-depth-articles/20-how-does-expandafter-work-from-basic-principles-to-exploring-tex-s-source-code.md)   [Part 5](/latex/in-depth-articles/17-how-does-expandafter-work-a-detailed-macro-case-study.md)   [Part 6](/latex/in-depth-articles/18-how-does-expandafter-work-a-detailed-study-of-consecutive-expandafter-commands.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/in-depth-articles/18-how-does-expandafter-work-a-detailed-study-of-consecutive-expandafter-commands.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
