三个环境,一个内核宏,一条规则
itemize、enumerate 和 description 就是基础系统里 LaTeX 列表词汇的全部。三者都是内核宏 \list 外面的一层薄壳,所以它们共用同一套缩进、同一套间距、同一串报错:itemize 提供一个符号,enumerate 提供一个计数器,description 则拿你在每个 \item 方括号里写进去的东西当标签。
唯一的规则是:列表的体必须由条目构成。每个条目以 \item 开头,任何第一个内容不是 \item 的环境——包括一个彻底空着的列表——都会抛出 LaTeX 里最让人摸不着头脑的错误:! LaTeX Error: Something's wrong--perhaps a missing \item. 这句话没冤枉人,起因却通常不是它字面说的那件事:一个多余的空行、一个被注释掉的 \item、或者紧跟 \begin{document} 开一个里面还没东西的列表,报的都是同一句。
选哪一个,取决于你之后想对这个列表说什么。没有顺序需要被引用的集合用 itemize;条目需要被交叉引用就用 enumerate,因为条目带着计数器,\label{itm:power} 配上 \ref{itm:power} 就能解析成印出来的那个编号。术语配释义的材料用 description,那里的标签是内容,而不是系统生成的记号。
还有第四种情形,新手常拿列表去解而其实不该:两栏的简短释义、一张答题纸、或者横铺整页的清单。那些是表格,或者是 tasks 宏包,不是嵌套的项目符号;本课最后一节会回到这个问题。
\begin{array}{l|l}\text{environment} & \text{what it contributes} \\\hline \texttt{itemize} & \text{a symbol, no counter} \\ \texttt{enumerate} & \text{a counter you can \textbackslash ref} \\ \texttt{description} & \text{a label you type per item}\end{array}
enumerate 持有计数器,所以也只有 enumerate 的条目值得被交叉引用。标签的梯子:四层与底下的计数器
标准文档类给每个列表留四层嵌套,每层有自己的预设标签。article 里的圆点梯是:第一层实心圆点,第二层加粗的短横线,第三层居中的星号,第四层居中的句点;report 和 book 第一层换成破折号,这就是同一份源码在论文类里长相不同的原因。编号梯则是 1.、(a)、iii.、A.。
这些标签不是硬编码的字符串,而是成对的宏。计数器是 enumi、enumii、enumiii、enumiv;\theenumi 规定计数器怎么读(\arabic{enumi}),\labelenumi 规定它怎么印在条目前面(\theenumi.)。itemize 这边对应的是 \labelitemi 到 \labelitemiv,背后没有计数器。
一旦你给条目加 \label,这个分裂立刻重要起来,因为 \ref 打印的是 \p@enumii\theenumii——父级前缀(标准类里为空)加上当前值。所以只重定义 \labelenumi 会改掉标号却把交叉引用留在旧样式里;重定义 \theenumi 则两者同变,当你希望子条目的引用写成「2.3」而不是「c」时,这正是你要的效果。两个宏分开的设计就是为了被这样使用:\renewcommand{\theenumii}{\theenumi.\arabic{enumii}},接着 \renewcommand{\labelenumii}{(\theenumii)}。
嵌套深度是被检查而不是被吸收的:任一环境的第五层会让编译停在 ! Too deeply nested. 上。想让列表不从 1 开始,就设对应层级的那个计数器——\begin{enumerate}\setcounter{enumi}{4} 让第一个条目编号为 5;而光一个 \begin{enumerate} 本来就会把 enumi 归零,归零与设值走的是同一套机制。
\begin{array}{c|c|c|c}\text{depth} & \text{counter} & \text{\textbackslash the…} & \text{printed label} \\\hline 1 & \texttt{enumi} & \texttt{\textbackslash arabic\{enumi\}} & \texttt{1.} \\ 2 & \texttt{enumii} & \texttt{\textbackslash alph\{enumii\}} & \texttt{(a)} \\ 3 & \texttt{enumiii} & \texttt{\textbackslash roman\{enumiii\}} & \texttt{iii.} \\ 4 & \texttt{enumiv} & \texttt{\textbackslash Alph\{enumiv\}} & \texttt{A.}\end{array}
enumitem:给单个列表传选项,而不是全局重定义
\usepackage{enumitem} 是标准升级,而且它换掉的是接口本身:\begin{enumerate}[label=(\alph*),leftmargin=*] 定制的是这一个列表,* 是当前计数器值的占位符。这才是人们通常真正想要的;\renewcommand{\labelenumi} 改的是全文所有列表,对下一个来编辑这份文件的人完全不可见。加载时加上 shortlabels 选项,还额外能用更短的 \begin{enumerate}[(a)] 写法。
最常用的键列在下面的表里。label 和 ref 分别顶替 \labelenumi 与 \theenumi;label* 在父标签之后追加,于是 1.1、1.2 不必自己写父级那一段;start 与 resume/series 管计数器;noitemsep 和 nosep 是现成的间距预设;wide 恢复标准类的宽松间距;align=left 与 align=parbox 决定标签宽过分配给它的 \labelwidth 时怎么处理。
全文默认是「设」出来的而不是「重定义」出来的:\setlist[enumerate,1]{label=\arabic*.} 指「每个 enumerate 的第一层」,\setlist[itemize]{topsep=4pt} 指「所有层级的每个 itemize」,光秃秃一句 \setlist{noitemsep} 指文档里每个列表。因为它们属于导言区的声明,这也是改列表间距最诚实的路子:标准类是在列表开始的那一刻从自己的 \@listi、\@listii 宏里取 topsep、parsep、itemsep 的,所以在导言区写 \setlength{\topsep}{…} 会被悄悄丢掉。
还有两个开关,值得在你自己把它们发明出来之前知道。\usepackage[inline]{enumitem} 增加 itemize*、enumerate*、description*:能跑在段落内部的列表,这正是「我们测试了 (a) 速度、(b) 成本与 (c) 重量」这类句子的正确工具,不必手写字母。而 \newlist{checklist}{itemize}{2} 是在既有环境之上声明一个新名字,带自己的深度和自己的样式,于是稿件规范不必渗进正文。
\begin{array}{ll}\texttt{label} & \text{what prints before each item} \\ \texttt{ref} & \text{what \textbackslash ref\ shows for that item} \\ \texttt{start} & \text{first value of the counter} \\ \texttt{series} & \text{a named list you can resume later} \\ \texttt{leftmargin=*} & \text{flush with the text block} \\ \texttt{noitemsep} & \text{itemsep and parsep to zero}\end{array}
\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amssymb} % for \square
\usepackage[inline,shortlabels]{enumitem}
\setlist[enumerate,1]{label=\textbf{\arabic*.},leftmargin=*}
\setlist[enumerate,2]{label=\textbf{\theenumi.\arabic*}}
\setlist[itemize]{topsep=4pt,itemsep=2pt,parsep=0pt}
\setlist[description]{style=nextline}
\newlist{checklist}{itemize}{2}
\setlist[checklist,1]{label=$\square$,leftmargin=*}
\newlist{questions}{enumerate}{2}
\setlist[questions,1]{label=Q\arabic*,ref=Q\arabic*}
\setlist[questions,2]{label=(\alph*),ref=\arabic{questionsi}(\alph*)}
\begin{document}
\section*{Per-instance options}
\begin{enumerate}[label=\textbf{\arabic*.},start=3]
\item Third in the sequence, because \texttt{start=3}.
\item \begin{enumerate}[label*=\arabic*] % label* appends to the parent
\item prints 3.1, where \texttt{label=\arabic*} on its own would
have restarted at 1.
\end{enumerate}
\end{enumerate}
\begin{checklist}
\item Power budget signed off
\item Rack space measured
\end{checklist}
\begin{questions}
\item Which port is upstream? \label{q:one}
\begin{questions}
\item Justify with the diagram. \label{q:onea}
\end{questions}
\item See question~\ref{q:one} and part~\ref{q:onea}.
\end{questions}
\section*{Inline lists}
Three candidates were screened \begin{enumerate*}[label=(\roman*)]
\item by price \item by lead time \item by warranty
\end{enumerate*}, and only the last two survived.
\section*{Broken across a page}
\begin{enumerate}[series=steps]
\item Unpack
\item Mount
\end{enumerate}
Text on the next page continues the same numbering:
\begin{enumerate}[resume*=steps]
\item Commission
\end{enumerate}
\end{document}
description:标签是内容,不是计数器
description 环境的定义是 \list{}{\labelwidth\z@ \itemindent-\leftmargin \let\makelabel\descriptionlabel}。照字面读就能看出两个后果。第一,\labelwidth 是零:标签不被放进固定宽度的盒子,它排完之后文字紧接着走,于是一个长术语会把该条目的文字往右推,而邻居们留在原位。第二,\itemindent 是 \leftmargin 的负值,这正是把标签拖回到版心边上的机制。
内核的 \descriptionlabel 是 \hspace\labelsep\normalfont\bfseries #1:描述列表的标签之所以是粗体,全部理由就在这儿;重定义它,就能换成斜体、小型大写或者全大写。有了 enumitem,同一件事变成一个键——\setlist[description]{font=\normalfont\itshape}——或者是现成的 style=nextline,把释义放到术语下面而不是旁边,那正是让记号表变得可读的选项。
标签里可以放任何在段落里能用的东西,包括数学:\item[$\alpha$-decay] …。不太放得下的是脚注和 \verb,因为在若干上下文里方括号参数属于移动参数;想放看起来像代码的术语,安全的选择是 \texttt{}。
\item[] 配一个空的方括号参数是一条真正的惯用法,不是笔误:它产出一个没有标签的条目,于是在一个像标题的术语下面续写段落时,既保留 description 的缩进又去掉那个粗体词——这是标准做法。
决定列表形状的那几个寄存器
水平方向上,一个列表由几个长度决定:\leftmargin(以及按层分的 \leftmargini 到 \leftmarginiv)把整块从版心往里移进来;\labelwidth 是标签所在的盒子;\labelsep 是标签与文字之间的缝;\itemindent 在此基础上再移动每个条目的首行;\listparindent 是条目内部第二个段落的缩进。\rightmargin 从右侧取走宽度。enumitem 把它们逐个用同名键暴露出来。
垂直方向有四个距离,把它们搞混是最常见的间距抱怨:\topsep 是整个列表上下的胶;\partopsep 是额外加的胶,只在 \begin 前面有空行、也就是列表另起段落时才出现;\itemsep 在条目之间;\parsep 在一个条目内部的段落之间。第一层上 article 大约给 8pt 的 \topsep 与 4pt 的 \itemsep、\parsep,全部带伸缩量,而越深的层级越接近零——这就是第四层列表看上去挤的原因。
因为这些值是 \@listi 及其同伙在列表启动那一刻赋进去的,所以 enumitem 是改它们的干净路子(\setlist[enumerate]{topsep=6pt}),给类宏打补丁是脏路子。唯一的例外是 \parskip:它是真正全局的,设了非零 \parskip 的文档会看到自己的列表把它继承过去,除非文档类或 parskip 宏包重排了列表参数来补偿。
一个条目本身就是独立的段落上下文,所以条目内部的空行不是错误——它是第二个段落,代价是 \parsep,并按 \listparindent 缩进。但父 \item 与它所拥有的嵌套 \begin{itemize} 之间那个空行,确实会造出一个空段落和没人要的缝隙:把嵌套列表写在拥有它的那个条目同一逻辑行上。
\begin{array}{c}\texttt{partopsep}\quad\text{list starts a new paragraph} \\ \hline \texttt{topsep}\quad\text{above and below the whole list} \\ \hline \texttt{itemsep}\quad\text{between two items} \\ \hline \texttt{parsep}\quad\text{between two paragraphs inside one item}\end{array}
什么时候列表是错的工具
四十个术语塞进 description 会在两个地方崩:长术语撞上文字(如上节所述),以及整块无法按栏扫读。替代方案是记号表用两栏的 longtable 或 tabularx,短条目用两三栏排版。tasks 宏包正是为此存在:\begin{tasks}(3) 配每行一个 \task,把编号或圆点列表横铺成三栏并保持阅读顺序的编号,这正是一张练习纸要的东西。
任何有视觉结构而非顺序的东西——一份表单、一个地址块、一行签名——正确的环境是 tabular、quote,或者 center 块里显式的 \\ 换行,而不是一串单词条目的列表。列表自带边距与垂直胶;这两样你都不想要,那你要的其实是段落。
做清单别用 itemize 加手打的 [ ] 去伪造方框:方括号在某些上下文里会被读成方括号参数,而且样式无法统一。应该声明一个列表——\newlist{checklist}{itemize}{1} 配 \setlist[checklist,1]{label=$\square$,leftmargin=*},并加载 amssymb——那个方框就成了粗细与尺寸都正确的一个字形。
最后,如果所谓「列表」其实是一段会被别处按编号引用的步骤序列,那么 enumerate 配 ref/label 才是工具,编号本身就是交付物;如果它只是读起来像有编号的序列,用 label=\textbf{\arabic*.} 的圆点也可以,但 enumerate 对这件事更诚实。
一个完整示例,逐行看
下面的清单是标准类的一次巡游:它重定义一个 itemize 标签和两个 enumerate 宏,让子条目的引用打印成「1.2」而可见标签打印成「(1.2)」;它在 \begin{enumerate} 之后设 enumi,让列表从三开始;它一路嵌到第五层并把失败那一行注释掉;还用 description 做了一小段词汇表。
里面有两个细节很容易错过,而恰恰值得照抄。一是 \item Cooling, as item~\ref{itm:psu} requires:那个连字符让 item 与它的编号待在同一个行单位里,换行无法把词与引用拆开。二是被注释掉的第五层和紧跟其后的报错文字——文件能编译,把注掉的那行打开,就是一次九十秒的读日志练习。
把它和第三节里的 enumitem 清单对照:同样的文档形状,改成按列表传选项、一个名叫 checklist 的列表、一个两层的 questions 列表(它的 ref 能取到 \newlist 为它创建的父计数器 questionsi)、句子内部一个行内的 enumerate*,以及一对 series/resume* 跨过中间一段文字把编号续上。标准类版本需要在导言区写四行 \renewcommand,enumitem 版本为了同一个局部决定一行导言区都不必写——改到第三稿时,你会在这一点上感到差别。
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
% ---- labels and counters of the standard classes -------------------
% level 1: 1. level 2: (a) level 3: iii. level 4: A.
% itemize: bullet, endash, centred asterisk, centred period
\renewcommand{\labelenumi}{\theenumi)} % 1) instead of 1.
\renewcommand{\theenumii}{\theenumi.\arabic{enumii}} % refs read "1.2"
\renewcommand{\labelenumii}{(\theenumii)} % labels read "(1.2)"
\renewcommand{\labelitemii}{$\circ$} % a math ring as a bullet
\begin{document}
\section{Requirements}
\begin{enumerate}\setcounter{enumi}{2} % this list starts at 3)
\item Power supply \label{itm:psu}
\item Network
\begin{itemize}
\item Wired
\item Wireless
\begin{itemize}
\item 2.4\,GHz only, no WPA3 support in the older access points
\begin{itemize}
\item a fourth level is the last one the standard classes allow
% \begin{itemize}
% \item a fifth level stops the run with the message
% ! Too deeply nested.
% \end{itemize}
\end{itemize}
\end{itemize}
\end{itemize}
\item Cooling, as item~\ref{itm:psu} requires
\end{enumerate}
\section{Definitions}
\begin{description}
\item[Baseline] the invisible line the letters sit on; descenders go below it.
\item[Leading] the distance from one baseline to the next, stored in
\verb|\baselineskip|.
\item[Overfull] a line TeX could not fit; it sticks into the margin instead
of hyphenating.
\end{description}
An empty list is an error, not an empty space on the page:
% \begin{itemize}
% \end{itemize}
% ! LaTeX Error: Something's wrong--perhaps a missing \item.
\end{document}
本课速查
每节抽一行关键代码:点小节名跳到对应位置,代码一键复制。
\begin{array}{l|l}\text{environment} & \text{what it contributes} \\\hline \texttt{itemize} & \text{a symbol, no counter} \\ \texttt{enumerate} & \text{a counter you can \textbackslash ref} \\ \texttt{description} & \text{a label you type per item}\end{array}
\begin{array}{c|c|c|c}\text{depth} & \text{counter} & \text{\textbackslash the…} & \text{printed label} \\\hline 1 & \texttt{enumi} & \texttt{\textbackslash arabic\{enumi\}} & \texttt{1.} \\ 2 & \texttt{enumii} & \texttt{\textbackslash alph\{enumii\}} & \texttt{(a)} \\ 3 & \texttt{enumiii} & \texttt{\textbackslash roman\{enumiii\}} & \texttt{iii.} \\ 4 & \texttt{enumiv} & \texttt{\textbackslash Alph\{enumiv\}} & \texttt{A.}\end{array}
\documentclass[10pt]{article}
\begin{array}{c}\texttt{partopsep}\quad\text{list starts a new paragraph} \\ \hline \texttt{topsep}\quad\text{above and below the whole list} \\ \hline \texttt{itemsep}\quad\text{between two items} \\ \hline \texttt{parsep}\quad\text{between two paragraphs inside one item}\end{array}
\documentclass[11pt]{article}
常见错误
- 在没加载
enumitem的文档里写\begin{enumerate}[a)]:标准环境不吃方括号参数,那串字符被当作普通内容排在列表前面,标签照旧是 1.、2.、3.。 - 删掉唯一的
\item,或者留一个空列表,然后把Something's wrong--perhaps a missing \item当成文档类里的 bug。这个环境确实要求至少一个条目体。 \item与它所拥有的嵌套\begin{itemize}之间空一行:嵌套列表于是另起一个段落,父条目前面就多出一行\parsep的空隙。- 写了
\renewcommand{\labelenumi}{\arabic{enumi})},然后纳闷\ref为什么还是旧样式。标签来自\labelenumi,引用来自\theenumi。 - 嵌到第五层然后怪文档类。要么把结构压平,要么用
\setlistdepth{9}配\renewlist{itemize}{itemize}{9}抬高上限,并定义\labelitemv。 - 想用导言区里的
\setlength{\topsep}{0pt}收紧列表。列表开始时会从\@listi重新取\topsep;要用\setlist{topsep=0pt}或noitemsep。 - 用
\\而不是\item分隔条目:整份列表被塞进一个条目,所有标签一起丢掉。
关于本课的常见问题
怎么把 LaTeX 的 enumerate 编号改成字母?
\begin{enumerate}[label=\alph*)] 得到 a) b) c),label=\textbf{\arabic*.} 得到加粗数字。没有这个宏包就得在导言区重定义类宏:\renewcommand{\labelenumi}{\alph{enumi})}——但它会改掉全文所有顶层列表,而且想让 \ref 也印成新样式还得连 \theenumi 一起重定义。怎么让编号列表从 5 开始?
\begin{enumerate}\setcounter{enumi}{4},第一个条目就印成 5,因为计数器是在每个标签排版之前步进的。有了 enumitem,同一件事变成一个键:\begin{enumerate}[start=5]。要接上前一个列表的编号,就给两者同一个 series= 名字,第二个用 resume*= 打开。Something is wrong, perhaps a missing item 这句报错是什么意思?
\end 之前没找到 \item。空列表、条目被注释掉的列表、以及不知怎么跑进别的命令参数里的 \item,报的都是这一句。删掉这个环境或者补上条目;这个错误与页面上别的东西无关。怎么去掉 LaTeX 列表条目之间的空隙?
\setlist{noitemsep} 全局设置;nosep 会连列表上下的空隙一起去掉。这些键把 \itemsep 与 \parsep 设成零,而正是这两个寄存器造出那道缝。LaTeX 的列表允许嵌套几层?
\setlistdepth 抬高上限,再声明新层级长什么样,但四层圆点已经超出读者能跟住的范围,先把它当结构问题来看。itemize 和 description 有什么区别?
\labelitemi 到 \labelitemiv;description 印的是每个 \item 的方括号参数,由 \descriptionlabel 排版,内核默认是粗体。description 不给标签分配固定宽度,所以长术语会把自己的文字往右推,除非改用 enumitem 的 style=nextline。实时演示:直接编辑 LaTeX,立刻看渲染结果
预览随输入实时更新——不妨故意写坏一次,看看报错长什么样。
继续读
同一个东西的三个入口:记法本身、讲它的那一课、以及真正用它写东西的一篇文章。