LaTeX 标准文档类提供了 \section 和 \section* 等两组命令,用于排版章节标题。其中不带星号的版本有章节编号,会列入目录,同时修改章节标记。带星号的版本只有章节标题格式而不编号,不列入目录,也不会修改章节标记。
有时,我们会希望将不编号的章节标题列入目录。这种情况使用 \section 或 \section* 都不大合适。本文将对这些问题做出分析,并给出解决方案。
LaTeX 标准文档类的 \section 命令做四件事情:
- 编号自增 1
- 输出章节标题
- 处理目录(和 PDF 书签)
- 做页眉的章节标记
相应地,\section* 命令只做上述第二件事情,其余三件都不做。
因此,我们的思路很简单,有两个方向。第一,使用 \section*,手工做目录的处理;第二,使用 \section,但是抑制编号。
思路一LaTeX 处理目录需要编译两次。在第一次编译的过程中,\section 命令将目录信息写入 .aux文件。随后,在第二次编译的过程中,LaTeX 读取 .aux 文件中的相应信息,形成目录。因此,我们只需要模仿 \section 写入 .aux 文件的过程就可以了。
LaTeX 提供了 \addcontentsline{< 辅助文件后缀 >}{< 章节等级 >}{名字} 命令来做这样的工作。我们来看一个例子。
[color=rgb(170, 170, 170) !important]1 [color=rgb(170, 170, 170) !important]2 [color=rgb(170, 170, 170) !important]3 [color=rgb(170, 170, 170) !important]4 [color=rgb(170, 170, 170) !important]5 [color=rgb(170, 170, 170) !important]6 | \documentclass[color=rgb(51, 51, 51) !important]{ctexart[color=rgb(51, 51, 51) !important]} \begin[color=rgb(51, 51, 51) !important]{document[color=rgb(51, 51, 51) !important]} \tableofcontents \section*[color=rgb(51, 51, 51) !important]{不编号的章节标题[color=rgb(51, 51, 51) !important]} \addcontentsline[color=rgb(51, 51, 51) !important]{toc[color=rgb(51, 51, 51) !important]}[color=rgb(51, 51, 51) !important]{section[color=rgb(51, 51, 51) !important]}[color=rgb(51, 51, 51) !important]{不编号的章节标题[color=rgb(51, 51, 51) !important]} \end[color=rgb(51, 51, 51) !important]{document[color=rgb(51, 51, 51) !important]} |
思路二
LaTeX 标准文档类中的 \appendix 命令,会使得后续的章节标题从 0 开始编号,同时将编号格式从阿拉伯数字修改为大写英文字母。\appendix 命令虽然不符合我们的需求,但是产生的效果却和我们需要的效果类似:修改了章节标题编号。我们可以对这个命令做适当的修改,达成我们的目的。
我们来看一下 article.cls 里的 \appendix 是如何定义的。
[color=rgb(170, 170, 170) !important]1 [color=rgb(170, 170, 170) !important]2 [color=rgb(170, 170, 170) !important]3 [color=rgb(170, 170, 170) !important]4 | \newcommand\appendix[color=rgb(51, 51, 51) !important]{\par [color=rgb(0, 111, 224) !important] \setcounter[color=rgb(51, 51, 51) !important]{section[color=rgb(51, 51, 51) !important]}[color=rgb(51, 51, 51) !important]{0[color=rgb(51, 51, 51) !important]}[color=rgb(153, 153, 153) !important]% [color=rgb(0, 111, 224) !important] \setcounter[color=rgb(51, 51, 51) !important]{subsection[color=rgb(51, 51, 51) !important]}[color=rgb(51, 51, 51) !important]{0[color=rgb(51, 51, 51) !important]}[color=rgb(153, 153, 153) !important]% [color=rgb(0, 111, 224) !important] \gdef\thesection[color=rgb(51, 51, 51) !important]{[color=rgb(51, 51, 51) !important]\@Alph\c[color=rgb(51, 51, 51) !important]@section[color=rgb(51, 51, 51) !important]}[color=rgb(51, 51, 51) !important]} |
接下来,我们仿造它,定义一个新的命令 \specialsectioning。
[color=rgb(170, 170, 170) !important]1 [color=rgb(170, 170, 170) !important]2 [color=rgb(170, 170, 170) !important]3 [color=rgb(170, 170, 170) !important]4 [color=rgb(170, 170, 170) !important]5 [color=rgb(170, 170, 170) !important]6 [color=rgb(170, 170, 170) !important]7 [color=rgb(170, 170, 170) !important]8 [color=rgb(170, 170, 170) !important]9 [color=rgb(170, 170, 170) !important]10 [color=rgb(170, 170, 170) !important]11 | \documentclass[color=rgb(51, 51, 51) !important][hyperref[color=rgb(51, 51, 51) !important][color=rgb(51, 51, 51) !important]{ctexart[color=rgb(51, 51, 51) !important]} \newcommand\specialsectioning[color=rgb(51, 51, 51) !important]{\par [color=rgb(0, 111, 224) !important] \setcounter[color=rgb(51, 51, 51) !important]{section[color=rgb(51, 51, 51) !important]}[color=rgb(51, 51, 51) !important]{0[color=rgb(51, 51, 51) !important]}[color=rgb(153, 153, 153) !important]% [color=rgb(0, 111, 224) !important] \setcounter[color=rgb(51, 51, 51) !important]{subsection[color=rgb(51, 51, 51) !important]}[color=rgb(51, 51, 51) !important]{0[color=rgb(51, 51, 51) !important]}[color=rgb(153, 153, 153) !important]% [color=rgb(0, 111, 224) !important] \renewcommand\thesection[color=rgb(51, 51, 51) !important]{\relax[color=rgb(51, 51, 51) !important]}[color=rgb(51, 51, 51) !important]} \begin[color=rgb(51, 51, 51) !important]{document[color=rgb(51, 51, 51) !important]} \tableofcontents \section[color=rgb(51, 51, 51) !important]{正常编号的章节标题[color=rgb(51, 51, 51) !important]} \specialsectioning \section[color=rgb(51, 51, 51) !important]{不编号的章节标题[color=rgb(51, 51, 51) !important]} \end[color=rgb(51, 51, 51) !important]{document[color=rgb(51, 51, 51) !important]} |
编译之后我们会发现,在 \specialsectioning 之后的 \section 虽然不带星花,但已然不编号了,并且出现在了目录当中。
不过,正常标题的编号和标题正文之间是有一定的空距的。我们现在虽然将编号取消了,但是这个空距依然存在。虽然它不明显,但是毕竟是个问题。作为一个精益求精的完美主义者,我们要解决它。
正文中编号的格式由 LaTeX 内部宏 \@seccntformat 控制,默认是在编号后面加一个 \quad,我们把它去掉。
[color=rgb(170, 170, 170) !important]1 | \def[color=rgb(51, 51, 51) !important]\@seccntformat##1[color=rgb(51, 51, 51) !important]{[color=rgb(51, 51, 51) !important]\@nameuse[color=rgb(51, 51, 51) !important]{the##1[color=rgb(51, 51, 51) !important]}[color=rgb(51, 51, 51) !important]} |
选自:http://liam0205.me/2015/04/10/how-to-list-unnumbered-section-in-the-table-of-contents/