> 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-tw/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` 用來執行各式各樣「token 來回操弄」的命令 `...\expandafter\expandafter\expandafter...`.

本系列文章有一個雄心勃勃的目標：說明 *如何* 該 `\expandafter` 命令，以及 TeX 展開，實際上是如何運作的——從基本原理一路深入到探討發生在 TeX 引擎軟體深處的處理過程。讀者可以直接跳到 [文章](#accessing-articles-the-navigation-bar) ，或先閱讀一篇 [引言](#introduction-and-who-is-this-series-for) 以及本系列的原因與構想。

## 存取文章：導覽列

每篇文章在正文前後都有以下導覽列，方便你快速跳到本系列的另一篇文章：

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

以下是本系列 6 篇文章的連結：

1. [TeX token 簡介](/latex/zh-tw/shen-ru-wen-zhang/19-how-does-expandafter-work-an-introduction-to-tex-tokens.md)
2. [展開的意義](/latex/zh-tw/shen-ru-wen-zhang/22-how-does-expandafter-work-the-meaning-of-expansion.md)
3. [TeX 使用暫時性 token 清單](/latex/zh-tw/shen-ru-wen-zhang/21-how-does-expandafter-work-tex-uses-temporary-token-lists.md)
4. [從基本原理到探索 TeX 的原始碼](/latex/zh-tw/shen-ru-wen-zhang/20-how-does-expandafter-work-from-basic-principles-to-exploring-tex-s-source-code.md)
5. [巨集詳細案例研究](/latex/zh-tw/shen-ru-wen-zhang/17-how-does-expandafter-work-a-detailed-macro-case-study.md)
6. [連續的詳細研究 `\expandafter` 命令](/latex/zh-tw/shen-ru-wen-zhang/18-how-does-expandafter-work-a-detailed-study-of-consecutive-expandafter-commands.md)

## 簡介：本系列適合誰？

### \expandafter：運作的基本原理

\expandafter 的目的，是強制輸入中的某個 token 提前展開：給定輸入中的兩個連續 token， $$\mathrm{T\_1}$$ 和 $$\mathrm{T\_2}$$, `\expandafter` 會儲存 token $$\mathrm{T\_1}$$，展開 token $$\mathrm{T\_2}$$ 並將 token $$\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`行為的標準描述，我們注意到它會「儲存 token $$\mathrm{T\_1}$$」，強制 token $$\mathrm{T\_2}$$ （在 TeX 通常會這麼做之前）進行「展開」，然後將 token $$\mathrm{T\_1}$$ 「放回輸入中」，讓 TeX 再次讀取。本文作者回想起來，曾對 `\expandafter`的「token 來回操弄」行為中的某些面向感到困惑；特別是：

* 「儲存一個 token」或「將一個 token 放回輸入中」到底是什麼意思——這些被儲存的 token 會「去哪裡」，而 TeX 又是如何重新讀取它們的？
* 展開看起來是什麼樣子，或它真正的意思是什麼？
* TeX 如何處理多個連續的 `\expandafter` 命令——那究竟是如何 *真正地* 運作？

### 本系列文章的目標

本系列試圖透過記錄對 TeX 內部運作研究的結果來回答上述問題。Overleaf 建立了某個 TeX 引擎的客製化版本，並使用 [Eclipse IDE](https://www.eclipse.org/ide/) 來觀察 TeX 原始碼在處理一個 `.tex` 包含 …… 範例用法的檔案時的運作情形 `\expandafter`.

我們將分享該研究的成果，並探討：

* TeX token 的真正本質／意義；
* TeX 產生／使用暫時性 token 清單的方式；
* 「展開」真正的意思。

透過探討這些主題，我們將拼湊出整體圖像，以說明 *如何* `\expandafter` 如何執行其「token 來回操弄」行為。本系列文章是為任何對這些主題有興趣、希望培養洞見的人所寫：深入檢視 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-tw/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.
