> 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/18-how-does-expandafter-work-a-detailed-study-of-consecutive-expandafter-commands.md).

# \expandafter はどのように動作するか：連続する \expandafter コマンドの詳細な研究

&#x20;[第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)&#x20;

## ケーススタディ: \expandafter\expandafter\expandafter... の理解

LaTeXパッケージに見られるような高度なマクロは、しばしば連続する複数の `\expandafter` コマンドを多用して、巧妙なトークンの「やりくり」を行います。私たちの大半にとって、そのようなマクロは理解しにくかったり、書くのが難しく感じられたりします。この節では、TeX が連続する `\expandafter` コマンド:

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

説明を助けるために、それぞれの `\expandafter`—どれを指しているのかを示すために:

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

さらに、各 `\expandafter` に対して処理されるトークンの表記を拡張して $$\mathrm{T^i\_1}$$ と $$\mathrm{T^i\_2}$$ を表す $$\mathrm{T\_1}$$ と $$\mathrm{T\_2}$$ によって読み込まれた `\expandafter` 添字付きの `i`: `\expandafter`<sub>i</sub> $$\mathrm{T^i\_1T^i\_2}$$。また、2つのトークンが $$\mathrm{T\_X}$$ と $$\mathrm{T\_Y}$$ の後に続く `\expandafter<sub>3</sub>` ようにして、入力は次のようになります: `\expandafter<sub>1</sub>\expandafter<sub>2</sub>\expandafter<sub>3</sub>` $$\mathrm{T\_{X}T\_{Y}}$$.

TeX がこの入力の処理を開始すると、 `\expandafter<sub>1</sub>` については

* $$\mathrm{T^1\_1} =$$`\expandafter<sub>2</sub>` これは後で使うために保存され *再挿入* して入力に戻す
* $$\mathrm{T^1\_2} =$$`\expandafter<sub>3</sub>`、そして展開されます

先ほどの [TeX 内部のコード](/latex/ja/xiang-xi-ji-shi/20-how-does-expandafter-work-from-basic-principles-to-exploring-tex-s-source-code.md) を実装している `\expandafter`、展開によって `\expandafter<sub>3</sub>` 再帰が起きているのがわかります。 `\expandafter<sub>1</sub>` TeX はすでに内部関数 `expand()`を呼び出しているので、（展開して）処理するには `\expandafter<sub>3</sub>` TeX は *2回目の* 呼び出しを行っています `expand()`—その `expand()` 関数自身から。

については `\expandafter<sub>3</sub>` があります

* $$\mathrm{T^3\_1 = T\_X}$$、これは後で使うために保存され *再挿入* して入力に戻す
* $$\mathrm{T^3\_2 = T\_Y}$$、これは展開可能だと仮定します

さらに、 $$\mathrm{T\_Y}$$ の展開が次のトークン列を返すものと仮定しましょう: $$\mathrm{{T^1\_Y}{T^2\_Y}{T^3\_Y}}\cdots\mathrm{T^N\_Y}$$。これで、コマンド列によって始まった展開処理は終端に達しました `\expandafter<sub>1</sub>\expandafter<sub>2</sub>\expandafter<sub>3</sub>` $$\mathrm{T\_{X}T\_{Y}}$$ そして TeX は、そこから始まった再帰処理を「巻き戻し」ていきます `\expandafter<sub>1</sub>`.

を処理した後、 `\expandafter<sub>3</sub>` TeX のメモリには、 $$\mathrm{T\_Y\text{: }{T^1\_Y}{T^2\_Y}{T^3\_Y}}\cdots\mathrm{T^N\_Y}$$の展開から得られたトークンを含むトークンリストがあります。TeX は今、 `\expandafter` コマンド:

1. TeX はトークンの再挿入から始めます $$\mathrm{T\_X}$$ が保存した `\expandafter<sub>3</sub>`. $$\mathrm{T\_X}$$ が再挿入されます *の前に* の展開 $$\mathrm{T\_Y}$$、その結果トークン列は次のようになります: $$\mathrm{{{T\_X}T^1\_Y}{T^2\_Y}{T^3\_Y}}\cdots\mathrm{T^N\_Y}$$.
2. ただし、まだ `\expandafter<sub>1</sub>` は、 `\expandafter<sub>2</sub>`
3. TeX によって組み立てられた最終的なトークン列で、TeX の次の処理段階で読み取る準備ができているものは `\expandafter<sub>2 (token)</sub>` $$\mathrm{T\_{X}T^1\_{Y}T^2\_{Y}T^3\_{Y}\cdots T^N\_Y}$$
4. TeX はこれで「最初のラウンド」の処理を終え、自身が生成したトークンリストの列の読み取りに切り替えます—その列は `\expandafter<sub>2 (token)</sub>` を TeX は引き続き処理します。 `\expandafter<sub>2</sub>` があります
   * $$\mathrm{T^2\_1} =\ \mathrm{T\_X}$$ これは後で使うために保存され *再挿入* して入力に戻す
   * $$\mathrm{T^2\_2} = \mathrm{T^1\_Y}$$ これは $$\mathrm{T\_Y}$$；展開可能なら、それは展開されます
5. トークン $$\mathrm{T^1\_Y}$$が $$\mathrm{T\_Y}$$の展開から得られる最初のトークンであり $$\mathrm{{T^A\_{Y1}}{T^B\_{Y1}}{T^C\_{Y1}}}$$ が $$\mathrm{T\_X}$$、TeX によって再処理される結果のトークン列は次のようになります: $$\mathrm{{T\_X}{T^A\_{Y1}}{T^B\_{Y1}}{T^C\_{Y1}}{T^2\_Y}{T^3\_Y}\cdots{T^N\_Y}}$$

   これは次のように言い換えられます

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

次の図は、TeX コードによって作成されるトークンリストを示しています

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

![TeX が複数の \expandafter コマンドを処理している様子を示す画像](/files/b28d20a04bf11c99d9fdbd249f2e059e0fe1ce5b)

### 理論を実践へ

例として、次のマクロを、 $$\mathrm{T\_X}$$ と $$\mathrm{T\_Y}$$

* $$\mathrm{T\_X}=$$`\foo` ここで `\foo` を次のように定義します `\def\foo#1{\textbf{#1}}`
* $$\mathrm{T\_Y}=$$`\bar` まず次のように定義します `\def\abc{Hello}`, `\def\xyz{, World!}` その後 `\def\bar{\abc\xyz}`

先ほどの分析を示すために、次のコード断片を使います:

\expandafter\expandafter\expandafter\foo\bar

これまでの議論から、 `\expandafter<sub>1</sub>\expandafter<sub>2</sub>\expandafter<sub>3</sub>` $$\mathrm{T\_X}\mathrm{T\_Y}$$ 次のような形式のトークン列を生成します:

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

ここで、正確な列はトークンの性質によって異なります $$\mathrm{T\_Y}$$。例のコマンドを $$\mathrm{T\_X}=$$`\foo` と $$\mathrm{T\_Y}=$$`\bar`、これは次のように定義されています `\def\bar{\abc\xyz}`を使って挿入すると、次のようになります:

* の最初のトークンは $$\mathrm{T\_Y}$$ は `\abc` そして、その展開は文字トークンの列です: `Hello`
* に残っているトークンは $$\mathrm{T\_Y}$$ は、 `\xyz`.

この情報を「分析」に当てはめると、次のようになります

$$\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\*}$$

添字に注意してください <sub>トークン</sub> と <sub>トークンリスト（文字）</sub> は、TeX が *整数トークン値*ではなく *テキスト文字*を読み取っていることを強調（思い出させる）ために使われています。したがって、 `\foo`の後に空白文字やその他の区切り文字を示す必要はありません：そのような区切り文字はとうの昔に処理されるか破棄されており、ここでは TeX の内部世界であるトークンリストと整数トークン値の中にしっかり入っています。

TeX が、私たちの `\expandafter` コマンドによって生成されたトークンリスト列を処理すると `**H**ello, World!`—ただし `H` だけが太字で組版されます。同等の TeX コードを書くことで、同じ結果を得ることもできます `\foo Hello\xyz`。\foo の定義に注目してください。 `\foo` では単一のパラメータを使っていました。したがって、 `\foo` 単一の `H` 引数としての文字トークンを吸収し、残りの文字トークン（`ello`）はそのまま残ります。

**注記:**

* 書くと `\foo\bar` は、まったく異なる出力になります： `\bar` のトークンが `\foo` の引数として使われます **`Hello, World`**—すべてが太字で組版されます。
* 書くと `\expandafter\foo\bar` により `\bar` が展開され、2つのトークンが生成されます: $$\text{abc}*\text{token}\text{xyz}*\text{token}$$。その後、 $$\text{foo}*\text{token}$$ が `\expandafter` TeX がトークン列 $$\text{foo}*\text{token}\text{abc}*\text{token}\text{xyz}*\text{token}$$ を組版し `**Hello**, World`—ただし **`Hello`** が太字で組版されます。ここでは、単一のトークン $$\text{abc}*\text{token}$$ がマクロトークンの引数として処理されます $$\text{foo}*\text{token}$$、トークン $$\text{xyz}\_\text{token}$$ を残して、その内容は現在のフォントで組版されます。

## \expandafter と引数を持つマクロに関する注意

を使うときは `\expandafter` マクロの展開を強制するために使う場合は、マクロ展開の仕組みを知っておく価値があります—特に引数を取るマクロでは重要です。TeX がマクロを実行できるようにするには—つまり、マクロ定義の中に含まれるトークンを読み取り、処理できるようにするには—まず初期のマクロ展開処理を行って、そのマクロを「実行準備完了」にする必要があります。マクロの定義にパラメータ（`#1`, `#2`, ... `#9`）が含まれている場合、マクロ展開処理の一部として、TeX は入力を走査して、ユーザーが与えた引数を構成するトークンを探す必要があります。これらの引数トークンは入力から吸収（削除）されます。マクロ展開の間、TeX は入力からトークンを読み取り吸収して、小さなトークンリストを作成します。引数ごとに 1 つのリストが作られ、それらのトークンリストは後でマクロ本体の適切な位置に挿入されます—TeX がそれを実行するときに。マクロ展開の最後の段階では、TeX はメモリに保存されているマクロ定義を見つけ、その場所を TeX が次に入力トークンを読み取る元として機能するようにします。マクロの実行は、TeX がそれらのトークンを読み取り処理し始め、前もって作成しておいた引数保存用のトークンリストを流し込むときに始まります。

### 区切りトークンも吸収されます

元のマクロ定義でも区切りとして働くトークンが使われている場合、TeX は元のマクロ定義とユーザーによるそのマクロの使用（呼び出し）を比較して、区切りトークンを見つけて照合する必要もあります。一致して見つかると、区切りトークンはその後無視されます。なぜなら、その唯一の目的は「句読点」として働き、TeX が各引数を構成する実際のトークンを見つけ出して識別するのを助けることだからです。

&#x20;[第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)


---

# 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/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.
