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

# 索引

在一些大型文档中，例如书籍，通常会制作一个包含主要术语的字母顺序列表。借助 LaTeX 和辅助程序 **imakeidx**，索引可以相当容易地生成。

## 引言

让我们看看一个简单的工作示例。

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

\begin{document}
\section{引言}
在这个例子中，将使用若干关键词\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{引言}
在这个例子中，将使用若干关键词\index{keywords}
它们很重要，值得出现在索引\index{Index}中。

诸如 generate\index{generate} 和 some\index{others} 这样的术语也会
出现。索引中的术语也可以嵌套 \index{Index!nested}

\clearpage

\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{引言}
在这个例子中，将使用若干关键词\index{keywords}，它们
很重要，值得出现在索引\index{Index}中。

诸如 generate\index{generate} 和 some\index{others} 这样的术语也会
出现。索引中的术语也可以嵌套 \index{Index!nested}

\clearpage

\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`被改为“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{引言}
在这个例子中，将使用若干关键词\index{keywords}，它们很重要，值得出现在索引\index{Index}中。

诸如 generate\index{generate}、great\index{great} 列表以及一些其他\index{others} 术语，它们可能很重要\index{important}
也会出现。索引中的术语也可以嵌套 \index{Index!nested}

\clearpage

\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{引言}
在这个例子中，将使用若干关键词\index{keywords}，它们将
被使用，它们很重要，值得出现在
索引\index{Index}中。

诸如 generate\index{generate} 和 some\index{others}
也会出现。索引中的术语也可以
嵌套 \index{Index!nested}

\clearpage

\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 命令的参数

**title**

在特定索引开头排版的标题。索引格式化中给出了一个示例。

**intoc**

如果传递此选项，索引标题将放入目录中。

**columns**

使用 key=value 语法，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" 插入到折行行首的空白（默认：两个制表符）。                                                                 |
| `indent_length <number>`    | 16 indent\_space 的长度（默认：16，相当于 2 个制表符）。                                                      |

## 进一步阅读

更多信息请参见：

* [术语表](/latex/zh-cn/wen-dang-jie-gou/05-glossaries.md)
* [表格与图形列表](/latex/zh-cn/tu-biao-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://overleaf-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.
