LaTeX 索引

原  文:Indices
译  者:Xovee
翻译时间:2021年6月4日

索引 Indices

在大型的文档中,例如书籍等,通常会有一个按字母顺序排列的列表来列出文档中的主要术语。通过 LaTeX 和 imakeidx包,你可以轻松地在文档中创建索引表。

介绍

让我们先看一个简单的例子。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}
\makeindex

\begin{document}

\section{Introduction}
In this example, several keywords\index{keywords} will be used 
which are important and deserve to appear in the Index\index{Index}.

Terms like generate\index{generate} and some\index{others} will 
also show up. 

\printindex

\end{document}

在这里插入图片描述
首先,在文档的 preamble 中引入这个包:

\usepackage{imakeidx}

然后你需要使用\makeindex命令来创建索引。你可以给这个命令传入一些参数来改变索引的样式。

添加索引的命令是\index{},大括号中是索引条目的名称。需要注意的是,这个命令并不会在当前位置输出索引条目的名称,它只会在索引表中显示条目的名称。

最后,\printindex是实际输出索引表的命令。如果你使用了本地化的包,例如babel,那么索引表的标题也会随之变化。

注意:除了imakeidx,你也可以使用makeidx包,不过后者的自定义功能较少。

Overleaf 上的索引

如果你使用 Overleaf 来生成索引表,那么你需要将主.tex文件放置于项目的根目录下(在任何文件夹之外)。这可以保证一切用于生成索引的额外文件可以被编译器所访问。

索引条目

所之前所描述过的,\index命令会在索引表中添加条目。这并不是它唯一的使用方式。

\documentclass{article}
\usepackage{imakeidx}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\makeindex

\begin{document}

\section{Introduction}
In this example, several keywords\index{keywords} will be used 
which are important and deserve to appear in the Index\index{Index}.

Terms like generate\index{generate} and some\index{others} will also 
show up. Terms in the index can also be nested \index{Index!nested}

\clearpage

\section{Second section}
This second section\index{section} may include some special word, 
and expand the ones already used\index{keywords!used}.

\printindex
\end{document}

在这里插入图片描述

索引表的一种常见的现象是对条目进行扩展,从而可以包含额外的附属条目。例如,当单词Field之后跟了一个附属的finite或者跟了一个of characteristic 0,它可能会有特殊的含义。在类似的情况下,为Field添加一个新的条目会比较繁琐和不直观。你可以在术语之后添加一个感叹号(!)来对该术语新增子条目。

在这个例子中,术语index拥有一个名为nested的子条目。

索引的格式

简单的调整索引的格式,例如改变标题、新增索引列,以及改变列的宽度,可以简单地通过给\makeindex命令传递参数来实现:

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

\begin{document}

\section{Introduction}
In this example, several keywords\index{keywords} will be used which 
are important and deserve to appear in the Index\index{Index}.

Terms like generate\index{generate} and some\index{others} will also 
show up. Terms in the index can also be nested \index{Index!nested}

\clearpage

\section{Second section}
This second section\index{section} may include some special word, 
and expand the ones already used\index{keywords!used}.

\printindex
\end{document}

在这里插入图片描述
在这个例子中,索引表拥有三个列(参数为column=3),标题为"Alphabetical Index"(通过命令title=Alphabetical Index);这些参数传递给文档 preamble 中的\makeindex命令。文末列出了可用的参数及它们的解释。

使用格式文件

你可以使用格式文件来详细地自定义索引的格式。makeindex通过格式文件来确定索引具体的、期望的格式。格式文件中包含了格式为<key, value>的列表。例如,如果你希望对索引表中条目按照首字母进行分组,并且将页码向右对齐,那么下面的格式文件可以做到这一点:

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 "

其中的 keys 改变索引表中不同元素的样式,具体解释如下:

  • headings_flag = 1:激活分组,在新组之前插入组的标头(符号、数字或字母)
  • heading_prefix:定义要插入的标头。它使用标准的格式化命令,包括字体、字体大小和对齐。注意:“反斜杠”必须输入两个而不是一个。
  • item_0:是两个主要的项目之间需要输入的内容
  • delim_*:是 key 和首个页码之间的分隔符

.ist文件中完整的 keys 和 values 定义请见文末。

假设上面的文件被保存为example_style.ist。那么,使用它的方法是给\makeindex命令传递参数options= -s example_style.ist

\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}
In this example, several keywords\index{keywords} will be used which are important and deserve to appear in the Index\index{Index}.

Terms like generate\index{generate}, a great\index{great} list and som other\index{others} terms that might be important\index{important} 
will also show up. Terms in the index can also be nested \index{Index!nested}

\clearpage

\section{Second section}
This second section\index{section} may include some special word, and expand the ones already used\index{keywords!used}. 

\printindex
\end{document}

在这里插入图片描述

在目录中加入索引表

默认情况下,索引表并不包含在目录之中。当然,你可以很轻松地将其加入:

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

\begin{document}

\tableofcontents

\section{Introduction}
In this example, several keywords\index{keywords} will be 
used which are important and deserve to appear in the 
Index\index{Index}.

Terms like generate\index{generate} and some\index{others} 
will also show up. Terms in the index can also be 
nested \index{Index!nested}

\clearpage

\section{Second section}
This second section\index{section} may include some special 
word, and expand the ones already used\index{keywords!used}.

\printindex
\end{document}

在这里插入图片描述
\makeindex命令传递参数intoc即可。

参考指南

makeindex命令的参数

title
索引表的标题

intoc
是否将索引表的标题添加到目录之中

columns
语法:key=value。value 必须是整数,代表索引表中的列数。默认的值为2。

columnsep
制定两列之间的间隔宽度。例如:columnsep=2em

columnseprule
如果传递了此参数,那么列之间将会有一个垂直的线。

创建格式文件的关键字

下面介绍用于创建.ist文件的关键字、默认值,以及它们的解释。

actual<char>
@
符号表示了下一个条目将会出现在输出文件之中。

arg_close<char>
}
索引条目参数的闭分隔符

arg_open<char>
{
索引条目参数的开分隔符

encap<char>
'
符号:标示着剩余的参数列表将会被使用为页码的封装指令

escape<char>
\\
符号:其之后字母的转义字符(除非它之前的符号是它自己)。注意:引号被用作转义它之后紧跟着的字母,如果它之后是一个转义字符,那么它将被看作是一个正常符号。这两个符号必须是不同的。

keyword<string>
'\\indexentry'
命令:告诉makeindex它的参数是一个索引条目

level<char>
!
标示着一个新层级的子条目的分隔符

quote<char>
"
注意:引号被用作转义它之后紧跟着的字母,如果它之后是一个转义字符,那么它会被当做是一个正常字符。这两个符号必须是不同的符号。

range_close<char>
)
闭分隔符:标示着一个显式页范围的结束

range_open<char>
)
开分隔符:标示着一个显式页范围的开始

preamble<string>
\\begin{theindex}\n
输出文件的 preamble

postamble<string>
\n\n\\end{theindex}\n
输出文件的 postamble

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
如果heading_flag为正数,那么该参数代表将要插入的符号的标头

symhead_negative<string>
symbols
如果heading_flag为负数,那么该参数代表将要插入的符号的标头

numhead_positive<string>
Numbers
如果heading_flag为正数,那么该参数代表将要插入的数字的标头

numhead_negative<string>
Numbers
如果heading_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,与两个制表符等价)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值