> 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/21-how-does-expandafter-work-tex-uses-temporary-token-lists.md).

# \expandafter는 어떻게 작동하나요: TeX는 임시 토큰 목록을 사용합니다

[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)

## 확장과 내부 토큰 목록

지금까지 우리는 토큰, 토큰 목록, 그리고 TeX의 확장 개념 뒤에 있는 핵심 원리를 살펴보았습니다. 이 섹션에서는 TeX 원시 명령인 `\jobname` 을 사용해 확장 처리의 중요한 측면, 즉 TeX가 사용하는 *임시 토큰 목록*—이는 *근본적인* 의 한 측면입니다. `\\expandafter` 가 작동하는 방식은 이 글 연재의 후반부에서 살펴보겠습니다.

`\jobname` 는 확장 가능한 TeX 원시 명령으로, 그 확장은 주 입력 파일의 이름을 나타내는 일련의 문자 토큰을 생성합니다 `.tex` 파일입니다. 예를 들어, 다음 텍스트가 어떤 `.tex` 라는 파일이 생성됩니다 `mycode.tex`:

```
    내 파일의 이름은 \jobname .tex % \jobname 뒤의 공백에 주의
```

이는 다음과 같이 조판됩니다

```
    내 파일의 이름은 mycode.tex입니다
```

우리가 `\jobname` 명령을 사용하면 결과로 조판되는 문자들은 실제 `.tex` 파일에서 읽히는 것이 아닙니다. 그렇다면 그것들은 어디서 오는 것일까요? TeX는 그 토큰들을 어디에 저장하고 어디서 읽을까요? 사용자에게는 보이지 않게(즉, TeX 내부 깊숙한 곳에서) 다음의 확장 과정은 `\jobname` 파일 이름을 나타내는 문자 토큰들의 연속으로 구성된 임시 토큰 목록을 생성합니다. 일단 `\jobname` 가 그 토큰 목록을 만들면, TeX는 일시적으로 현재 입력 원천(여기서는 우리의 `.tex` 파일)에서 그 임시 토큰 목록의 토큰(문자 토큰)들을 읽는 쪽으로 “시선을 돌립니다”. TeX가 추가 입력 토큰을 필요로 하면 내부 목록에서 다음 토큰을 읽고, 목록의 끝에 도달할 때까지 이를 계속합니다. 그 시점에서 TeX는 이전 입력 원천으로 돌아가 토큰 읽기를 재개하는데, 여기서는 우리의 `.tex` 입력 파일에서 읽은 텍스트가 됩니다.

다음 도표에서 보이듯, TeX는 `.tex` 을 처리한 뒤 멈춘 정확한 위치에서 입력 파일 읽기를 다시 시작합니다 `\jobname`—공백 문자를 읽은 뒤이지만 “.” 문자를 읽기 전입니다. 실제로 마침표(.)는 TeX의 입력 버퍼에서 읽히기를 기다리고 있는데, 이는 입력 파일에서 읽은 텍스트 한 줄을 보관하도록 설계된 TeX 메모리의 작은 영역입니다 `.tex` 파일—TeX는 여러분의 `.tex` 파일을 한 줄씩 읽고 처리하며, 파일 전체를 메모리에 읽어들이지는 않습니다.

다음 그림을 볼 때는 아래에서 위로 읽으면서 처리 흐름을 따라가 보세요.

![TeX가 \jobname을 확장하는 방법](/files/01096982895ada548642ecadb840528efda6c3d0)

확장에 대한 앞선 논의로 돌아가 보면, 다음의 확장은 `\jobname` 결과적으로 `\jobname` 명령(토큰)이 *제거* 입력에서 *되고* 확장에서 생겨난 토큰들과 함께: 다음의 이름을 보관하기 위해 생성된 임시 토큰 목록 `.tex` 파일에 있습니다.

확장 가능한 명령(예: `\jobname`)만이 효과를 내기 위해 토큰 목록을 “몰래” 생성하고 사용하는 유일한 TeX 원시 명령은 아닙니다. 예를 들어, 다음 명령들은 `\uppercase` 및 `\lowercase` 둘 다 인자의 대소문자를 바꾸기 위해 내부 토큰 목록을 생성합니다. 대소문자 변경 작업이 끝나면 TeX는 그 명령들이 생성한 토큰 목록에서 문자 토큰을 읽도록 전환합니다. 토큰 목록은 물리적 디스크 파일에 데이터를 기록하는 것을 제외하면 TeX의 유일한 “토큰 데이터 저장” 메커니즘입니다.

### 토큰의 원천: TeX는 뛰어난 저글러입니다

TeX가 일반적인 문서를 처리할 때는 관리해야 합니다 *많은* 여러 토큰 원천을: 수많은 물리적 디스크 파일에서 들어오는 입력과 처리 중에 생성되는 셀 수 없이 많은 내부 토큰 목록들입니다. 이 섹션에서는 TeX가 이러한 입력 원천을 어떻게 “저글링”하는지 아주 간단히 살펴보겠습니다.

우리의 이름을 조판하는 간단한 매크로를 원한다고 가정해 봅시다 `.tex` 파일에 추가하세요:

```
    \def\myfile{내 파일의 이름은 \jobname .tex}
```

나중에, 우리 `.tex` 파일의 어느 시점에서 매크로 `\myfile`: 일시적으로 TeX는 텍스트(`.tex`) 파일에서 토큰을 읽는 방식으로 전환하여 `\myfile` 메모리에 저장된 정의(토큰 목록)에서 토큰을 읽게 됩니다. TeX가 `\myfile` 매크로를 실행할 때(그 토큰들을 처리할 때) 다음을 나타내는 토큰을 감지하게 됩니다 `\jobname` 명령인데, 그 확장은 TeX가 토큰을 읽어야 하는 또 하나의 임시 토큰 목록을 생성합니다. 이 간단한 경우에서도 TeX는 세 가지 입력 원천을 관리해야 합니다:

1. 다음 `.tex` 다음을 담고 있는 텍스트 파일 `\myfile` 매크로;
2. 다음의 정의를 저장하는 토큰 목록 `\myfile` 매크로;
3. 다음에 의해 생성된 토큰 목록 `\jobname` 명령이 포함된 `\myfile` 매크로.

TeX가 문서를 처리하는 동안 물리적 파일과 토큰 목록 같은 입력 원천 사이를 끊임없이 전환하는데, 그렇다면 TeX는 이를 어떻게 추적할까요? 답은 내부적으로 TeX 엔진이 이른바 [입력 스택](https://en.wikipedia.org/wiki/Stack_\(abstract_data_type\)) 을 유지하며, 이는 TeX가 입력 원천 사이를 오갈 때 자신이 무엇을 하고 있었는지(어디에서 읽고 있었는지)를 기억할 수 있게 해 주는 일종의 “기억” 역할을 합니다.

너무 자세히 들어가지 않으면, TeX 엔진 내부 코드는 다음과 같은 전역 변수를 사용합니다 `curinput` (현재 입력)은 여러 가지 중에서도 TeX가 현재 물리적 파일에서 읽고 있는지 토큰 목록에서 읽고 있는지를 알려줍니다. `curinput` 또한 TeX가 다음 토큰을 가져와야 하는 위치(현재 토큰 목록 또는 그 텍스트 버퍼 안의 위치)를 가리킵니다. TeX가 토큰 목록에서 읽고 있다면 `curinput` 또한 처리 중인 토큰 목록의 유형—예를 들어 매크로로 저장된 토큰 목록인지, 혹은 그 토큰들이 다른 원천에서 비롯되었는지—도 기록합니다.

필요할 때 `curinput` 변수는 새로운 입력 원천을 가리키도록 바뀌고, TeX의 현재 “입력 상태”(원천과 위치)는 입력 스택에 저장되어 나중에 TeX가 그 정확한 위치(의 위치) `.tex` 나 토큰 목록의 다음 토큰)으로 돌아갈 수 있게 됩니다. 새 입력 원천이 소진되면(예: 토큰 목록에 더 이상 토큰이 없거나 파일의 끝에 도달하면) 그것은 스택에서 제거되고 `curinput` TeX가 이전 원천에서 다시 토큰을 가져오도록 curinput이 갱신됩니다.

## 더 깊이 살펴보기(선택 읽기)

다음 섹션들은 세부 사항을 좋아하는 독자를 위해 추가 배경 정보를 제공합니다.

### 실제 토큰 목록

다음 그림은 TeX의 내부 데이터와 데이터 구조에 접근할 수 있게 해 주는 Overleaf의 Knuth TeX 사용자 정의 빌드로 생성되었습니다. 이 토큰 목록 그림은 앞서 제시한 단순화된 버전을 바탕으로 하며, 다음이 생성한 문자들을 보여 주는 것과 같은 추가 데이터도 포함합니다 `\jobname` 는 보통의 범주 코드 11이 아니라 범주 코드 12를 가집니다. 이 도표에서 “노드(node)”는 TeX가 사용하는 메모리 저장 단위에 붙인 이름일 뿐입니다.

![TeX 토큰 목록 내부](/files/3974dde87094cef814eca5a62b098d9c5cdf09e8)

### TeX가 \jobname을 읽고 처리하는 방법

또한 완결성을 위해, TeX가 다음을 감지할 때의 “사고 과정” 개요를 소개합니다 `\jobname` 우리 입력의 `.tex` 파일. 이 그림에서는 TeX가 이스케이프 문자(`\` 범주 코드 0) 를 감지하고, 문자 시퀀스를 처리하며 `jobname`, 토큰을 생성하고 다음의 의미를 찾아보는 과정을 볼 수 있습니다 `\jobname` 명령인데, 여기서 TeX는 그 명령 코드가 100보다 크다는 것을 확인하게 되며, 이는 그것이 확장 가능한 명령임을 나타냅니다.

![TeX가 \jobname을 탐색하고 처리하는 방법](/files/8aaf6d6160da226a74444abe17f66ae0bf9a69a5)

[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/21-how-does-expandafter-work-tex-uses-temporary-token-lists.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.
