> 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/zh-cn/shen-ru-wen-zhang/03-a-six-part-article-series-on-expandafter-tex-tokens-and-expansion.md).

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

## 概述

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

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

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