> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/wen-dang-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/72d84c392e2709d2f4d21fec2e6114b30353158b)

首先，通过以下行将该宏包包含到导言区中

```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/1f71c333ce31f9afd66fa67349a7bf0cfee98447)

在索引中扩展术语以包含特殊形容词是常见做法。例如，单词 *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/cc51cec4bd976db1d2de1dc5c68d897c72cf3f69)

在这个示例中，索引以三栏格式显示，并使用参数 `columns=3`，并通过 `title=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}、a great\index{great} 列表和 som other\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/fa81bc85aaabb551eb24099a09d9288fc89c8c08)

## 将索引包含到目录中

默认情况下，索引不包含在目录中，这可以很容易地调整。

```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/9666d475ef7ced2811b7e216c9c7be7e9d7adbd9)

通过添加参数 `intoc` 到命令 `\makeindex` 即可将索引标题包含进目录。

## 参考指南

### \makeindex 命令的参数

**标题**

要在特定索引开头排版的标题。索引格式化部分给出了示例。

**intoc**

如果启用此选项，索引标题将被放入目录中。

**列数**

使用 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 " 插入在两个一级（level 0）条目之间的命令。                                                        |
| `item_1 <string>`           | "\n \\\subitem " 插入在两个二级（level 1）条目之间的命令。                                                     |
| `item_2 <string>`           | "\n \\\subsubitem " 插入在两个二级（level 2）条目之间的命令。                                                  |
| `item_01 <string>`          | "\n \\\subitem " 插入在一个 level 0 条目和一个 level 1 条目之间的命令。                                         |
| `item_x1 <string>`          | "\n \\\subitem " 插入在一个 level 0 条目和一个 level 1 条目之间的命令，其中 level 0 条目没有关联的页码。                    |
| `item_12 <string>`          | "\n \\\subsubitem " 插入在一个 level 1 条目和一个 level 2 条目之间的命令。                                      |
| `item_x2 <string>`          | "\n \\\subsubitem " 插入在一个 level 1 条目和一个 level 2 条目之间的命令，其中 level 1 条目没有关联的页码。                 |
| `delim_0 <string>`          | ", " 插入在 level 0 键及其第一个页码之间的分隔符（默认：逗号后跟一个空格）。                                                 |
| `delim_1 <string>`          | ", " 插入在 level 1 键及其第一个页码之间的分隔符（默认：逗号后跟一个空格）。                                                 |
| `delim_2 <string>`          | ", " 插入在 level 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" 插入在换行行首的空白（默认：两个制表符）。                                                                  |
| `indent_length <number>`    | 16 indent\_space 的长度（默认：16，相当于 2 个制表符）。                                                       |

## 延伸阅读

更多信息请参见：

* [术语表](/latex/zh-cn/wen-dang-jie-gou/05-glossaries.md)
* [表格和图形列表](/latex/zh-cn/tu-xing-he-biao-ge/03-lists-of-tables-and-figures.md)
* [目录](/latex/zh-cn/wen-dang-jie-gou/02-table-of-contents.md)
* [章节与章](/latex/zh-cn/wen-dang-jie-gou/01-sections-and-chapters.md)
* [超链接](/latex/zh-cn/wen-dang-jie-gou/09-hyperlinks.md)
* [国际语言支持](/latex/zh-cn/yu-yan/03-international-language-support.md)
* [章节和公式的交叉引用](/latex/zh-cn/wen-dang-jie-gou/03-cross-referencing-sections-equations-and-floats.md)
* [大型项目中的管理](/latex/zh-cn/wen-dang-jie-gou/07-management-in-a-large-project.md)
* [多文件 LaTeX 项目](/latex/zh-cn/wen-dang-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/wen-dang-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.
