> 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/wen-jian-jie-gou/04-indices.md).

# 索引

在一些大型文件中，例如書籍，通常會製作一份包含主要術語的字母索引。使用 LaTeX 以及輔助程式 **imakeidx**，就可以相當容易地產生索引。

## 簡介

讓我們看一個簡單的可運作範例。

```latex
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}
\makeindex

\begin{document}
\section{Introduction}
在此範例中，將使用幾個重要的關鍵字\index{keywords}
，它們很重要，值得出現在索引\index{Index}中。

像 generate\index{generate} 和 some\index{others} 之類的詞彙
也會出現。

\printindex
\end{document}
```

[在 Overleaf 中開啟此範例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Index+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5BT1%5D%7Bfontenc%7D%0A%5Cusepackage%7Bimakeidx%7D%0A%5Cmakeindex%0A%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BIntroduction%7D%0AIn+this+example%2C+several+keywords%5Cindex%7Bkeywords%7D+will+be+used+%0Awhich+are+important+and+deserve+to+appear+in+the+Index%5Cindex%7BIndex%7D.%0A%0ATerms+like+generate%5Cindex%7Bgenerate%7D+and+some%5Cindex%7Bothers%7D+will+%0Aalso+show+up.+%0A%0A%5Cprintindex%0A%5Cend%7Bdocument%7D)

下圖顯示了上述範例產生的輸出：

![Index1OLV2.png](/files/bb1ee6adc10ce993f4e4df5ca6f1dc974d2de7ce)

首先，透過這一行將套件加入前言區：

```latex
\usepackage{imakeidx}
```

接著，指令 `\makeindex` 對索引正常運作是必要的，並且可以接受一些參數來自訂其外觀；在後續章節中這一點會更清楚。

若要將條目加入索引，可使用指令 `\index{}` ，其中要加入的單字會作為參數插入。請注意，這不會在目前位置輸出該單字，只會出現在索引中。

最後，指令 `\printindex` 才會實際渲染索引。如果你使用透過 `babel` 套件進行的特殊本地化，標題也會相應翻譯。

注意：除了 `imakeidx`之外，還可以匯入套件 `makeidx` ，但它提供的自訂選項較少。

## Overleaf 上的索引

在 Overleaf 專案中加入索引時， **需要特別注意** ，為了讓索引能正確編譯，主 `.tex` 檔案必須位於專案的根目錄中， *不能放在任何資料夾內*。這可確保產生索引所需的輔助檔案以編譯器可存取的方式快取。

## 條目與子條目

如上一節所述，指令 `\index` 會將傳入的單字加入索引，但這並不是它唯一的用途。

```latex
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}
\makeindex

\begin{document}
\section{Introduction}
在此範例中，將使用幾個重要的關鍵字\index{keywords}
，它們很重要，值得出現在索引\index{Index}中。

像 generate\index{generate} 和 some\index{others} 之類的詞彙也會
出現。索引中的條目也可以巢狀化 \index{Index!nested}

\clearpage

\section{Second section}
這個第二節\index{section} 可能包含一些特殊字詞，
並擴充已經使用過的\index{keywords!used}。

\printindex
\end{document}
```

[在 Overleaf 中開啟此範例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Index+example+2\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5BT1%5D%7Bfontenc%7D%0A%5Cusepackage%7Bimakeidx%7D%0A%5Cmakeindex%0A%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BIntroduction%7D%0AIn+this+example%2C+several+keywords%5Cindex%7Bkeywords%7D+will+be+used+%0Awhich+are+important+and+deserve+to+appear+in+the+Index%5Cindex%7BIndex%7D.%0A%0ATerms+like+generate%5Cindex%7Bgenerate%7D+and+some%5Cindex%7Bothers%7D+will+also+%0Ashow+up.+Terms+in+the+index+can+also+be+nested+%5Cindex%7BIndex%21nested%7D%0A%0A%5Cclearpage%0A%0A%5Csection%7BSecond+section%7D%0AThis+second+section%5Cindex%7Bsection%7D+may+include+some+special+word%2C+%0Aand+expand+the+ones+already+used%5Cindex%7Bkeywords%21used%7D.%0A%0A%5Cprintindex%0A%5Cend%7Bdocument%7D)

下圖顯示了上述範例產生的輸出：

![Index2OLV2.png](/files/b1dccb9c93ec0ef8f268893eb4eac54ecc8120d8)

一種常見做法是擴充索引中的術語，以包含特殊形容詞。例如，單字 *Field* 若前面加上形容詞 *finite* 或後面接著 *of characteristic 0*，它就可能具有特殊意義，而把不同類型的 *Field*作為索引中的獨立條目加入，會有點繁瑣。對於這種情況，你可以加上一個驚嘆號「!」，將這個符號之後的詞作為主要單字的子條目加入 *Field*.

在此範例中，單字「Index」將「nested」作為子條目。

## 索引格式設定

索引的簡單格式設定，例如變更索引標題、增加多欄以及變更欄寬，都可以透過將可選值傳給 `\makeindex`:

```latex
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}
\makeindex[columns=3, title=Alphabetical Index]

\begin{document}
\section{Introduction}
在此範例中，幾個關鍵字\index{keywords}將會被
使用，而它們很重要，值得出現在索引\index{Index}中。

像 generate\index{generate} 和 some\index{others} 之類的詞彙也會
出現。索引中的條目也可以巢狀化 \index{Index!nested}

\clearpage

\section{Second section}
這個第二節\index{section} 可能包含一些特殊字詞，
並擴充已經使用過的\index{keywords!used}。

\printindex
\end{document}
```

[在 Overleaf 中開啟此範例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Index+example+3\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5BT1%5D%7Bfontenc%7D%0A%5Cusepackage%7Bimakeidx%7D%0A%5Cmakeindex%5Bcolumns%3D3%2C+title%3DAlphabetical+Index%5D%0A%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BIntroduction%7D%0AIn+this+example%2C+several+keywords%5Cindex%7Bkeywords%7D+will+be+used+which+%0Aare+important+and+deserve+to+appear+in+the+Index%5Cindex%7BIndex%7D.%0A%0ATerms+like+generate%5Cindex%7Bgenerate%7D+and+some%5Cindex%7Bothers%7D+will+also+%0Ashow+up.+Terms+in+the+index+can+also+be+nested+%5Cindex%7BIndex%21nested%7D%0A%0A%5Cclearpage%0A%0A%5Csection%7BSecond+section%7D%0AThis+second+section%5Cindex%7Bsection%7D+may+include+some+special+word%2C+%0Aand+expand+the+ones+already+used%5Cindex%7Bkeywords%21used%7D.%0A%0A%5Cprintindex%0A%5Cend%7Bdocument%7D)

下圖顯示了上述範例產生的輸出：

![Index3OLV2.png](/files/ee8596682761c20cc2d91bf52708c61498db033e)

在此範例中，索引以三欄格式呈現，並使用參數 `columns=3`，且標題透過 `title=Alphabetical Index`變更為「Alphabetical Index」；這些參數會傳入 `\makeindex` 前言區中的 [參考指南](#reference-guide) ，以查看可用參數及其說明清單。

### 使用樣式檔案

若要進一步自訂索引，可以使用樣式檔案。樣式檔案會告知 `makeindex` 最終輸出的預期格式。樣式檔案包含一系列 `<key, value>` 配對。例如，如果需要依索引術語的首字母分組，並將頁碼靠右對齊，下面這個樣式檔案就能勝任：

```latex
headings_flag 1

heading_prefix "\n\\centering\\large\\sffamily\\bfseries%
\\noindent\\textbf{"heading_suffix "}\\par\\nopagebreak\n"

item_0 "\n \\item \\small "

delim_0 " \\hfill "
delim_1 " \\hfill "
delim_2 " \\hfill "
```

各個鍵會改變索引輸出的不同元素：

* `headings_flag 1` 啟用分組，並在新群組（符號、數字或字母）之前插入群組標頭。
* `heading_prefix` 會格式化要在新字母開始之前插入的標頭。它使用一般的字型族、字體大小與對齊格式指令，但反斜線必須以雙反斜線而非單反斜線進行跳脫。
* `item_0` 是要插入在兩個主要項目之間的內容。
* `delim_*` 是要插入在鍵與第一個頁碼之間的分隔符號。

若要查看 .ist 檔中可使用的鍵和值完整清單，請參見 [參考指南中所附的表格](#keys-to-create-styles-files).

假設前一個檔案儲存為「example\_style.ist」。若要在文件中使用它，我們加入參數 `options= -s example_style.ist` 到 `\makeindex` 指令中。

```latex
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}
\makeindex[columns=3, title=Alphabetical Index,
           options= -s example_style.ist]

\begin{document}

\tableofcontents

\section{Introduction}
在此範例中，幾個重要的關鍵字\index{keywords}將會被使用，而它們很重要，值得出現在索引\index{Index}中。

像 generate\index{generate}、一個很棒的\index{great} 清單，以及一些可能很重要的\index{others} 其他\index{important}術語，也會
出現。索引中的條目也可以巢狀化 \index{Index!nested}

\clearpage

\section{Second section}
這個第二節\index{section} 可能包含一些特殊字詞，並擴充已經使用過的\index{keywords!used}。

\printindex
\end{document}
```

[在 Overleaf 中開啟使用索引樣式檔案的範例](https://www.overleaf.com/project/new/template/20614?id=70756423\&templateName=Using+an+index+style+file\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

下圖顯示了上述範例產生的輸出：

![Index4OLV2.png](/files/029a06952a7862b613ebf79e76bc70d81cebef0d)

## 將索引納入目錄

預設情況下，索引不會包含在目錄中，這可以很容易地調整。

```latex
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}
\makeindex[columns=3, title=Alphabetical Index, intoc]

\begin{document}
\tableofcontents

\section{Introduction}
在此範例中，幾個關鍵字\index{keywords}將會被
使用，而它們很重要，值得出現在
索引\index{Index}中。

像 generate\index{generate} 和 some\index{others}
也會出現。索引中的條目也可以
巢狀化 \index{Index!nested}

\clearpage

\section{Second section}
這個第二節\index{section} 可能包含一些特殊
字詞，並擴充已經使用過的\index{keywords!used}。

\printindex
\end{document}
```

[在 Overleaf 中開啟此範例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Index+example+3\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5BT1%5D%7Bfontenc%7D%0A%5Cusepackage%7Bimakeidx%7D%0A%5Cmakeindex%5Bcolumns%3D3%2C+title%3DAlphabetical+Index%2C+intoc%5D%0A%0A%5Cbegin%7Bdocument%7D%0A%5Ctableofcontents%0A%0A%5Csection%7BIntroduction%7D%0AIn+this+example%2C+several+keywords%5Cindex%7Bkeywords%7D+will+be+%0Aused+which+are+important+and+deserve+to+appear+in+the+%0AIndex%5Cindex%7BIndex%7D.%0A%0ATerms+like+generate%5Cindex%7Bgenerate%7D+and+some%5Cindex%7Bothers%7D+%0Awill+also+show+up.+Terms+in+the+index+can+also+be+%0Anested+%5Cindex%7BIndex%21nested%7D%0A%0A%5Cclearpage%0A%0A%5Csection%7BSecond+section%7D%0AThis+second+section%5Cindex%7Bsection%7D+may+include+some+special+%0Aword%2C+and+expand+the+ones+already+used%5Cindex%7Bkeywords%21used%7D.%0A%0A%5Cprintindex%0A%5Cend%7Bdocument%7D)

下圖顯示了上述範例產生的輸出：

![Index5OLV2.png](/files/f2a2d00bdc5f36e3565a9a9ec49e38d97ce8132c)

透過加入參數 `intoc` 到指令 `\makeindex` ，即可啟用將索引標題納入目錄。

## 參考指南

### \makeindex 指令的參數

**title**

要在特定索引開頭排版的標題。索引格式設定一節有範例。

**intoc**

如果傳入此選項，索引標題就會放入目錄中。

**columns**

使用 key=value 語法，值必須是代表欄數的整數。預設值是 2。

**columnsep**

指定代表欄與欄之間間距的單位。語法必須是 key=value，例如 columnsep=2em。

**columnseprule**

如果傳入此選項，欄與欄之間將會顯示一條垂直分隔線。

### 建立樣式檔案的鍵

下表顯示用來建立 .ist 樣式檔案的鍵、預設值與說明。

| 鍵                           | 說明                                                                                           |
| --------------------------- | -------------------------------------------------------------------------------------------- |
| `actual <char>`             | '@' 表示下一個條目將出現在輸出檔案中的符號。                                                                     |
| `arg_close <char>`          | '}' 關閉分隔符號，用於索引條目參數。                                                                         |
| `arg_open <char>`           | '{' 開啟分隔符號，用於索引條目參數。                                                                         |
| `encap <char>`              | ' 表示其餘參數清單將作為頁碼的包裝指令的符號。                                                                     |
| `escape <char>`             | '\\\\' 可跳脫後續字母的符號，除非其前導符號本身也是跳脫符號。注意：quote 用來跳脫緊接在其後的字母，但如果它前面有一個跳脫符號，則會被視為普通字元。這兩個符號必須彼此不同。 |
| `keyword <string>`          | "\\\indexentry" 命令，用來告訴 makeindex 其參數是一個索引條目。                                                |
| `level <char>`              | '!' 表示子項目新層級的分隔符號。                                                                           |
| `quote <char>`              | "' 注意：quote 用來跳脫緊接在其後的字母，但如果它前面有一個跳脫符號，則會被視為普通字元。這兩個符號必須彼此不同。                                |
| `range_close <char>`        | ')' 表示明確頁碼範圍結束的關閉分隔符號。                                                                       |
| `range_open <char>`         | '(' 表示明確頁碼範圍開始的開啟分隔符號。                                                                       |
| `preamble <string>`         | "\\\begin{theindex}\n" 輸出檔案的前置部分。                                                            |
| `postamble <string>`        | "\n\n\\\end{theindex}\n" 輸出檔案的後置部分。                                                          |
| `setpage_prefix <string>`   | "\n \\\setcounter{page}{" 設定起始頁碼的指令之前綴。                                                      |
| `setpage_suffix <string>`   | "}\n" 設定起始頁碼的指令之後綴。                                                                          |
| `group_skip <string>`       | "\n\n \\\indexspace\n" 新群組開始前要插入的垂直空白。                                                       |
| `headings_flag <string>`    | 0 表示如何處理新群組標頭的旗標，這些標頭會插入在新群組（符號、數字與 26 個字母）之前：正值會在前綴與後綴之間插入大寫字母，負值則插入小寫字母（預設為 0，表示不產生標頭）。    |
| `heading_prefix <string>`   | "" 要在新字母開始之前插入的標頭前綴。                                                                         |
| `symhead_positive <string>` | "Symbols" 在 headings\_flag 為正時要插入的符號標頭。                                                      |
| `symhead_negative <string>` | "symbols" 在 headings\_flag 為負時要插入的符號標頭。                                                      |
| `numhead_positive <string>` | "Numbers" 在 headings\_flag 為正時要插入的數字標頭。                                                      |
| `numhead_negative <string>` | "numbers" 在 headings\_flag 為負時要插入的數字標頭。                                                      |
| `item_0 <string>`           | "\n \\\item " 要插入在兩個主要（第 0 級）項目之間的指令。                                                        |
| `item_1 <string>`           | "\n \\\subitem " 要插入在兩個次級（第 1 級）項目之間的指令。                                                     |
| `item_2 <string>`           | "\n \\\subsubitem " 要插入在兩個第 2 級項目之間的指令。                                                      |
| `item_01 <string>`          | "\n \\\subitem " 要插入在一個第 0 級項目與一個第 1 級項目之間的指令。                                               |
| `item_x1 <string>`          | "\n \\\subitem " 要插入在一個第 0 級項目與一個第 1 級項目之間的指令，其中第 0 級項目沒有相關頁碼。                               |
| `item_12 <string>`          | "\n \\\subsubitem " 要插入在一個第 1 級項目與一個第 2 級項目之間的指令。                                            |
| `item_x2 <string>`          | "\n \\\subsubitem " 要插入在一個第 1 級項目與一個第 2 級項目之間的指令，其中第 1 級項目沒有相關頁碼。                            |
| `delim_0 <string>`          | ", " 要插入在第 0 級鍵與其第一個頁碼之間的分隔符號（預設：逗號後接一個空白）。                                                  |
| `delim_1 <string>`          | ", " 要插入在第 1 級鍵與其第一個頁碼之間的分隔符號（預設：逗號後接一個空白）。                                                  |
| `delim_2 <string>`          | ", " 要插入在第 2 級鍵與其第一個頁碼之間的分隔符號（預設：逗號後接一個空白）。                                                  |
| `delim_n <string>`          | ", " 要插入在任何層級中同一鍵的兩個頁碼之間的分隔符號（預設：逗號後接一個空白）。                                                  |
| `delim_r <string>`          | "--" 要插入在範圍起始與結束頁碼之間的分隔符號。                                                                   |
| `delim_t <string>`          | "" 要插入在頁碼清單結尾的分隔符號。此分隔符號不會影響沒有相關頁碼清單的條目。                                                     |
| `encap_prefix <string>`     | "\\\\" 用於包裝頁碼的指令之包裝前綴的第一部分。                                                                  |
| `encap_infix <string>`      | "{" 用於包裝頁碼的指令之包裝前綴的第二部分。                                                                     |
| `encap_suffix <string>`     | "}" 用於包裝頁碼的指令之後綴。                                                                            |
| `line_max <number>`         | 72 輸出中一行的最大長度，超過此長度的行將會換行。                                                                   |
| `indent_space <string>`     | "\t\t" 要插入在換行後一行前方的空白（預設：兩個 tab）。                                                            |
| `indent_length <number>`    | 16 indent\_space 的長度（預設：16，相當於 2 個 tab）。                                                     |

## 延伸閱讀

更多資訊請參見：

* [詞彙表](/latex/zh-tw/wen-jian-jie-gou/05-glossaries.md)
* [表格與圖形清單](/latex/zh-tw/tu-biao-yu-biao-ge/03-lists-of-tables-and-figures.md)
* [目錄](/latex/zh-tw/wen-jian-jie-gou/02-table-of-contents.md)
* [節與章](/latex/zh-tw/wen-jian-jie-gou/01-sections-and-chapters.md)
* [超連結](/latex/zh-tw/wen-jian-jie-gou/09-hyperlinks.md)
* [國際語言支援](/latex/zh-tw/yu-yan/03-international-language-support.md)
* [章節與方程式的交叉引用](/latex/zh-tw/wen-jian-jie-gou/03-cross-referencing-sections-equations-and-floats.md)
* [大型專案中的管理](/latex/zh-tw/wen-jian-jie-gou/07-management-in-a-large-project.md)
* [多檔案 LaTeX 專案](/latex/zh-tw/wen-jian-jie-gou/08-multi-file-latex-projects.md)
* [imakeidx 套件文件](http://mirror.math.ku.edu/tex-archive/macros/latex/contrib/imakeidx/imakeidx.pdf)


---

# 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/wen-jian-jie-gou/04-indices.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.
