LaTeX数学公式编辑(1)——行内公式&行间公式


对数学公式的排版, 可以说是 LaTeX 中最精彩的部分.

首先说需要注意的问题. 数学公式中有时候会出现文字(中文或英文),
需要将文字用命令\text{...}包起来. 如果将文字不加处理, 直接写到公式里面会出现如下问题:

  • 如果是中文, 则不显示, 例如代码$根号9=3$编译之后显示为 \(9=3\), 而代码$\text{根号}9=3$ 编译之后显示为 \(\text{根号}9=3\).
  • 如果是英文, 虽然显示, 但显示为斜体 (行文中出现的英文一般为正体).

现在来看怎么排版数学公式. 数学公式多种多样, 但总的说起来, 可以分为行内公式和行间公式.

1. 行内公式

行内公式统一使用$...$来表达. $前后一般要有空格, 除非公式后面有标点符号.

2. 行间公式

对于行间公式, 要么编号, 要么不编号; 要么单行, 要么多行. 通过组合之后可知行间公式有四种: 单行编号、单行不编号、多行编号和多行不编号. 下面对每种公式的写法逐一介绍.

2.1 单行编号

单行编号的公式示例:

\begin{equation}\label{...}
\int_0^1f(t)dt = \iint_Dg(x,y)dxdy.
\end{equation}

运行结果为:
\begin{equation}
\int_0^1f(t)dt = \iint_Dg(x,y)dxdy.
\end{equation}
其中\label{...}的花括号中的内容可以使字母、数字、点号和减号的组合(例如\label{sec-2.3}), 用来对公式进行引用, 引用的命令为\eqref{...}.

2.2 单行不编号

不编号公式应避免使用 $$...$$, 因为它可能会产生不良间距. 可以用

\[
\int_0^1f(t)dt = \iint_Dg(x,y)dxdy.
\]

简单书写, 也可用

\begin{equation*}
\int_0^1f(t)dt = \iint_Dg(x,y)dxdy.
\end{equation*}

来书写, 效果相同, 运行结果为

\[\int_0^1f(t)dt = \iint_Dg(x,y)dxdy. \]

2.3 多行编号

多行编号的公式示例1 (注意对齐符号&的位置):

\begin{equation}\label{...}
\begin{split}
\frac{1}{2} (\sin(x+y) + \sin(x-y)) =& \frac{1}{2}(\sin x \cos y + \cos x \sin y)\\
& + \frac{1}{2} (\sin x \cos y - \cos x \sin y)\\
=& \sin x \cos y.
\end{split}
\end{equation}

运行结果为:

\begin{equation} \begin{split} \frac{1}{2} (\sin(x+y) + \sin(x-y)) =& \frac{1}{2}(\sin x \cos y + \cos x \sin y)\\ & + \frac{1}{2} (\sin x \cos y - \cos x \sin y)\\ =& \sin x \cos y. \end{split} \end{equation}

多行编号的公式示例2:

\begin{equation}\label{...}
\begin{dcases}
\frac{dS}{dt} = \Lambda - \beta SI - \mu S -\mu_1 mZS + \delta_0R, \\
\frac{dI}{dt} = \beta SI - (\mu+\delta+\gamma)I.
\end{dcases}
\end{equation}

运行结果为:

\begin{equation} \begin{cases} \frac{dS}{dt} = \Lambda - \beta SI - \mu S -\mu_1 mZS + \delta_0R, \\ \frac{dI}{dt} = \beta SI - (\mu+\delta+\gamma)I. \end{cases} \end{equation}

多行编号的公式示例3:

\begin{equation}\label{...}
D(x) =
\begin{cases}
1, & \text{if } x \in \mathbb{Q};\\
0, & \text{if } x \in \mathbb{R}\setminus\mathbb{Q}.
\end{cases}
\end{equation}

运行结果为:

\begin{equation} D(x) = \begin{cases} 1, & \text{if } x \in \mathbb{Q};\\ 0, & \text{if } x \in \mathbb{R}\setminus\mathbb{Q}. \end{cases} \end{equation}

2.4 多行不编号

可以在第2.3节命令的基础上将各个命令外层的\begin{equation}...\end{equation} 改为\begin{equation*}...\end{equation*} 使得公式不编号. 也可以用

\begin{align*}
\frac{1}{2} (\sin(x+y) + \sin(x-y)) =& \frac{1}{2}(\sin x \cos y + \cos x \sin y)\\
& + \frac{1}{2} (\sin x \cos y - \cos x \sin y)\\
=& \sin x \cos y.
\end{align*}

来排版此类公式. 运行结果为:

\begin{align*} \frac{1}{2} (\sin(x+y) + \sin(x-y)) =& \frac{1}{2}(\sin x \cos y + \cos x \sin y)\\ & + \frac{1}{2} (\sin x \cos y - \cos x \sin y)\\ =& \sin x \cos y. \end{align*}

3. 说明

本文中只介绍了数学公式的基本写作框架, 具体细节请参看文献[1,2].
文中如有不妥之处,肯请指教.

4. 参考文献

[1] 刘海洋. LaTeX 入门 [M]. 北京: 电子工业出版社, 2013.
[2] 胡伟. LaTeX 2e完全学习手册(第二版). 北京: 清华大学出版社, 2013.

本文为Beta2187原创博文,地址为https://www.cnblogs.com/beta2187/p/15476061.html,未经本人允许,禁止转载。

posted @ 2021-10-28 15:50  Beta2187  阅读(3418)  评论(0编辑  收藏  举报