> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-wen-zhang/03-a-six-part-article-series-on-expandafter-tex-tokens-and-expansion.md).

# 关于 \expandafter、TeX token 和展开的六部分文章系列

## 概述

TeX 原语 `\expandafter` 是一个用途广泛的命令，被 LaTeX/TeX 程序员用来创建复杂的宏——例如 LaTeX 宏包提供的命令（宏）。表面上， `\expandafter` 很容易使用，但当你遇到更复杂的构造时，这层简单的面具很快就会褪去，例如多个连续的 `\expandafter` 用于执行各种“记号摆弄”的命令 `...\expandafter\expandafter\expandafter...`.

本系列文章有一个雄心勃勃的目标：解释 *如何* 该 `\expandafter` 命令以及 TeX 展开究竟是如何工作的——从基本原理到探索发生在 TeX 引擎软件深处的过程。读者可以直接跳到这些 [文章](#accessing-articles-the-navigation-bar) ，或者阅读一篇 [引言](#introduction-and-who-is-this-series-for) 以及本系列的原因和目的。

## 访问文章：导航栏

每篇文章在正文前后都有如下导航栏，这样你就可以快速跳转到本系列中的其他文章：

[第 1 部分](/latex/zh-cn/shen-du-wen-zhang/19-how-does-expandafter-work-an-introduction-to-tex-tokens.md) [第 2 部分](/latex/zh-cn/shen-du-wen-zhang/22-how-does-expandafter-work-the-meaning-of-expansion.md) [第 3 部分](/latex/zh-cn/shen-du-wen-zhang/21-how-does-expandafter-work-tex-uses-temporary-token-lists.md) [第 4 部分](/latex/zh-cn/shen-du-wen-zhang/20-how-does-expandafter-work-from-basic-principles-to-exploring-tex-s-source-code.md) [第 5 部分](/latex/zh-cn/shen-du-wen-zhang/17-how-does-expandafter-work-a-detailed-macro-case-study.md) [第 6 部分](/latex/zh-cn/shen-du-wen-zhang/18-how-does-expandafter-work-a-detailed-study-of-consecutive-expandafter-commands.md)

以下是本系列 6 篇文章的链接：

1. [TeX 记号简介](/latex/zh-cn/shen-du-wen-zhang/19-how-does-expandafter-work-an-introduction-to-tex-tokens.md)
2. [展开的含义](/latex/zh-cn/shen-du-wen-zhang/22-how-does-expandafter-work-the-meaning-of-expansion.md)
3. [TeX 使用临时记号列表](/latex/zh-cn/shen-du-wen-zhang/21-how-does-expandafter-work-tex-uses-temporary-token-lists.md)
4. [从基本原理到探索 TeX 源代码](/latex/zh-cn/shen-du-wen-zhang/20-how-does-expandafter-work-from-basic-principles-to-exploring-tex-s-source-code.md)
5. [一个详细的宏案例研究](/latex/zh-cn/shen-du-wen-zhang/17-how-does-expandafter-work-a-detailed-macro-case-study.md)
6. [连续的详细研究 `\expandafter` 命令](/latex/zh-cn/shen-du-wen-zhang/18-how-does-expandafter-work-a-detailed-study-of-consecutive-expandafter-commands.md)

## 导言以及本系列适合谁？

### \expandafter：基本操作原理

\expandafter 的作用是强制对输入中的某个记号进行提前展开：给定输入中的两个连续记号， $$\mathrm{T\_1}$$ 和 $$\mathrm{T\_2}$$, `\expandafter` 将保存记号 $$\mathrm{T\_1}$$，展开记号 $$\mathrm{T\_2}$$ 并将记号 $$\mathrm{T\_1}$$ 放回到 $$\mathrm{T\_2}$$:

$$\verb\*\expandafter\*\ \mathrm{T\_1}\mathrm{T\_2} \rightarrow \mathrm{T\_1}\text{\<expansion of }\mathrm{T\_2}\text{>}$$

在标准的 `\expandafter`行为描述之后，我们注意到它会“保存记号 $$\mathrm{T\_1}$$”，强制对记号 $$\mathrm{T\_2}$$ 进行“展开”（在 TeX 正常执行之前），然后把记号 $$\mathrm{T\_1}$$ “放回输入中”供 TeX 再次读取。本文作者回忆起自己曾被 `\expandafter`的“记号摆弄”行为中的某些方面弄得困惑；特别是：

* “保存一个记号”或“把一个记号放回输入中”是什么意思——这些被保存的记号“去了哪里”，TeX 又是如何重新读取它们的？
* 展开“看起来像什么”，或者说它究竟是什么意思？
* TeX 如何处理多个连续的 `\expandafter` 命令——那 *究竟* 是如何工作的？

### 本系列文章的目标

本系列旨在通过记录对 TeX 内部工作机制研究的结果，来回答上面提出的问题。Overleaf 构建了其中一种 TeX 引擎的定制版本，并使用 [Eclipse IDE](https://www.eclipse.org/ide/) 来观察 TeX 源代码在处理一个 `.tex` 包含对 `\expandafter`.

示例用法的文件时是如何运行的：我们将分享这些研究结果，以探讨：

* TeX 记号的真实本质/含义；
* TeX 对临时记号列表的生成/使用；
* “展开”究竟真正意味着什么。

通过对这些主题的探讨，我们将拼合出全貌，以解释 *如何* `\expandafter` 是如何表现出其“记号摆弄”行为的。本系列文章面向任何希望对这些主题形成深入理解的人：更深入地审视 TeX 本身，考察 TeX 内部运作中那些使 `\expandafter` 发挥魔力的部分。

撰写关于 TeX 及与 TeX 相关概念/原理的文章颇具挑战，因为 TeX 是一款极其复杂的软件——其排版后的源代码长达数百页。例如，看看这个 [Overleaf 项目](https://www.overleaf.com/latex/examples/typeset-the-source-code-documentation-for-tex-e-tex-or-pdftex/qkgfgyspnhcv) ，它默认会排版 $$\mathrm{\varepsilon}\text{-}\mathrm{\TeX}$$的源代码：排版后的 PDF 文件超过 600 页。

从内部来看，TeX 是一个错综复杂、彼此关联的动作与过程网络；当我们试图讨论其行为的某个孤立方面时，这会带来挑战：例如剖析出某个特定“组件”，如 `\expandafter`，并试图在不不断引用 TeX 软件其他方面的情况下解释它。就其本质而言， `\expandafter` 命令参与了 TeX 的 *展开*过程——这个过程对我们许多人来说是一个模糊且有些难以完全理解的概念。然而，展开确实是 TeX 行为的一个根本方面，它贯穿于 TeX 的内部运作之中，并在 TeX 软件内部深处发生。在本系列文章中，我们将探究展开的真实含义，首先汇集一组相关的原理和行为，以足以构建对展开的基本理解。在此基础上，我们最后将进行一次“深潜”，深入 TeX 内部，探索实现 TeX 展开机制的实际源代码。

## 总而言之

在这些文章中，我们努力提供对尽可能多的读者有用/有价值或有兴趣的内容：在简洁与不可避免的细节之间寻求平衡，同时也尽量不因提供过多或过少的上下文材料而让读者失去耐心。我们希望这些内容能为 TeX 文献贡献一些有用/有价值的东西，供现在以及也许未来的 TeX 社区成员乐于阅读。

祝 $$\mathrm{\TeX}\text{ing!}$$，来自 Graham Douglas 和 Overleaf 团队。


---

# 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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-wen-zhang/03-a-six-part-article-series-on-expandafter-tex-tokens-and-expansion.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.
