阶段 2 · 文本与文档结构 15 / 48 ≈ 14 分钟读完

LaTeX 章节结构与目录:六级标题与两个深度旋钮

LaTeX 里的分级命令同时是计数器、层级和排版宏,而目录是一份在一个遍次里写出、下一个遍次里读回的文件。这一课给出 secnumdepth 与 tocdepth 所要比较的那些级别号、星号究竟移走了什么的准确差别、如何把一个不编号的标题放进目录列表,以及用 titlesec 或 KOMA-script 重排标题样式而不破坏编号与引用的办法。

    六级、三个类,以及每一级步进的计数器

    标准类的完整词汇是 \part,然后 \chapter(只在 report 和 book 里存在)、\section、\subsection、\subsubsection、\paragraph 和 \subparagraph。每一条同时是三样东西:它步进的计数器、由该计数器构造的打印编号格式,以及一段排版样式。分清自己动的是哪一个,标题才可预测——而你在哪个类里是一个结构决定,不是外观决定:文档若有章,它就是 report 或 book,而 \chapter 在 article 里根本不存在。

    类通过内核宏 \@startsection 来定义这些命令,其参数是名字、级别号、缩进、前置空间、后置空间和样式。就这一行,解释了人们大多数的挣扎。负的前置空间就是告诉 LaTeX 不要缩进标题之后的那个段落;负的后置空间让标题与正文接排、不自占一行,这正是 \paragraph 做的事,也是它看起来与上面各级不同的原因。

    印在标题旁边的编号来自 \thesection、\thesubsection 等等。在 article 里它们是裸的(\arabic{section});在 book 里是复合的——\thesection 定义为 \thechapter.\arabic{section},「3.2」就从这儿来。改了这些定义里的任何一个,\ref 的输出也随之而变,正如 \theenumi 之于列表条目。

    用能承载含义的最浅一级,然后就停在那里。需要第四级编号的文档通常需要的是重构,而期刊与论文稿件的行规很少允许超过三级。再往下,\paragraph 与 \subparagraph 是接排标题:它们的存在是为了拆开一长堵正文,默认不编号,默认也进不了目录列表。

    \begin{array}{c|l|l|l}\text{level} & \text{command} & \text{counter} & \text{numbered by default} \\\hline 0 & \texttt{part / chapter} & \texttt{part, chapter} & \text{yes} \\ 1 & \texttt{section} & \texttt{section} & \text{yes} \\ 2 & \texttt{subsection} & \texttt{subsection} & \text{yes} \\ 3 & \texttt{subsubsection} & \texttt{subsubsection} & \text{article: yes} \\ 4 & \texttt{paragraph} & \texttt{paragraph} & \text{no} \\ 5 & \texttt{subparagraph} & \texttt{subparagraph} & \text{no}\end{array}
    \begin{array}{c|l|l|l}\text{level} & \text{command} & \text{counter} & \text{numbered by default} \\\hline 0 & \texttt{part / chapter} & \texttt{part, chapter} & \text{yes} \\ 1 & \texttt{section} & \texttt{section} & \text{yes} \\ 2 & \texttt{subsection} & \texttt{subsection} & \text{yes} \\ 3 & \texttt{subsubsection} & \texttt{subsubsection} & \text{article: yes} \\ 4 & \texttt{paragraph} & \texttt{paragraph} & \text{no} \\ 5 & \texttt{subparagraph} & \texttt{subparagraph} & \text{no}\end{array}
    The sectioning ladder with the level number each command carries, the number it prints in the article class, and where secnumdepth and tocdepth cut it off SIX LEVELS, THE COUNTER EACH ONE STEPS, AND THE TWO DIALS THAT CUT THE LADDER SHORT COMMAND LVL COUNTER PRINTED NUMBER NUM TOC \part \chapter (report · book only) \section \subsection \subsubsection \paragraph \subparagraph 0 0 1 2 3 4 5 part chapter section subsection subsubsection paragraph subparagraph Part I 2 2.1 2.1.3 (run-in) (run-in) — not in article yes yes no such command yes yes yes yes yes yes no no no no the red rule is where the article class stops: secnumdepth and tocdepth both ship at 3 THE TWO DIALS ARE INDEPENDENT article ships both at 3; report and book at 2. \setcounter{secnumdepth}{4} numbers \paragraph too \setcounter{tocdepth}{1} the list stops at \section \setcounter{secnumdepth}{-1} nothing numbered at all Ask your class, do not trust memory: \showthe\c@secnumdepth HOW "2.1.3" IS ASSEMBLED \thesection = \arabic{section} article — a plain number, 2 \thesection = \thechapter.\arabic{section} book — and that is where 3.2 comes from Redefine one of these and \ref changes with it: the counter and its printed shape are one macro. BELOW THE CUT, AND THE STAR \paragraph and \subparagraph are run-in: the space after them in \@startsection is negative, so they take no line to themselves. That is the difference. \section*{X} is a third answer, not a hidden number: no counter step, no .toc line, no running-head mark, and \ref inside it lands on the previous heading.
    六个层级、它们的级别号,以及标准 article 类默认在哪里切断编号与目录列表。

    secnumdepth 与 tocdepth:两个旋钮,不是一个

    每个分级命令都带一个级别号——\part 是 0、\section 是 1、\subsection 是 2、\subsubsection 是 3、\paragraph 是 4、\subparagraph 是 5——有两个计数器要与它比较。secnumdepth 决定编号走多深,tocdepth 决定目录列多深。两者互相独立,所以某一层可以被编号却不被列出,或被列出却不编号,两种组合都合法:一份编号到 \subsection 却把 \subsubsection 也列进目录的报告,或者一篇 \section 以下全部编号、目录里却只出现章的论文。

    标准 article 类把两者都设为 3,这就是 \subsubsection 既编号又列出、而 \paragraph 两者皆无的原因;report 和 book 出厂更浅,所以论文模板要做的第一件事就是把它们抬高。在导言区按普通计数器设:\setcounter{secnumdepth}{4} 与 \setcounter{tocdepth}{2}。把 secnumdepth 设成 -1 就什么都不编号,这是得到一份无编号标题文档、同时保留 \section 的排版与目录条目的干净办法。

    你也可以在文档中途改它们,而且用 \addtocontents 而不是 \setcounter 是有理由的:目录是在下一遍从 .toc 文件读回来的,所以写在正文里的 \setcounter{tocdepth}{...} 只影响条目的写入、不影响读取,文档为此可能要多跑一遍才能安定。\addtocontents{toc}{\protect\setcounter{tocdepth}{1}} 把改动放进文件本身,于是它在构建列表的那一遍里生效。

    想知道你的类究竟设了什么,而不是相信任何人对默认值的记忆(包括本页):在导言区写 \makeatletter \typeout{tocdepth=\the\c@tocdepth, secnumdepth=\the\c@secnumdepth} \makeatother,两个值就都进日志;\showthe\c@tocdepth 则让运行停下来打印其中一个。两者都是两秒钟的检查,也都是你和一份从没用过的类讲和的方式。

    \text{book}\colon\ \texttt{\textbackslash thesection} \;=\; \texttt{\textbackslash thechapter.\textbackslash arabic\{section\}} \qquad \text{article}\colon\ \texttt{\textbackslash thesection}=\texttt{\textbackslash arabic\{section\}}
    \text{book}\colon\ \texttt{\textbackslash thesection} \;=\; \texttt{\textbackslash thechapter.\textbackslash arabic\{section\}} \qquad \text{article}\colon\ \texttt{\textbackslash thesection}=\texttt{\textbackslash arabic\{section\}}
    \documentclass[11pt]{article}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    
    % How deep to number (levels: part 0, section 1, subsection 2,
    % subsubsection 3, paragraph 4, subparagraph 5) and how deep to list in the
    % table of contents. Raise both if you want paragraph headings numbered.
    \setcounter{secnumdepth}{4}
    \setcounter{tocdepth}{3}
    
    \usepackage{hyperref}          % so that \texorpdfstring below is defined
    
    \begin{document}
    
    \title{Structure and the table of contents}
    \author{A. Student}
    \date{}
    \maketitle
    
    \begin{abstract}
    One sentence that says what the report measured. The abstract is not a
    sectioning level, which is why it never appears in the contents list unless
    you add it by hand.
    \end{abstract}
    
    \tableofcontents
    
    \section{Method}
    Body text. Numbering comes from the counter, and the heading owns its own
    before-and-after space -- never add vertical space by hand here.
    
    \subsection{Sampling}
    \subsubsection{Site selection}
    Numbered at the third level.
    \paragraph{Equipment list}
    Level four is numbered only because \texttt{secnumdepth} was raised above its
    default; note that this style of heading runs into the text instead of taking
    a line to itself, which is what a level-four heading is for.
    
    \section*{Acknowledgements}
    % A starred heading is unnumbered and invisible to the contents list, so it
    % has to be announced explicitly:
    \addcontentsline{toc}{section}{Acknowledgements}
    
    \section{Results with a math title}
    % \texorpdfstring keeps the .toc and the PDF bookmarks readable.
    \section{\texorpdfstring{$E = mc^{2}$}{E = mc2} and its consequences}
    Nothing in this file needs a fourth pass; \texttt{latexmk} runs as many as
    the labels and the contents list ask for.
    
    \end{document}

    目录是怎么做出来的,为什么非要有第二遍

    LaTeX 里没有任何东西在即时组装目录。处理到一个标题时,文档类往 .toc 文件写一行——实际上是 \contentsline {section}{\numberline {2.1}Method}{3}{page.7}——而 \tableofcontents 是一个把该文件重放一遍的读取器。同一套机制换个扩展名,就从 \caption 产出图清单与表清单(.lof、.lot),并在 hyperref 下产出 PDF 书签树(来自 .out 文件)。

    因为这个文件是「正在进行中的这一遍」的副产品,第一次编译写入的条目是读取器还没见过的,所以首跑时第二页上的目录是上一遍留下的东西——头一次跑则是一个空文件。这与让 \ref 晚一拍解析的是同一条两遍逻辑,日志也这么写:LaTeX Warning: Label(s) may have changed. Rerun. latexmk 替你数遍数;手工编一份带目录的文档至少两遍。

    每一层由一个名为 \l@<level> 的宏绘制,内核把它实现为对 \@dottedtocline 的调用,带三个数字:层级、从左边距起的缩进、以及留给编号的宽度。一份目录对不齐的全部解释就在这里——改那个编号盒的宽度,标题就又齐了。手工改样式意味着重定义这些宏;替你做的宏包是 tocloft(按层的类键长度)、titletoc(整行布局的完全控制,含按层的横线和无点线)与 etoc(按部或按章的局部目录,加上精细的深度控制)。

    目录上方那个标题本身也是类宏:\contentsname,朴素文档里用 \renewcommand\contentsname{Outline} 改名。在 babel 下这招会失效,因为这个名字归语言模块所有——正确的路是 \addto\captionsenglish{\renewcommand{\contentsname}{Outline}}。\listfigurename 与 \tablename 是另外两个清单的对应物,而 KOMA-script 的类把三个名字都包进 \addtokomafont{captionhead} 风格的元素,而不是裸的名字。

    \begin{array}{l}\texttt{\textbackslash addcontentsline\{toc\}\{subsection\}\{\{Title\}\}\{\{2\}\}} \\ \texttt{\textbackslash addtocontents\{toc\}\{\textbackslash protect\textbackslash setcounter\{tocdepth\}\{1\}\}}\end{array}
    \begin{array}{l}\texttt{\textbackslash addcontentsline\{toc\}\{subsection\}\{\{Title\}\}\{\{2\}\}} \\ \texttt{\textbackslash addtocontents\{toc\}\{\textbackslash protect\textbackslash setcounter\{tocdepth\}\{1\}\}}\end{array}
    How the table of contents is built: each heading writes a contentsline into the .toc file and tableofcontents replays that file on the next pass, with the page numbers it recorded NOTHING IN THE LIST IS COMPOSED LIVE — IT IS A FILE WRITTEN BY ONE RUN AND READ BY THE NEXT PASS 1 · WRITE \subsection{Method} steps the counters and appends one line report.toc THE .toc FILE a by-product of the run that is happening now, so nobody has read it yet \contentsline{subsection} PASS 2 · READ \tableofcontents replays the file of the previous run, one line \l@subsection{...} PASS 3 · SETTLED the numbers no longer move the breaks, so the list matches its own pages latexmk -pdf → 2-3 runs the page numbers the list prints can themselves shift a page break — that is the whole reason a second run exists on the very first compile the file is empty and the list is blank; the log says so: LaTeX Warning: Label(s) may have changed. Rerun. ONE LINE, FOUR ARGUMENTS \contentsline {subsection}{\numberline {2.1}Method}{3}{subsection.2.1} 1 the level name, which picks the \l@… macro that draws it 2 the number box plus the title 3 the page number as it stood then 4 the bookmark anchor AND THE ENTRY IT DRAWS 2.1 Method 3 number box · title · leaders · page indent and number-box width come from \@dottedtocline{2}{3.8em}{2em} SAME MECHANISM, OTHER FILES .lof \caption in a figure, read by \listoffigures .lot \caption in a table, read by \listoftables .out written by hyperref: the PDF bookmark tree WHEN THE LIST IS WRONG, NAME THE MECHANISM a starred heading wrote nothing: add the line by hand below tocdepth, or one run short — the file lags the text ragged titles: that level's number box is too narrow
    一个标题往 .toc 文件里写一行;下一遍看到的目录,就是那个文件的重放。

    星号到底移走了什么

    \section*{X} 不是「把编号藏起来的 section」。星号形式是跳过计数器步进、跳过 .toc 那一行、并跳过运行页眉的标记。所以带星号的标题无法被引用(指向它内部某个 label 的 \ref 会落在上一个被编号的东西上),它不在目录里,而在 book 里,它起始那一页的页眉仍写着上一章。

    常见的情形——致谢、摘要、参考文献、你不想编号的引言——要的是标题的排版,外加一个没有编号的目录条目。显式的一对是 \section*{Acknowledgements} 后面跟 \addcontentsline{toc}{section}{Acknowledgements};第二个参数是层级名,而条目不带编号出现,因为你没有提供 \numberline。KOMA-script 的类把这做成了内置的 \addsec{...} 与 \addchap{...},类既然提供了,那就是更短也更不易出错的路。

    姊妹命令 \addtocontents{toc}{...} 往文件里写的是原材料而不是一条条目,这就是你在章与章之间插一条横线、一段垂直空间或一小段全大写分隔标题的办法——而且,如上所述,也是在文档中途改 tocdepth、让读取器和写入器达成一致的办法。你以这种方式写入的任何东西都会在列表被读取时展开,所以标准写法要带 \protect 前缀:\addtocontents{toc}{\protect\vspace{6pt}}。

    book 里带星号的标题通常还要手工修页眉:紧跟标题写 \markboth{Acknowledgements}{Acknowledgements},单面印刷的文档则用 \markright{...}。就这一行,决定前置部分是看起来专业,还是页眉里仍写着上一章。

    前置部分、正文,以及彼此一致的页码

    report 和 book 把文档切成 \frontmatter、\mainmatter 和 \backmatter。这三个声明在边界上做的是一揽子事:前置部分用罗马数字编号,其章不编号但要列出;正文部分从 1 重新开始、用阿拉伯数字,并打开章编号;后置部分保持阿拉伯数字编号,再次停止给章编号。article 里这些全都没有,也就是一份带摘要、目录、引言和附录的报告通常该用 report 的老实理由。

    这些部件的先后次序是承重的。\pagenumbering{...}(也可以直接使用:roman、arabic、alph、Alph、fnsymbol)与 \setcounter{page}{1} 必须在 \tableofcontents 之前执行,因为目录携带的是每个标题被看到当时的页码。在目录之后才改编号样式,里面每个条目都差开一个节,而 LaTeX 不会警告你——只有下一遍才会让文件与文档重新达成一致。

    双面印刷补上布局的另一半:openright 让一章从正面(recto)页开始,而 \cleardoublepage 是手工到达那里的工具,它会把反面填成一页有意留空的纸。\newpage 和 \clearpage 是较弱的形式,而 \clearpage 是同时冲掉待处理浮动体的那一个——这就是以 \clearpage 收尾的一章会突然多出一页图的原因。

    页眉页脚是归文档类管的页面样式决定:\pagestyle{headings} 打开由 \markboth/\markright 供料的运行页眉,\thispagestyle{empty} 把当前页的页眉页脚去掉(题页的常规处理,也是 titlepage 选项替你做的事),而 \pagestyle{plain} 是标准类的默认:页码在底部居中,完全没有页眉。自定义页眉是 fancyhdr 或 KOMA-script 自带的 scrlayer-scrpage,而它那条著名的警告——headheight is too small——是一个该设的长度,不是一个该压掉的 bug。

    \documentclass[12pt,a4paper,twoside,openright]{book}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    \usepackage{graphicx}
    \usepackage{hyperref}
    
    \hypersetup{bookmarksnumbered=true}
    \setcounter{tocdepth}{1}       % contents lists chapters and sections only
    
    \begin{document}
    
    \frontmatter
    \title{A Thesis Skeleton}
    \author{A. Student}
    \date{\today}
    \maketitle
    \chapter*{Abstract}
    Roman page numbers belong to the front matter, so the page numbering style
    has to be switched \emph{before} the contents list is written.
    \tableofcontents
    \listoffigures
    \listoftables
    
    \cleardoublepage          % start the main matter on a right-hand page
    \mainmatter
    
    \part{Theory}
    \chapter{Definitions}
    \section{Scope}
    In the book class a section number carries its chapter: this heading is
    numbered \thesection, built from \verb|\thechapter.\arabic{section}|.
    \subsection{Terms}
    Below \texttt{tocdepth} = 1, so this subsection is numbered but is not
    listed in the contents.
    \begin{figure}[ht]
      \caption{A caption becomes a line in the list of figures.}
    \end{figure}
    
    \part{Practice}
    \chapter{Fieldwork}
    
    \appendix
    \chapter{Questionnaire}
    \section{Open items}
    Appendix chapters become letters, so this is section \thesection.
    
    \backmatter
    \chapter*{Bibliography}
    \addcontentsline{toc}{chapter}{Bibliography}
    
    \end{document}

    重排标题样式而不破坏编号

    错误做法人尽皆知而且总是诱人:\section{\Large\sffamily Method}。它把格式塞进标题文本,于是目录条目和 PDF 书签会继承这些字号与字族代码,运行页眉也可能继承,而标题也不再是一个能作为类样式来改的东西。你以那种方式能做的一切,排版宏包都能做到,而且不会漏进 .toc。

    titlesec 是通用答案:\titleformat{\section}[block]{\normalfont\LARGE\bfseries\sffamily}{\thesection}{1em}{} 点名布局、编号格式与编号同标题之间的间隙,而 \titlespacing*{\section}{0pt}{18pt plus 4pt minus 2pt}{6pt} 取代类的前后间距——其中可伸缩的部分正是防止页面在紧贴标题下方断掉的东西。\titlespacing 的星号形式在断页时丢掉后置空间,这恰恰是你要的行为,也恰恰是那件人们从不知道存在的事。

    KOMA-script 的类(scrartcl、scrreprt、scrbook)改用选项与元素字体来处理:\addtokomafont{section}{\sffamily}、\RedeclareSectionCommand[beforeskip=18pt,afterskip=6pt,indent=0pt,runin=false]{paragraph},以及 headings=small|normal|big、numbers=noenddot、chapterprefix 这类类选项。如果你是在为一篇长文档挑一个类,而不是给已有的类打补丁,这条路需要的补丁最少。

    如果唯一的改动是字族或字重,sectsty 两行就够(\allsectionsfont{\sffamily}、\sectionfont{\Large})。而如果改的是编号格式——去掉尾随的点、加一个前缀——那就重定义 \thesection,同时知道 \ref 读的是同一个宏;只想影响印出来的那个标题,就去覆盖 \@seccntformat,或者,再一次,让 titlesec 持有编号那一格。

    \documentclass[12pt]{article}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    
    % ---- Route 1: titlesec, works with any class -----------------------
    \usepackage{titlesec}
    \titleformat{\section}[block]{\normalfont\LARGE\bfseries\sffamily}
                {\thesection}{1em}{}
    \titleformat{\subsection}[block]{\normalfont\large\bfseries}
                {\thesubsection}{1em}{}
    \titlespacing*{\section}{0pt}{18pt plus 4pt minus 2pt}{6pt plus 2pt minus 2pt}
    \titlespacing*{\subsection}{0pt}{12pt plus 3pt minus 2pt}{4pt plus 1pt minus 1pt}
    
    % ---- Route 2: KOMA-script, when the class is scrartcl/scrbook -------
    % \addtokomafont{section}{\sffamily\MakeUppercase}
    % \RedeclareSectionCommand[beforeskip=18pt,afterskip=6pt,indent=0pt]{subsection}
    % \KOMAoptions{headings=normal,numbers=noenddot}
    
    % ---- Route 3: sectsty, the one-line version ------------------------
    % \usepackage{sectsty}
    % \allsectionsfont{\sffamily}
    % \sectionfont{\Large}
    
    \begin{document}
    
    \tableofcontents
    
    \section{Restyled heading}
    The class still owns the counter and the contents entry; \texttt{titlesec}
    only changed how the heading is drawn and how much space stands around it.
    That division of labour is the reason to use a heading-styling package
    instead of \texttt{\textbackslash Large\textbackslash bfseries} typed by hand.
    
    \subsection{A subsection}
    Notice that the number \ref{sec:two} is still produced by \thesubsection,
    because redefining \verb|\thesection| would have changed the cross-reference
    as well as the heading -- which is sometimes what you want, and usually not.
    \label{sec:two}
    
    \end{document}

    目录出错的四种症状与它们的成因

    条目缺失。三个成因,按可能性排序:标题带星号(于是根本不写条目)、层级低于 tocdepth、或者条目在但它的编号盒太窄、把标题挤出了那一行。前两个是计数器,第三个是 \l@<level> 的定义。验证计数器猜想:在正文里打印一次 \the\c@tocdepth;验证星号:在源码里搜 *{。

    页码不对,或者列表是空的。几乎总是遍数结构。目录是从上一遍写的文件读出来的,所以标题动过的文档需要再跑一遍——而带 \cleardoublepage、浮动体或 \pagenumbering 改动的文档,在收敛之前永远落后一遍。跑 latexmk,别自己数遍数;如果你在调的是别的东西,就手工 pdflatex 两遍。

    条目乱码,或者只在目录里报错。标题里有经不起「写进文件再读回来」的东西:数学、\verb、某个 fragile 命令。解法是给书签和标题用 \texorpdfstring{$E=mc^{2}$}{E = mc2},给列表用一个可选参数——\section[Plain title]{Title with maths}——以及在任何 fragile 的东西前面加 \protect。\chapter 标题里的数学是同一个问题带两个去处:.toc 那一行和书签。

    标题看起来不对、编号却对。那是样式问题,意味着格式进到了标题文本而不是类的钩子里——见上一节。四种症状共通的启发式:编号、列出、绘制在 LaTeX 里是三个独立机制,几乎每个分级 bug 都是从另一个的入口去改这一个。

    本课速查

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

    小节 关键代码
    \begin{array}{c|l|l|l}\text{level} & \text{command} & \text{counter} & \text{numbered by default} \\\hline 0 & \texttt{part / chapter} & \texttt{part, chapter} & \text{yes} \\ 1 & \texttt{section} & \texttt{section} & \text{yes} \\ 2 & \texttt{subsection} & \texttt{subsection} & \text{yes} \\ 3 & \texttt{subsubsection} & \texttt{subsubsection} & \text{article: yes} \\ 4 & \texttt{paragraph} & \texttt{paragraph} & \text{no} \\ 5 & \texttt{subparagraph} & \texttt{subparagraph} & \text{no}\end{array}
    \documentclass[11pt]{article}
    \begin{array}{l}\texttt{\textbackslash addcontentsline\{toc\}\{subsection\}\{\{Title\}\}\{\{2\}\}} \\ \texttt{\textbackslash addtocontents\{toc\}\{\textbackslash protect\textbackslash setcounter\{tocdepth\}\{1\}\}}\end{array}
    \documentclass[12pt,a4paper,twoside,openright]{book}
    \documentclass[12pt]{article}

    常见错误

    • 用 {\Large\bfseries Method\par} 伪造一个标题:没有计数器、没有目录条目、没有运行页眉,而且间距归到前面那个段落去了。用 \section{...},然后重排类的钩子。
    • 写了 \section*{} 然后到处找缺失的目录条目。星号一次性移走编号、目录行和页眉标记;补一条 \addcontentsline{toc}{section}{...},或者用 KOMA 的 \addsec。
    • 在标题里放数学却不用 \texorpdfstring 或一个可选的短标题,结果得到一份乱码的目录和坏掉的 PDF 书签。
    • 在小节标题里用 \verb:标题参数是移动参数,\verb 在那里无法变 robust。用带转义的 \texttt{},或者 cprotect 宏包。
    • 在 \tableofcontents 之后才设 \setcounter{page}{1} 或 \pagenumbering{arabic},于是目录里的每个数字都落后一种样式。
    • 在 article 文档里指望 \chapter——那条命令在那里不存在,而报错(Undefined control sequence)说的是文档类,不是缺某个宏包。
    • 因为结构失控就把 secnumdepth 抬到 5:四级编号已经超出读者能追踪的范围。把结构压平,而不是编得更深。

    关于本课的常见问题

    为什么我的 section 没出现在 LaTeX 目录里?
    要么它带星号(星号连目录行带编号一起压掉),要么它的层级比 tocdepth 更深。给带星号的标题用 \addcontentsline{toc}{section}{Title} 补一个条目,并在导言区用 \setcounter{tocdepth}{3} 把旋钮抬高。如果条目其实在、标题却看起来被截断了,那是该层的编号盒太窄,属于 \l@section 的样式问题而不是深度问题。
    secnumdepth 与 tocdepth 有什么区别?
    两者都是与每个分级命令的级别号比较的计数器:part 是 0、section 是 1、subsection 是 2、subsubsection 是 3、paragraph 是 4、subparagraph 是 5。secnumdepth 控制打印出来的编号有多深,tocdepth 控制目录列表有多深。它们互相独立,所以「编号但不列出」与「列出但不编号」都是正常需求。
    怎么把一个不编号的 section 放进目录?
    用星号写标题,然后自己报上去:\section*{Acknowledgements} 紧跟 \addcontentsline{toc}{section}{Acknowledgements}。在 KOMA-script 的类里同一件事是一条命令 \addsec{Acknowledgements},而且它连运行页眉也一起保住。要 book 类的章级条目,就把第二个参数写成 chapter。
    为什么编译完我的目录是空的?
    因为目录是由上一遍运行写入的 .toc 文件构建的,而第一次运行时那个文件还不存在。再编一次;如果标题或页码有变动,就再编一次。latexmk 通过观察输出是否还在变化来决定要跑几遍,这是跑带目录文档唯一理智的方式。
    怎么改 LaTeX 章节标题的字体或字号?
    用样式宏包而不是写进标题文本:titlesec 用 \titleformat 管布局、用 \titlespacing 管它周围的空间;KOMA-script 用 \addtokomafont{section}{...} 和 \RedeclareSectionCommand;sectsty 两行搞定字族与字重。写在 \section{...} 里面的格式会被目录条目和 PDF 书签继承,这就是它成为 bug 而不是风格的原因。
    part、chapter 和 section 有什么区别?
    它们是同一套编号方案里的 0、0 和 1 级,但各类不同:article 提供从 part 到 subparagraph 却没有 chapter,report 和 book 在 section 之上加了 chapter,book 还额外把一切包进部以及前置/正文/后置部分。按你需要的结构来挑类;这些层级本身在哪儿行为都一样。

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

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

    KaTeX 错误
    试一个例子

    继续读

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