\chapter{数学}

\TeX\ 原本是 Donald Knuth 为排版他的计算机算法专著而发明。他写的书属于数学类书籍，故而排版数学公式便成为 \TeX\ 最擅长的任务——这也是我对数学的唯一兴趣了，希望你是因为喜欢数学而喜欢 \TeX。近年来，\CONTEXT\ 对 \TEX\ 的数学公式的排版做出了许多显著的改进，俨然自成体系，限于能力，本章仅能略述一二。

\section{\TEX\ 风格}

\TeX\ 数学公式有两种模式，一种是正文模式（text mode），一种是显摆模式（display mode），在 \ConTeXt\ 中，自然也是如此。当然，可能你觉得显摆模式这个称谓不够严肃，于是随了它的俗称「行间模式」，并无不可，而且后文我也如此称谓它。

正文模式里的数学公式放在一对美元符号之间，据我猜测，意思是有钱人都精通数学，或者精通数学的人都会有钱。例如 \type{$\int_0^{+\infty}f(x) {\rm d}x$} 便是正文模式的数学公式，排版结果为 $\int_0^{+\infty}f(x) {\rm d}x$。\TeX\ 数学公式的行间模式是放在一对双美元符号之间，例如

\starttyping[option=TEX]
$$
\int_0^{+\infty}f(x) {\rm d}x
$$
\stoptyping

\noindent 排版结果为
\blank[halfline]
\noindent $$
\int_0^{+\infty}f(x) {\rm d}x
$$

\section{\CONTEXT\ 风格}

\CONTEXT\ 的数学排版兼容 \TEX\ 风格，但它另有一套功能更为丰富的语法。行内公式\index[hangneigongshi]{行内公式\crlf\tex{im}\crlf\tex{dm}\crlf\tex{m}}可以用 \type{\im{...}} 和 \type{\dm{...}} 命令，前者等效于 \TEX\ 的行内公式，后者则是以行间公式的形式构造行内公式。这两个命令的区别，从排版一个分数便可察知，例如 \type{\im{\frac{1}{2}}} 的结果是 \im{\frac{1}{2}}，而 \type{\dm{\frac{1}{2}}} 的结果是 \dm{\frac{1}{2}}。

还有一个可带有参数的行内公式命令 \type{\m{...}}，例如 \type{\m[color=blue]{a^2 + b^2}} 可构造一个蓝色的行内公式，结果为 \m[color=blue]{a^2 + b^2}。

\CONTEXT\ 的行间公式语法，其形式见下例：

\starttyping[option=TEX]
\startformula
\int_0^{+\infty}f(x) {\rm d}x
\stopformula
\stoptyping
\startformula
\int_0^{+\infty}f(x) {\rm d}x
\stopformula

\type{formula} 环境为行间公式提供了居中对齐以及前后留白，且能在其样式设定里控制行间公式前后的空白大小。下例将公式前后的留白设为 0 长度。

\starttyping[option=TEX]
\setupformulas[spacebefore=0pt,spaceafter=0pt]
\startformula
\int_0^{+\infty}f(x) {\rm d}x
\stopformula
\stoptyping

\begingroup
\setupformula[spacebefore=0pt,spaceafter=0pt]
\startformula
\int_0^{+\infty}f(x) {\rm d}x
\stopformula
%\setupformulas[spacebefore=,spaceafter=]
\endgroup

\section[placeformula]{序号}

\tex{placeformula} 可以让数学公式带有序号，且能使之支持引用，见下例。该命令有些像 \tex{placetable} 和 \tex{placefigure}，只是后两者皆为浮动对象，而数学公式不可浮动，也无需用花括号将其包裹起来作为 \tex{placeformula} 的参数。

\startTEX
\placeformula[formula-foo]
\startformula
\int_0^{+\infty}f(x) {\rm d}x
\stopformula
\stopTEX
\placeformula[formula-foo]
\startformula
\int_0^{+\infty}f(x) {\rm d}x
\stopformula

\indentation 用 \type{\in[formula-foo]} 命令可引用上例构造的数学公式的序号，结果为 \in[formula-foo]。需要注意的是，数学公式之后的第一个中文段落的首行缩进会被消除，若需缩进，可用 \tex{indentation}\index[indenting]{indenting + \tex{indentation}} 命令强行缩进，见下例。

\startTEX
\placeformula
\startformula c = \pm\sqrt{a^2 + b^2} \stopformula
公式之后的段落 ... ... ...
\placeformula
\startformula f(x) = \sin x \stopformula
\indentation 公式之后的段落 ... ... ...
\stopTEX

\startframedtext[width=\textwidth]
\setupindenting[first,always,2em]
\placeformula\startformula c = \pm\sqrt{a^2 + b^2} \stopformula
公式之后的段落 ... ... ...
\placeformula\startformula f(x) = \sin x \stopformula
\indentation 公式之后的段落 ... ... ...
\stopframedtext

请记住 \tex{noindent} 和 \tex{indentation} 这两个命令，在表格、插图以及数学公式之后的段落里，经常会用到它们。

由于 \tex{placeformula} 命令与 \type{formula} 环境的结合在形式上不够紧密，\CONTEXT\ 提供了 \type{placeformula} 环境，它与 \tex{placeformula} 命令等效。此外，默认的公式序号是小括号形式，但是你可以将其设定为你需要的形式，见下例。

\startTEX
\setupformula[left={[}, right={]}]
\startplaceformula[在此放置引用]
\startformula
e = mc^2
\stopformula
\stopplaceformula
\stopTEX

\setupformula[left={[}, right={]}]
\startplaceformula[在此放置引用]
\startformula
e = mc^2
\stopformula
\stopplaceformula

\section{定理和证明}

对于数学工作者而言，最重要的任务是发现数学定理，并给予证明。\CONTEXT\ 没有为数学家们提供定理及其证明的排版命令，但是提供了实现这类命令的机制，即枚举。\type{itemize} 环境实际上可谓是枚举的特例。使用 \tex{defineenumeration}\index[definenumeration]{\tex{defineenumeration}} 可以定义新的枚举特例，例如

\startTEX
\defineenumeration[theorem][text=定理]
\stopTEX

\noindent 然后便可使用该特例，如下

\startexample
\starttheorem
道可道也，非恒道也。
\stoptheorem
\starttheorem
名可名也，非恒名也。
\stoptheorem
\stopexample
\example[option=TEX][theorem-01]{定理环境}{\externalfigure[08/01.pdf][width=.3tw]}

\noindent 通过上例，可观察到，\type{theorem} 环境的序号会自动递增，这意味着，无论你发现了多少个数学定理，都可以反复使用该环境呈现它，只是其样式也许并非你想要的。

如果你希望定理的序号和内容的首行处于同一行，可将定理环境的样式设定为紧凑模式\index[setupenumeration]{\tex{setupenumeration}}，见下例。

\startexample
\setupenumeration[theorem][alternative=serried]
\starttheorem
道可道也，非恒道也。
\stoptheorem
\stopexample
\example[option=TEX][theorem-02]{紧凑的定理环境}{\externalfigure[08/02.pdf][width=.3tw]}

上例里的定理序号与内容之间的间距过大，只需对定理环境的宽度加以限制，便可消除该间距，见下例。

\startexample
\setupenumeration[theorem]
                 [alternative=serried, width=broad]
\starttheorem 道可道也，非恒道也。 \stoptheorem
\stopexample
\example[option=TEX][theorem-03]{限宽的定理环境}{\externalfigure[08/03.pdf][width=.3tw]}

\noindent 也可以将 \type{width} 设定为 \tex{textwidth}，与上例设定等效。至于为何会如此，以及 \CONTEXT\ 为何不将这些设定作为默认设定呢？我不知道，也许 \CONTEXT\ 开发者觉得，若一切都设定得太好，这是授人以鱼。

如果你希望定理的内容部分的字体也用粗体，可做以下设定：

\startTEX
\setupenumeration[theorem]
                 [alternative=serried, width=broad, style=bold]
\stopTEX

同理，基于枚举环境也能为定理的证明定义一个环境，只需去掉序号部分，并在证明的结尾靠版面的右侧放置 $\square$ 符号表示证毕，见下例。

\startexample
\defineenumeration[proof][text=证明]
\setupenumeration[proof]
                 [alternative=serried, width=broad,
                  number=no,
                  closesymbol={\m{\square}}]
\startproof
因为名可名也，非恒名也，所以道可道也，非恒道也。
\stopproof
\stopexample
\example[option=TEX][proof]{证明}{\externalfigure[08/04.pdf][width=.3tw]}

\subject{结语}

现在你已经基本学会了 \ConTeXt\ 数学公式排版。与数学排版专家相比，你缺乏的可能主要是如何熟练地输入各种符号以及各种具体形式的数学公式。若想在这方面能有所精进，可参考文档 \cite[math]。此外，在 \ConTeXt\ 系统的文档目录里，有一份详尽的数学公式排版手册，使用以下命令可以找到它。

\starttyping
$ mtxrun --search mathincontext-screen.pdf
\stoptyping
