對數學公式的排版, 可以說是 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*}
來書寫, 效果相同, 運行結果為
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,未經本人允許,禁止轉載。