阶段 4 · 结构与交叉引用 30 / 48 ≈ 14 分钟读完

LaTeX 怎么建索引:imakeidx 与 makeindex 全链路

索引不是 LaTeX 写出来的一份列表,而是另一个程序去给 LaTeX 写出的文件排序。一旦你看清这条四文件的流水线,索引构建里的每一个谜题——条目不见了、条目归错了字母、带重音的名字掉进标点堆、再加一个记号索引——都有了显而易见该去哪儿修。

    四个文件、两个程序,干的是一件事

    你敲下 \index{Galois theory},LaTeX 并不会去建任何索引。它只是往一个叫 main.idx 的文件里写进一行 \indexentry{Galois theory}{3},然后继续排版。这一行里没有任何东西被排序、合并或格式化——它是一份原始流水账,按阅读顺序记下「这个字符串在这一页被提到过」,一次调用占一行。

    真正做排序的是外部程序,而这样的程序有两个。makeindex 是经典的那个:一个随每个发行版分发的迷你 C 工具,用 makeindex main.idx 调用。它读入 .idx,逐字节比对条目,再写出 main.ind——里面是一个 theindex 环境,层级已经套用、重复项已合并、页码列表已压缩成区间。它还会写出 main.ilg,也就是它自己的日志;某个条目莫名其妙消失时,答案通常就在这里。

    xindy 是它的接班人,而两者的差别正是你在意它的唯一理由:makeindex 比对的是字节,xindy 比对的是某门语言里的字符串。它按语言和文字分门别类地内置了索引样式模块,所以带重音的字母会排在基础字母之后(é 归到 E 下面,而不是掉进一个标点栏),还能处理西里尔、希腊和转写。代价是它需要自己那套输入方言,这也是各个宏包默认退回 makeindex 的原因。

    最后,\printindex 把 .ind 读回文档。它是链条里的第四个文件,也正是它让你能看懂每一个索引 bug:.tex → .idx →(.ist 加 makeindex 或 xindy)→ .ind → PDF。如果 \printindex 什么也没印出来,要问的永远是这四步里哪一步产出了一个空的或过期的文件。

    The index pipeline: tex source, idx file, makeindex or xindy with a style file, ind file, then printindex main.tex \index{Set!open} \index{Borel@Borel set} \printindex LaTeX does not sort main.idx \indexentry {Set!open}{12} one line per call, in reading order - unsorted external program makeindex main.idx xindy -L german -C utf8 sorts bytes (makeindex) or strings in a language (xindy) book.ist headings_flag 1 delim_2 ", --" symhead_positive -s style.ist main.ind \begin{theindex} Item Set 12 sub open sorted ranges style read by the program pass 2 and pass 3 of LaTeX \printindex reads the .ind; the index now occupies pages, so pagination moves and the recorded page numbers change main.ilg the makeindex log: entries it could not parse are reported HERE, never in main.log so the honest sequence is: compile, index, compile, index, compile - which is what imakeidx plus latexmk automate Two packages, same commands, different number of programs you type \usepackage{makeidx} + \makeindex + \printindex you run makeindex yourself, once per index file \usepackage{imakeidx} + \makeindex[title=Index] the tool is invoked at the end of the run, multicols output, named indices with \makeindex[name=symbols] and \printindex[symbols], program=xindy for Unicode collation
    LaTeX 只负责记录出现,排序交给另一个程序,而把结果排出来的是第二遍 LaTeX。

    makeidx 还是 imakeidx:谁去跑那个工具

    老的接口是贴近内核的 makeidx 宏包:导言区写 \usepackage{makeidx} 和 \makeindex,想要索引的地方写 \printindex——然后由你本人在两遍编译之间去跑 makeindex。命令行上就是 pdflatex book、makeindex book、pdflatex book。这确实管用,也正是很多人坚信索引「要编三遍」的由来:它其实要编两遍、外加跑一次索引,而多出来的那一遍是因为索引会改动条目当初写下的页码。

    imakeidx 保留了同样的命令,只把缺的那块补上:它会在这一遍结束(或在 latexmk 结束)时替你调用 makeindex,于是一遍编译就能得到成品索引。它还给了索引真正的分栏排版——输出用 multicol 来排,栏高平衡,索引不会再硬撑出一个分页——外加针对每个索引的选项。加载写作 \usepackage{imakeidx},声明写作 \makeindex[title=Index, columns=2]。要是你终究想自己到外面跑工具,就传 [makeindex=false];要换程序,就传 program=xindy。想让索引另起一页,就在 \printindex 前自己加一个 \clearpage,或者反过来用 nonewpage 选项去要。

    配 latexmk 时,imakeidx 什么都不必多做;但要是你留在 makeidx 上,正确的开关是 latexmk --makeindex -pdf book.tex,或者在 .latexmkrc 里写 $makeindex = 1;。编辑器各有各的脾气:好几个「统计字数」和「构建」按钮会整个跳过索引那一步,于是就有了那份经典报告——本地索引好好的,合著者编译出来的版本里却不见了。

    关于这种两段式设计,有个警告每个项目只会咬你一次:一个条目只能和它写下时所在的那一页一样准,而加入索引材料可能拉长文档、把每一页都挪走。所以改过一本长书靠前的条目之后,要再跑一遍索引那步——从旧的 .idx 生成的 .ind,看上去完全合理,却行行都是错的。

    \text{entry} = (\text{key},\ \text{page}) \qquad \text{both halves come from the previous pass}
    \text{entry} = (\text{key},\ \text{page}) \qquad \text{both halves come from the previous pass}

    条目语法:层级、排序键与封装算子

    在 \index{...} 里干活的一共就五个字符。! 分隔层级,所以 \index{Integral!Lebesgue} 会嵌在 Integral 之下,而 \index{Zeta function!values!at even integers} 嵌了两层。层级是靠排序发现的,不是靠声明:子条目会自动把主词拿来当自己的标题,这就是为什么主层级里一个拼写错误会凭空造出整整一个孤儿标题。@ 把排序键和打印形式分开:\index{Bourbaki@Bourbaki, Nicolas} 按 @ 前的键排序、显示 @ 后的内容。| 引入一个封装算子,也就是套在页码上的命令:\index{group|textbf} 把页码加粗。

    封装算子还能标出一段区间。在第一次提到处用 \index{factorial|(textbf} 打开它,在最后一次提到处用 \index{factorial|)} 关掉它;makeindex 于是写出 \(textbf{245--248},算子作用在整段区间上而非单个页码——这正是「一个概念被真正展开讲」的那几页的惯例。" 是转义符,因为 makeindex 把 ! @ | 和这个引号本身都保留了下来:\index{"@\textbackslash\ bs} 通过在要排序的那个字符前加引号,把一个字面符号放进索引。而 \see(来自 makeidx)产出的是一行不带页码的交叉引用:\index{Cohomology!de Rham|see{De Rham cohomology}}。

    大家最常写错的两种形式值得点明。键是在任何 LaTeX 被展开之前就比对的,所以@ 之前那部分应当是纯 ASCII,条目里的数学要放在 @ 之后:\index{sigma-algebra@$\sigma$-algebra} 排在 s 下面、显示那个符号。其次,一个含 \\ 或段落断行的条目,对 LaTeX 不算错、对 makeindex 却是——条目请保持在一行之内。

    一个实用习惯:给提到建索引,而不是给标题建。把 \index{...} 紧跟在它描述的那个词或那个公式之后,才能让条目落在正确的页上;放在段落末尾,就有滑到下一页的风险;放进 \section 标题里,则在排目录时又被执行了一遍。

    \text{Zeta function ! values ! at even integers} \;\longmapsto\; \zeta(2) = \tfrac{\pi^{2}}{6}
    \text{Zeta function ! values ! at even integers} \;\longmapsto\; \zeta(2) = \tfrac{\pi^{2}}{6}
    Anatomy of an index entry: levels, sort key, display form and encapsutor, plus makeindex special characters \index{Measure!outer@{\textbackslash outer\ measure}|(textbf} Measure! primary level - the heading the reader looks for first outer secondary level, and at the same time the SORT KEY @{\textbackslash outer measure} what is printed in the index; braces keep the macro from eating the rest |(textbf open a bold page range; close it later with \index{...|)} THE FIVE CHARACTERS makeindex OWNS INSIDE \index{...} ! level A!B!C nests twice under A; a typo makes an orphan heading @ key / print ASCII before it, typeset form after it - fixes accents too | encapsutor |textbf bolds one page, |(textbf ... |) bolds a range " escape \index{"@} puts a literal reserved character in the index \see{...} cross-reference with no page number, from makeidx WHAT THE READER SEES AFTER makeindex Measure 12, 14--19, 41; outer measure 14--19 sub-entry indented, ranges collapsed by the .ist delimiters, the bold range from the |(textbf ... |) pair
    一个条目字符串,从左往右读:先是层级,再是排序键,然后是打印形式,最后是怎么处理页码。

    重音、数学符号与排错序的条目

    这是每本非英文书都会撞上的毛病。makeindex 是个 8-bit 文本工具,从不和 LaTeX 说话,所以它按 .idx 那行写出之后它看到的字节来排序。像 \index{\'Ecole de Paris} 这样的条目——一个反斜杠、一个重音宏、一对花括号——按它第一个非空白字节排序,而那是反斜杠,于是整个名字落到索引最前面的标点栏里,挂在一个根本不存在的标题下面。带重音的专有名词、以数学开头的条目、含花括号的名字,全都以同样的方式翻车。

    在 makeindex 下,修法永远是排序键:把 ASCII 形式写在 @ 前、排版形式写在 @ 后,并把任何脆弱的东西用花括号包住好让分组撑住——\index{Ecole@{\'Ecole} de Paris},或者用 UTF-8 输入时写 \index{Beyonce@Beyoncé}。这算不上什么见不得人的取巧:@ 字段本就为此而生,它还顺手解决了「按姓排序、显示名」这一类文献表式索引里每个条目都会遇到的情况。

    在 xindy 下,修法是结构性的:xindy 读的是字符,所以带重音的条目根本不用排序键就能正确归类,你通过它的语言模块选校对规则——德语用 \makeindex[program=xindy, options=-L german -C utf8](含 DIN 排序那几个变体),西里尔用 -L russian,以此类推。xindy 还额外能读懂经它 tex 输入样式编码的数学条目,当一个记号索引远不止一串符号名时,这就是该抓的那个选项。

    数学条目值得单独一条规矩:给名字建索引,而不只是符号,并把符号放进显示部分。\index{sigma-algebra@$\sigma$-algebra} 给出的是一条读者只要记得词就能找到的条目,再在「S」下补一条又能兜住只记得字母的人。记号索引通常就该是独立的一个索引,原因正在于此;它也是唯一一个可以接受不写排序键的写法(直接对某个符号建索引)的地方,因为读者本来就预期符号按字符串来排。

    \underbrace{\text{sorts under B}}_{\text{key}} \quad \text{prints as} \quad \underbrace{\text{Bourbaki, Nicolas}}_{\text{display}}
    \underbrace{\text{sorts under B}}_{\text{key}} \quad \text{prints as} \quad \underbrace{\text{Bourbaki, Nicolas}}_{\text{display}}
    如果某个条目无声消失,去开 .ilg 日志而不是 .log:makeindex 把无法解析的行报在那里,而在 LaTeX 里它一声不吭。
    % ---- the style file, written by the document itself before the class ----
    \begin{filecontents*}{book.ist}
    preamble         "\\begin{theindex}\n"
    postamble        "\n\n\\end{theindex}\n"
    headings_flag    1        % emit a heading for each new letter
    heading_prefix   "\\vspace*{14pt}\\nopagebreak\\textbf{\\large "
    heading_suffix   "}\\nopagebreak\n"
    symhead_positive "Symbols"
    numhead_positive "Numbers"
    delim_0          ", "
    delim_1          ", "
    delim_2          ", --"
    encap_prefix     "\\textbf{"
    encap_infix      "}--\\textbf{"
    encap_suffix     "}"
    \end{filecontents*}
    
    \documentclass[12pt,a4paper]{report}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{amsmath}
    \usepackage[margin=2.5cm]{geometry}
    \usepackage{hyperref}
    \hypersetup{colorlinks=true, linkcolor=blue}
    \hypersetup{hyperindex=false}   % index page numbers as plain text, not links
    \usepackage{imakeidx}
    \makeindex[options=-s book.ist, title=Index]
    
    \begin{document}
    \chapter{Names and accented entries}
    
    % Sort key before @, printed form after it. The key is what makeindex sorts;
    % it must be plain ASCII, because makeindex is an 8-bit tool that never sees
    % the fonts LaTeX will use.
    Beyoncé\index{Beyonce@Beyoncé} appears beside Bourbaki\index{Bourbaki}.
    \'Ecole\index{Ecole@{\'Ecole} de Paris} sorts under E, not under the backslash.
    
    % makeindex reserves ! @ | and the quote character; the quote protects them,
    % so these three entries display the characters themselves.
    The reserved characters are \index{at@"@}, \index{bang@"!} and
    \index{pipe@"|}.
    
    % An index call belongs in the text, not in the heading: the heading is
    % re-typeset for the table of contents, which would write the entry twice.
    \section[Density]{Density}
    % If a heading really must carry one, \protect it and give a short optional
    % form:  \section[Density]{Density\protect\index{Density}}
    The Radon--Nikod\'ym derivative\index{Radon-Nikodym@Radon--Nikod\'ym}
    $d\mu/d\nu$ is the density of $\mu$ with respect to $\nu$%
    \index{Density}\index{Measure!density}.
    
    % Entries inside a footnote are legal; keep them short, and remember that the
    % page recorded is the page the footnote is set on.
    Text.\footnote{Indexed fact.\index{Footnote!entries in}}
    
    \printindex
    \end{document}

    一份文档里放两三个索引

    一本教材通常至少要两个:概念用的主题索引,和符号用的记号索引。用 imakeidx 时,你给每个索引起个名字来声明——\makeindex[name=symbols, title=Index of Notation, columns=2]——然后按名字往里写:\index[symbols]{\sigma-algebra}。不带名字的 \index{...} 永远进默认索引,所以两种写法混用是合法的,也是常态。要印出每个索引用 \printindex[symbols]。

    在老的 makeidx 下,这需要第三方宏包(multind、index),还要为每个索引手动跑一遍 makeindex——这正是选用 imakeidx 最有力的现实理由。有多个索引时,各有各的 .idx 和 .ind,latexmk 或 imakeidx 会为每个文件各跑一次工具。请记住:每个索引记下的页码都是那一次运行的页码,所以所有索引得一起重新生成,不能一个一个来。

    一个辅助宏值得定义一次,只要它把排序键当作自己独立的一个参数:\newcommand{\sym}[2]{\ensuremath{#1}\index[symbols]{#2@\ensuremath{#1}} 会排出一个符号、并用一个 ASCII 键把它索引进来,这正是「能排序的记号索引」和「掉进标点堆的记号索引」之间的分水岭。给正文用时,\newcommand{\xdx}[2]{#1\index{#2}} 干同样的活。两者都把条目紧贴出现处,也就是让页码诚实的办法。例外是你只想在定义处索引一次、却提到四十次的那个术语:只索引定义处,然后在另一种拼写第一次出现的地方补一条 |see。

    \documentclass[12pt,a4paper]{report}
    
    \usepackage[T1]{fontenc}
    \usepackage{amsmath}
    \usepackage[margin=2.5cm]{geometry}
    
    % ---- two independent index streams ---------------------------------
    \usepackage{imakeidx}
    \makeindex[title=Subject index]
    \makeindex[name=symbols, title=Index of Notation, columns=2]
    % xindy instead of makeindex: correct Unicode sorting, language collation.
    % \makeindex[program=xindy, options=-L english -C utf8 -I xindy]
    
    % A notation entry needs an ASCII sort key, so the helper takes two arguments:
    % #1 is what is typeset, #2 is what makeindex sorts on.
    \newcommand{\sym}[2]{\ensuremath{#1}\index[symbols]{#2@\ensuremath{#1}}}
    
    \begin{document}
    \chapter{Measure}
    
    Let \sym{\Omega}{Omega} be an open set and \sym{\mu}{mu} a measure on it, so
    that \sym{\sigma(\mathcal{A})}{sigma(A)} is the smallest
    $\sigma$-algebra containing \sym{\mathcal{A}}{A set}.%
    \index{sigma-algebra@$\sigma$-algebra}%
    \index{Measure!countably additive}
    A function is integrable when
    \[
      \int_{\Omega} \lvert f \rvert \,d\mu < \infty .
    \]
    \index{Integral!Lebesgue}%
    \index{Integrable function}
    The \emph{support} of $f$ is the closure of the set where it is non-zero.%
    \index{Support (of a function)}
    
    \chapter{Function spaces}
    The space $L^{p}(\Omega)$ is complete for $1 \le p \le \infty$;%
    \index{Space!Lp@$L^{p}$|(textbf}
    see \index{Space!Lp@$L^{p}$!duality|see{Duality of Lp spaces}}the duality with
    $L^{q}$ where $1/p + 1/q = 1$.%
    \index{Space!Lp@$L^{p}$|)}
    
    \clearpage
    \printindex            % subject index
    \printindex[symbols]   % notation index, printed after it
    \end{document}

    排序与版式:.ist 文件改了什么

    索引样式文件(扩展名是 .ist,用 options=-s book.ist 传进去)是 makeindex 唯一的外观控制,里面装着任何 LaTeX 命令都够不着的决定。headings_flag 1 请求在每组前加一个字母标题;heading_prefix 和 heading_suffix 把它包起来,而在这两个字符串里 \letter 会被换成那个字母——这就是为什么它们在样式文件里必须做双重转义。symhead_positive 和 numhead_positive 给符号和数字落脚的伪分组命名,没有它们,你的 \sigma 条目会出现在一个光秃秃、没标题的栏里。

    delim_0、delim_1、delim_2 这几个键设定每一层级页码前的分隔符——英式惯例是前两级用 ", "、第三级用 ", --",于是三级条目读起来是一道破折号而不是逗号串。encap_prefix/encap_infix/encap_suffix 控制一段加粗区间怎么写,keyword_group 则把一组术语钉在一起——对付条目末尾那堆「另见」很有用。

    索引的排版部分归 LaTeX,就在 theindex 环境里:\parindent=0pt,组与组之间靠 \indexspace 留一点竖空,各级缩进由文档类设定。要改字体或缩进,你要么用 \indexsetup{othercode=\footnotesize} 给 imakeidx 额外塞代码,要么在类文件里重定义 theindex。而要把索引放进目录,那行得你自己加——在 \printindex 前写 \addcontentsline{toc}{chapter}{Index}——这和其他每个不编号标题用的是同一招。

    构建顺序、hyperref 与那个消失的条目

    索引和 hyperref 的相互影响只以一种可见的方式发生:默认 hyperref 会把索引页码变成链接,于是一段页码区间成了一道长长的蓝条,多数读者觉得它比没链接还糟。\hypersetup{hyperindex=false} 让页码保持素净,同时文档其余部分照旧可点;想要链接的话,配套选项 hyperindexformat 允许你对每个号套一个宏,而 |hyperpage 封装算子会把区间处理正确。

    条目消失的原因就一小串,逐条走一遍比推理更快。这个 \index 调用待在一个移动参数里、被展开了两遍——用 \protect\index{...} 保护它,或把它挪出标题。它待在一个浮动体的 \footnote里,而那个脚注被排版的地方和源码说的不一样。它待在一个 verbatim 环境里,那里根本没有任何命令会执行。它从没被读到,因为装着它的文件在一个被 \includeonly{} 列表排除掉的 \include{} 里。最后,.idx 在、但 .ind 过期了——makeindex 的日志 .ilg 会凭时间戳证实这一点。

    总能收敛的顺序是:编译、索引、编译、索引、编译——三遍 LaTeX,索引步插在第二遍之后,因为第二遍会改分页,也就改了索引当初记下的那些页码。latexmk 配 imakeidx 自动化的正是这套,且一旦没有变化就停下;要是你自己写构建脚本,别「优化」掉最后那一遍,因为差了一页的索引,比没有索引更糟。

    最后一件事没有哪个宏包能答:到底要多少条目。把每个「函数」出现处都列出来的索引不是索引,而是一份 concordance;标准惯例是标出概念被定义或展开的那些页(这正是加粗区间的用途)、每个名字只在有实质讨论处索引一次、并为读者真会去试的同义词写交叉引用。靠每次提到都补一个 \index{} 堆出来的索引,读起来比查目录还费时间。

    \{245, \mathbf{248}, \mathbf{249}, \mathbf{250}, 251\} \quad \longrightarrow \quad 245, \ \mathbf{248\text{--}251}
    \{245, \mathbf{248}, \mathbf{249}, \mathbf{250}, 251\} \quad \longrightarrow \quad 245, \ \mathbf{248\text{--}251}
    \documentclass[11pt,a4paper]{article}
    
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{amsmath}
    \usepackage[a4paper,margin=2.5cm]{geometry}
    \usepackage{imakeidx}          % loads makeidx AND runs makeindex for you
    
    \makeindex[title=Index, columns=1]
    
    \begin{document}
    \section{Three special values}
    
    The Gaussian integral%
    \index{Integral!Gaussian}%
    \index{Gaussian integral}
    \[
      \int_{0}^{\infty} e^{-x^{2}}\,dx = \frac{\sqrt{\pi}}{2}
    \]
    is the prototype of a factorial bound, since $n!$ eventually dominates
    $a^{n}$ for every fixed $a$.%
    \index{Growth!of a factorial}%
    \index{Factorial|(textbf}          % open a bold page range
    Stirling's formula gives $n! \sim \sqrt{2\pi n}\,(n/e)^{n}$.%
    \index{Stirling's formula}
    The same factorial appears in the binomial
    coefficient $\binom{n}{k} = n!/(k!(n-k)!)$%
    \index{Binomial coefficient}%
    \index{Factorial|)}                % close the bold page range
    
    \section{Values of the zeta function}
    Euler found $\zeta(2) = \pi^{2}/6$%
    \index{Zeta function!values!at even integers}
    and, more generally, $\zeta(2m)$ is a rational multiple of $\pi^{2m}$ for every
    $m \ge 1$.%
    \index{Zeta function!values!at even integers}
    For the half-integer argument of Gamma, see
    \index{Gamma function!at one half}$\Gamma(1/2) = \sqrt{\pi}$.\index{Pi}
    
    \printindex                          % reads main.ind
    \end{document}

    本课速查

    每节抽一行关键代码:点小节名跳到对应位置,代码一键复制。

    小节 关键代码
    \text{entry} = (\text{key},\ \text{page}) \qquad \text{both halves come from the previous pass}
    \text{Zeta function ! values ! at even integers} \;\longmapsto\; \zeta(2) = \tfrac{\pi^{2}}{6}
    \begin{filecontents*}{book.ist}
    \documentclass[12pt,a4paper]{report}
    \documentclass[11pt,a4paper]{article}

    常见错误

    • 只跑 LaTeX,然后纳闷为什么 \printindex 什么都没印。索引是第二个程序做的:用 imakeidx 它自动跑,用 makeidx 你得在两遍之间手动跑 makeindex main,然后再编两遍。
    • 把带重音的名字写成 \index{\'Ecole}:条目会按反斜杠排序、落进标点栏。把 ASCII 排序键写在 @ 前——\index{Ecole@{\'Ecole} de Paris}——或者改用 xindy。
    • 一处写 \index{Integral!lebesgue}、另一处写 \index{Integral!Lebesgue}:印出来两个看着一样的标题,页码被拆到两边。子条目字符串是逐字比对的。
    • 把 \index{...} 放进 \section 标题又不加 \protect:标题会为目录重排一遍,于是条目被写两次、还可能记错了页。
    • 指望 hyperref 链接过的页码区间看起来清爽。加粗区间叠上彩色链接根本没法读;设 hyperindex=false,或者用 |hyperpage 封装算子而不是默认那套。
    • 在清理构建目录的同一个脚本里删掉 .idx、.ind 和 .ilg,然后交上去一份早先那轮建出来的索引——或者反过来,提交了 .ind 却在章节改写完后再没重新生成。

    关于本课的常见问题

    makeidx 和 imakeidx 有什么区别?
    makeidx 提供 \makeindex、\printindex 和 \see、\indexspace 这几个小助手,然后把索引程序留给你自己跑:编译、运行 makeindex、再编译。imakeidx 保留同样的命令,在本遍结束时替你调用该程序,用平衡的多栏排索引而不强行分页,还支持带名字的多个索引,每个索引可单独设 title=、columns=、program=xindy 以及 options=-s style.ist 等选项。
    我的索引条目为什么不见了?
    按顺序查四件事:那一行到底有没有被读到(被排除的 \include 文件根本不会执行),条目是不是在没有 \protect 的移动参数里,是不是在 verbatim 内容里,以及 .ind 是不是比 .idx 还旧。然后再读 makeindex 的日志 .ilg,它会报出无法解析的条目——LaTeX 本身对这类问题一言不发。
    怎么给一个人名建索引,让它归到姓氏下面?
    用 @ 分隔符:\index{Bourbaki@Bourbaki, Nicolas} 按 at 符号前的键排序、打印后面的内容。同一机制也能救带重音和带符号的条目——那里的键必须是纯 ASCII,因为 makeindex 排的是字节,不是字符。
    我能既放一个记号索引、又放一个主题索引吗?
    能,用 imakeidx:声明 \makeindex[name=symbols,title=Index of Notation,columns=2],用 \index[symbols]{...} 往里写,再用 \printindex[symbols] 打印。每个索引各自保留自己的 .idx 和 .ind,所以要在一次构建里一起重新生成,别一个一个来。
    我的带重音索引条目为什么会排到符号底下?
    因为 makeindex 是个 8-bit 工具,在 LaTeX 展开任何东西之前就比对 .idx 里发现的字节,于是一个开头的反斜杠或重音宏会被当成标点来排。在 @ 前给它一个 ASCII 排序键,或者把索引程序换成 xindy,它懂语言校对规则(-L german、-C utf8 之类)。
    带索引的文档到底要编几遍?
    编译、跑索引程序、编译、再跑一遍、编译。索引占页,因而会改分页;而每个条目都带一个页码,所以正是第二遍索引让页引用和最终版式对得上。latexmk 和 imakeidx 会自动把这笔账算清。

    实时演示:直接编辑 LaTeX,立刻看渲染结果

    预览随输入实时更新——不妨故意写坏一次,看看报错长什么样。

    KaTeX 错误
    试一个例子

    继续读

    同一个东西的三个入口:记法本身、讲它的那一课、以及真正用它写东西的一篇文章。