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

# \expandafter는 어떻게 작동하나요: 연속된 \expandafter 명령에 대한 상세 연구

&#x20;[1부](/latex/ko/in-depth-articles/19-how-does-expandafter-work-an-introduction-to-tex-tokens.md)   [2부](/latex/ko/in-depth-articles/22-how-does-expandafter-work-the-meaning-of-expansion.md)   [3부](/latex/ko/in-depth-articles/21-how-does-expandafter-work-tex-uses-temporary-token-lists.md)   [4부](/latex/ko/in-depth-articles/20-how-does-expandafter-work-from-basic-principles-to-exploring-tex-s-source-code.md)   [5부](/latex/ko/in-depth-articles/17-how-does-expandafter-work-a-detailed-macro-case-study.md)   [6부](/latex/ko/in-depth-articles/18-how-does-expandafter-work-a-detailed-study-of-consecutive-expandafter-commands.md)&#x20;

## 사례 연구: \expandafter\expandafter\expandafter... 이해하기

LaTeX 패키지에서 볼 수 있는 것과 같은 고급 매크로는 종종 여러 개의 연속된 `\\expandafter` 명령을 광범위하게 사용하여 정교한 토큰 “저글링”을 수행한다. 우리 대부분에게 이런 매크로는 이해하기 어렵거나 작성하기 부담스러울 수 있다. 이 절에서는 연속된 `\\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}$$. 또한 두 개의 토큰 $$\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/ko/in-depth-articles/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는 *두 번째* 호출을 `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}}}$$ 로 확장되고, 그러면 TeX가 $$\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
```

![여러 \expandafter 명령을 처리하는 TeX를 보여주는 이미지](/files/092317ac6011e916d1f4ed67226445b04078c9f5)

### 이론을 실전으로

예를 들어, 우리는 다음 매크로들을 다음의 역할을 하도록 정의하겠다 $$\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` 단일 매개변수를 사용했다는 점에 주목하라; 따라서 `H` 는 인수로 사용될 단일`ello`를 남겨 둔 채 나머지 문자 토큰들은 건드리지 않는다.

**참고:**

* \foo\bar를 `작성하면` 매우 다른 출력을 낸다: `\bar` 에 대한 인수로 사용될 토큰은 `\foo` 되며, 그 결과 다음이 조판된다 **`Hello, World`**—모든 것이 굵게 조판된다.
* \foo\bar를 `\expandafter\foo\bar` 는 `\bar` 를 확장하게 하고, 그 결과 두 개의 토큰이 생성된다: $$\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는 입력에서 토큰들을 읽고 흡수하여 미니 토큰 목록들을 만든다. 인수마다 하나의 목록이 만들어지며; 그런 토큰 목록들은 이후 TeX가 그것을 실행할 때 매크로 본문의 적절한 위치에 삽입된다. 매크로 확장의 마지막 단계에서는 TeX가 메모리에 저장된 매크로 정의를 찾아 그 위치가 TeX가 다음 입력 토큰 집합을 읽게 될 स्रोत가 되도록 배열한다. 매크로 실행은 TeX가 그 토큰들을 읽고 처리하기 시작할 때 시작되며, 이전에 인수를 저장하기 위해 생성한 토큰 목록들을 입력으로 공급한다.

### 구분자 토큰도 또한 흡수된다

원래의 매크로 정의가 구분자 역할을 하는 토큰들도 사용했다면, TeX는 원래의 매크로 정의와 사용자가 그 매크로를 사용하는 방식(호출)도 비교하여 구분자 토큰을 찾아 일치시켜야 한다. 일단 일치/위치가 확인되면, 구분자 토큰은 이후 무시되는데, 그 유일한 목적이 TeX가 각 인수를 형성할 실제 토큰들을 골라내고 식별하는 데 도움을 주는 “구두점” 역할을 하는 것이기 때문이다.

&#x20;[1부](/latex/ko/in-depth-articles/19-how-does-expandafter-work-an-introduction-to-tex-tokens.md)   [2부](/latex/ko/in-depth-articles/22-how-does-expandafter-work-the-meaning-of-expansion.md)   [3부](/latex/ko/in-depth-articles/21-how-does-expandafter-work-tex-uses-temporary-token-lists.md)   [4부](/latex/ko/in-depth-articles/20-how-does-expandafter-work-from-basic-principles-to-exploring-tex-s-source-code.md)   [5부](/latex/ko/in-depth-articles/17-how-does-expandafter-work-a-detailed-macro-case-study.md)   [6부](/latex/ko/in-depth-articles/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/ko/in-depth-articles/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.
