> 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/ja/xiang-xi-ji-shi/03-a-six-part-article-series-on-expandafter-tex-tokens-and-expansion.md).

# \expandafter、TeXトークン、展開に関する全6回の記事シリーズ

## 概要

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

連載の6本の記事へのリンクはこちらです：

1. [TeXトークン入門](/latex/ja/xiang-xi-ji-shi/19-how-does-expandafter-work-an-introduction-to-tex-tokens.md)
2. [展開の意味](/latex/ja/xiang-xi-ji-shi/22-how-does-expandafter-work-the-meaning-of-expansion.md)
3. [TeXは一時的なトークンリストを使う](/latex/ja/xiang-xi-ji-shi/21-how-does-expandafter-work-tex-uses-temporary-token-lists.md)
4. [基本原理からTeXのソースコードの探究へ](/latex/ja/xiang-xi-ji-shi/20-how-does-expandafter-work-from-basic-principles-to-exploring-tex-s-source-code.md)
5. [マクロの詳細なケーススタディ](/latex/ja/xiang-xi-ji-shi/17-how-does-expandafter-work-a-detailed-macro-case-study.md)
6. [連続したものの詳細な研究 `\expandafter` コマンド](/latex/ja/xiang-xi-ji-shi/18-how-does-expandafter-work-a-detailed-study-of-consecutive-expandafter-commands.md)

## はじめに、そしてこの連載は誰のためのものか？

### \expandafter：動作の基本原理

\expandafterの目的は、入力中のトークンを早期に展開させることです。入力中に2つの連続したトークンがあるとすると、 $$\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エンジンの1つをカスタマイズした版を作成し、 [Eclipse IDE](https://www.eclipse.org/ide/) それが `.tex` の使用例を含む `\expandafter`.

その研究結果を共有し、以下を検討します：

* TeXトークンの真の性質／意味；
* TeXによる一時的なトークンリストの生成／使用；
* 「展開」が本当に意味するもの。

これらの話題を掘り下げることで、全体像を組み立てて *どのように* `\expandafter` が「トークンのやりくり」の動作を行う仕組みを説明します。この連載は、これらの話題への理解を深めたい人向けに書かれています。TeXそのものの内部をより深く見て、 `\expandafter` その魔法のような働きを可能にしているTeX内部の動作部分を検証します。

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/ja/xiang-xi-ji-shi/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.
