定理与证明 (amsthm)

定理、引理、定义和证明都有不同于正文的固定外观:以 Theorem 3.1 这样的粗体标题开头,正文用斜体排版,编号自动递增,证明末尾带一个小空心方块(□)。手工维护这些格式很繁琐,因此 amsmath 的姊妹包 amsthm 提供了预先*声明*定理类环境的机制,并提供专用的 proof 环境。本页介绍用 \newtheorem 声明环境、共享计数器和按节编号、无编号变体、用 \theoremstyle 切换外观、proof 环境及其证明结束符号,以及更现代的高层接口 thmtools

声明定理环境

定理类结构先在导言区 声明为环境,然后在正文中使用该环境书写。声明使用 \newtheorem。第一个参数是 环境名(正文中写在 \begin{…} 里的名字),第二个参数是 输出标题中以粗体打印的词。例如下面的写法会启用一个名为 theorem 的环境。

latex
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}        % プリアンブルで宣言 / declare in the preamble

这样设置后,正文中每次使用 theorem 环境,都会自动得到粗体标题和连续编号,如 Theorem 1Theorem 2;其中的文字在默认样式下用斜体排版。编号由 LaTeX 计数器管理,所以在中间插入一个定理时,后面的编号会自动调整。

latex
\begin{theorem}
  素数は無限に存在する。
\end{theorem}

\begin{theorem}
  There are infinitely many primes.
\end{theorem}

注意,\newtheorem 也是 base LaTeX(标准 LaTeX2e)的一部分。单独的 \newtheorem 已经可以创建带编号的定理环境,但下面介绍的 样式切换(\theoremstyle)、无编号星号形式(\newtheorem*)、proof 环境和证明结束符号 都是加载 amsthm 后才可用的扩展。写数学文档时,通常会同时加载 amsthm(以及 amsmath)。

控制编号方式

如果定理、引理、推论各自独立编号,就会出现 “Theorem 1, Lemma 1, Theorem 2, Lemma 2…” 这样的重复编号,读者容易混淆。多数数学写作会让它们使用 共同编号,形成 “Theorem 1, Lemma 2, Theorem 3…” 这样的序列。\newtheorem可选参数(方括号) 可以精确控制这一点。关键在于方括号放置的位置不同,含义也不同。

共享计数器,就在新环境名的 正后方 用方括号写入一个已有环境名。下面的例子中,lemma 使用与 theorem 相同的计数器,因此定理和引理共用同一串编号。

latex
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}   % theorem と番号を共有 / share theorem’s counter

另一方面,要 在每章或每节重新编号,并把节号作为前缀,生成 “Theorem 2.1, Theorem 2.2” 这样的编号,就在第二个参数 之后 用方括号给出父计数器(如 section)。这样每进入新节,定理编号都会回到 1,并与节号组合显示。

latex
\newtheorem{theorem}{Theorem}[section]   % 節ごとにリセットし「2.1」形式 / reset per section, “2.1” style

两个方括号形式不能同时使用,但也可以让一个计数器从属于另一个环境。例如 \newtheorem{corollary}{Corollary}[theorem] 会让推论编号 每出现一个定理就重置,因此继承父定理编号,如 “Corollary 3.1(附属于 Theorem 3 的第一个推论)”。下表整理了方括号的位置及其效果。

声明效果
\newtheorem{theorem}{Theorem}独立的连续编号在自己的计数器上编号为 1, 2, 3, …
\newtheorem{lemma}[theorem]{Lemma}共享 theorem 计数器(同一序列)引理和定理共用一个编号序列
\newtheorem{theorem}{Theorem}[section]每节重置;显示为 “2.1”每个 \section 重新开始,并以前面的节号为前缀
\newtheorem{corollary}{Corollary}[theorem]每个 theorem 处重置;显示为 “3.1”从属于 theorem 计数器

无编号以及添加名称

对于不想编号的定理类环境,例如只出现一次的 “Remark” 或 “Main Theorem”,使用 amsthm 提供的 星号形式 \newtheorem*。它完全不分配编号,只打印标题词。语法与不带星号的 \newtheorem 相同,只是不能带与计数器有关的参数。

latex
\newtheorem*{remark}{Remark}   % 番号なし(amsthm が必要) / unnumbered (needs amsthm)

还可以给某个定理 附加专有名称或出处。在正文中打开环境时,在 \begin{theorem} 后面立即用方括号传入名称,它会显示在编号后面的括号中。例如下面的写法会生成类似 “Theorem 1 (Pythagorean theorem)” 的标题。

latex
\begin{theorem}[Pythagorean theorem]
  直角三角形において $a^2 + b^2 = c^2$ が成り立つ。
\end{theorem}

这个方括号 与环境是否编号无关。带编号的环境会得到 “编号 +(名称)”,用 \newtheorem* 创建的无编号环境会得到 “标题词 +(名称)”;两种情况下名称都会作为括号内容附加。

选择外观:\theoremstyle

amsthm 提供 \theoremstyle 来切换定理类环境的外观。内置样式有三种,区别在于标题(head)和正文(body)的字体组合。关键点是:\theoremstyle{…} 会应用到它之后声明的所有 \newtheorem。因此应在 \newtheorem 声明 之前 设置样式,并按样式把声明分组。不设置时默认是 plain

  • plain:标题为粗体,正文为斜体。用于需要强调陈述的命题,如定理、引理、命题、推论。不设置时的默认样式。
  • definition:标题为粗体,正文为直立(罗马)体。用于希望按普通正文阅读的内容,如定义、例子、问题、条件。
  • remark标题为斜体,正文为直立体。用于补充性的文字,如评注、注记、claim。

下面的导言区是一个典型配置:同时使用三种样式,并让 theorem、lemma、corollary 共用一个计数器。在 \theoremstyle{plain} 下声明 theoremlemmacorollary;在 \theoremstyle{definition} 下声明 definitionexample;在 \theoremstyle{remark} 下声明 remark

latex
\usepackage{amsmath, amsthm}

\theoremstyle{plain}              % 本文イタリック / italic body
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}

\theoremstyle{definition}        % 本文立体 / upright body
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}

\theoremstyle{remark}            % 見出しイタリック / italic head
\newtheorem*{remark}{Remark}

在这个设置中,theoremlemmacorollarydefinitionexample 都共享按节重置的一套编号(如 “Theorem 2.1, Definition 2.2, Lemma 2.3…”);定理、引理、推论的正文为斜体,定义和例子的正文为直立体。只有 remark 无编号,其 “Remark” 标题为斜体。注意声明位置:如果把 \theoremstyle 放在某个 \newtheorem 后面,该声明不会受影响,而会保留此前的样式(若此前没有设置则为 plain)。

proof 环境与 QED 符号

amsthm 还提供专用的 proof 环境(同样不属于 base LaTeX,而是 amsthm 的功能)。proof 环境会在开头自动放置 斜体 “Proof.” 标题,并在末尾自动加上 证明结束符号,默认是一个小的 空心方块(□,也称 tombstone 或 QED 符号)

latex
\begin{proof}
  $\sqrt{2}$ が有理数だと仮定して矛盾を導く。……よって $\sqrt{2}$ は無理数である。
\end{proof}

标题词可以更改。proof 环境也接受逐个实例的方括号参数,因此写 \begin{proof}[Proof of Theorem 1] 会把标题替换为 “Proof of Theorem 1.”(也可以写成如 \begin{proof}[Proof of the lemma])。

证明结束符号的位置需要注意。amsthm 会尝试把符号放在正文最后一行的 行末,但如果证明 以陈列公式或列表环境结束,符号可能会掉到单独一行。处理方法是在希望符号出现的位置显式放置 \qedhere。例如把它放在陈列公式末尾,□ 就会贴到该公式行的右端。

latex
\begin{proof}
  両辺を整理すると、次を得る。
  \[
    a^2 + b^2 = c^2. \qedhere
  \]
\end{proof}

符号本身也可以替换。重新定义 \qedsymbol 后,可以使用实心方块(■)、文本字符串或任意结束标记;若要完全移除它,就把它重定义为空。在导言区写入如下内容即可。

latex
\renewcommand{\qedsymbol}{$\blacksquare$}   % 黒い四角に / filled square
% \renewcommand{\qedsymbol}{}              % 記号を消す / remove the symbol

thmtools:现代高层接口

随着声明增多,通过 \newtheorem 方括号的位置来指定编号会变得难读。thmtools 是位于 amsthm(或 ntheorem之上的高层接口,提供 \declaretheorem,让所有设置都以 key=value 选项给出。thmtools 本身不拥有编号机制;它把 amsthm\newtheorem 作为后端调用,因此需要与 amsthm 一起加载。

最简单时只传入环境名,编号控制用键来写。从属于某节用 numberwithin=(同义的 parent=within= 也可);共享计数器用 sibling=(同义的 numberlike=sharecounter= 也可);无编号用 numbered=no;标题词用 name=(同义的 title=heading=);样式用 style=。下面的例子用 thmtools 写出与上一节 amsthm 版本几乎相同的设置。

latex
\usepackage{amsthm, thmtools}

\declaretheorem[numberwithin=section]{theorem}
\declaretheorem[sibling=theorem]{lemma}
\declaretheorem[style=definition, sibling=theorem]{definition}
\declaretheorem[numbered=no, name=Remark]{remark}

thmtools 的优势在于,它能快速使用单独 amsthm 中比较麻烦的高级功能。典型例子是 重述(restatable)。加载 thm-restate 包(随 thmtools 附带),并在 restatable 环境中书写定理时,其内容会 保存为宏,从而可以在后面的章节中 以原编号重新列出同一定理restatable 的第一个参数(可省略)是名称,第二个参数是要使用的定理环境,第三个参数是 记住内容的宏名

latex
\usepackage{amsthm, thmtools, thm-restate}
\declaretheorem{theorem}

% 本編:内容を \firsteuclid に保存 / store the content in \firsteuclid
\begin{restatable}[Euclid]{theorem}{firsteuclid}
  \label{thm:euclid}
  There are infinitely many primes.
\end{restatable}

% 付録などで同じ定理・同じ番号を再掲 / restate later with the same number
\firsteuclid*

之后展开已保存的宏 \firsteuclid 时,该定理会 以原编号 再次排版(编号固定在原出现处,内部的陈列公式编号等也使用原来的编号)。加上星号,如 \firsteuclid*,可以在重述时让交叉引用(\label/\ref)继续指向 原声明处而不是重述副本。当你想在正文中带编号陈述结论,而把证明放到附录时,这很有用。

另一个方便功能是 定理列表(list of theorems)。写入 \listoftheorems 后,会像目录一样自动生成定理、引理等的列表。可以先用 ignoreall 排除所有项目,再用 show={…} 列出要包含的环境名来筛选。使用 onlynamed(或 onlynamed={…})时,只列出像 \begin{…}[name] 那样 带有名称的定理。如果同时加载了 hyperref,列表中的每一项都会成为指向对应定理正文的 可点击链接

latex
\listoftheorems                                  % すべて / everything
\listoftheorems[ignoreall, show={theorem, lemma}]   % theorem と lemma だけ / only these

如果想自定义外观,可以用 thmtools\declaretheoremstyle 定义新样式,并通过 \declaretheorem[style=…]{…} 调用。标题字体(headfont)、正文字体(bodyfont)、前后间距(spaceabovespacebelow)、备注外的括号(notebraces)、标题与正文之间的间距(postheadspace)、结束符号(qed)等都可以用键细致指定。若普通 amsthm 已足够,就不必勉强使用;但在整理复杂定理环境时,它是很有力的选择。