amsmath + amssymb + mathtools — 数学排版
CTAN: https://ctan.org/pkg/amsmath | https://ctan.org/pkg/mathtools
手册: texdoc amsmath,texdoc mathtools
设置
\usepackage{amsmath} % core math environments
\usepackage{amssymb} % extra symbols (loads amsfonts)
\usepackage{mathtools} % fixes + extensions for amsmath (loads amsmath)
\usepackage{amsthm} % theorem environments
% mathtools loads amsmath, so you can just use:
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsthm}
公式环境
单个公式
% Numbered
\begin{equation}
E = mc^2
\label{eq:einstein}
\end{equation}
% Unnumbered
\begin{equation*}
E = mc^2
\end{equation*}
% or: \[ E = mc^2 \]
align — 多个对齐公式
\begin{align}
f(x) &= x^2 + 2x + 1 \label{eq:f} \\
&= (x+1)^2 \label{eq:f2}
\end{align}
% Unnumbered: align*
% Suppress single number: \nonumber or \notag before \\
gather — 居中且不对齐
\begin{gather}
x + y = 1 \\
x - y = 3
\end{gather}
multline — 长单行公式
\begin{multline}
f(x) = a_0 + a_1 x + a_2 x^2 + a_3 x^3 \\
+ a_4 x^4 + a_5 x^5 + a_6 x^6
\end{multline}
第一行左对齐,最后一行右对齐,中间各行居中。
split — 在 equation 内部进行子对齐
\begin{equation}
\begin{split}
f(x) &= a + b + c \\
&\quad + d + e
\end{split}
\end{equation}
整组公式共用一个编号。
cases
\begin{equation}
|x| = \begin{cases}
x & \text{if } x \geq 0 \\
-x & \text{if } x < 0
\end{cases}
\end{equation}
% mathtools: dcases (displaystyle), rcases (right brace)
\begin{dcases}
\frac{x}{2} & x > 0 \\
0 & x = 0
\end{dcases}
aligned / gathered — 行内子环境
% Use inside equation or \[ \] for sub-alignment
\begin{equation}
\left\{
\begin{aligned}
2x + 3y &= 7 \\
x - y &= 1
\end{aligned}
\right.
\end{equation}
subequations
\begin{subequations} \label{eq:system}
\begin{align}
x + y &= 1 \label{eq:system_a} \\
x - y &= 3 \label{eq:system_b}
\end{align}
\end{subequations}
% Produces (1a), (1b)
环境概要
| 环境 | 对齐方式 | 编号方式 | 用途 |
|---|
equation | 居中 | 一个编号 | 单个公式 |
align | & 列 | 每行编号 | 对齐的公式 |
gather | 居中 | 每行编号 | 彼此无关的公式 |
multline | 首行左对齐,末行右对齐 | 一个编号 | 长公式 |
split | & 列 | 一个编号(父级) | 在公式内进行子对齐 |
cases | & 列 | 无(在公式环境内) | 分段函数 |
aligned | & 列 | 无(在公式环境内) | 行内子对齐 |
flalign | & 列 | 每行编号 | 全宽对齐 |
添加 * 可使用无编号变体(split、aligned、gathered 除外)。
数学公式中的文本
\text{if } % normal text (respects surrounding font)
\mathrm{const} % upright roman
\textit{word} % italic text
\mathit{diff} % math italic (different spacing than default)
\mathbf{v} % bold (not for Greek — use \boldsymbol)
\boldsymbol{\alpha} % bold Greek
\mathbb{R} % blackboard bold: ℝ (amssymb)
\mathcal{L} % calligraphic: ℒ
\mathfrak{g} % Fraktur (amssymb)
\mathsf{X} % sans-serif
\mathtt{code} % monospace
\operatorname{span} % upright operator (one-off)
数学公式中的间距
| 命令 | 宽度 | 示例 |
|---|
\, | 3/18 em(窄间距) | \int f(x)\, dx |
\: 或 \> | 4/18 em(中等间距) | |
\; | 5/18 em(宽间距) | |
\! | −3/18 em(负窄间距) | 使用 \!\! 缩小间距 |
\quad | 1 em | x \quad y |
\qquad | 2 em | |
\phantom{x} | 占据 “x” 的空间 | 对齐技巧 |
\hphantom{x} | 仅水平方向 | |
\vphantom{x} | 仅垂直方向 | |
运算符
% Built-in: \sin, \cos, \tan, \log, \ln, \exp, \min, \max,
% \sup, \inf, \lim, \det, \dim, \ker, \gcd, \Pr, \hom, ...
% Custom operator (preamble)
\DeclareMathOperator{\tr}{tr} % like \sin
\DeclareMathOperator*{\argmax}{arg\,max} % limits below in display
% Usage
\tr(A) = \sum_i a_{ii} \qquad x^* = \argmax_x f(x)
矩阵
\begin{pmatrix} a & b \\ c & d \end{pmatrix} % ( )
\begin{bmatrix} a & b \\ c & d \end{bmatrix} % [ ]
\begin{Bmatrix} a & b \\ c & d \end{Bmatrix} % { }
\begin{vmatrix} a & b \\ c & d \end{vmatrix} % | | (determinant)
\begin{Vmatrix} a & b \\ c & d \end{Vmatrix} % ‖ ‖
\begin{matrix} a & b \\ c & d \end{matrix} % no delimiters
% Small inline matrix
$\bigl(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\bigr)$
默认最大列数 = 10。 如需更多列:\setcounter{MaxMatrixCols}{20}
定界符
% Auto-sizing (use sparingly — can oversize)
\left( \frac{a}{b} \right)
\left[ \sum_i x_i \right]
\left\{ x \in \mathbb{R} \mid x > 0 \right\}
\left. \frac{df}{dx} \right|_{x=0} % \left. = invisible delimiter
% Manual sizing (preferred for control)
\bigl( \bigr) % slightly bigger
\Bigl( \Bigr) % bigger
\biggl( \biggr) % even bigger
\Biggl( \Biggr) % biggest
% mathtools paired delimiters (best approach)
\DeclarePairedDelimiter\abs{\lvert}{\rvert}
\DeclarePairedDelimiter\norm{\lVert}{\rVert}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}
\DeclarePairedDelimiter\set{\{}{\}}
% Usage:
\abs{x} % |x| (no sizing)
\abs*{x} % \left|x\right| (auto-sizing)
\abs[\big]{x} % \bigl|x\bigr| (manual)
mathtools 扩展
% Colon equals
\coloneqq % :=
\eqqcolon % =:
% dcases (displaystyle fractions in cases)
\begin{dcases} ... \end{dcases}
% rcases (right brace)
\begin{rcases} ... \end{rcases}
% Short intertext (less vertical space than \intertext)
\begin{align}
f(x) &= x^2 \\
\shortintertext{where}
x &> 0
\end{align}
% Prescript (left sub/superscript)
\prescript{14}{6}{\mathrm{C}} % ¹⁴₆C
% Cramped styles
\cramped{x^{x^x}} % reduces height
% Smashed operator limits
\smashoperator{\sum_{i=1}^{n}}
符号参考
希腊字母
| 小写 | 大写 | 小写 | 大写 |
|---|
\alpha α | — | \nu ν | — |
\beta β | — | \xi ξ | \Xi Ξ |
\gamma γ | \Gamma Γ | \pi π | \Pi Π |
\delta δ | \Delta Δ | \rho ρ | — |
\epsilon ε | — | \sigma σ | \Sigma Σ |
\varepsilon ε | — | \tau τ | — |
\zeta ζ | — | \upsilon υ | \Upsilon Υ |
\eta η | — | \phi ϕ | \Phi Φ |
\theta θ | \Theta Θ | \varphi φ | — |
\iota ι | — | \chi χ | — |
\kappa κ | — | \psi ψ | \Psi Ψ |
\lambda λ | \Lambda Λ | \omega ω | \Omega Ω |
\mu μ | — | | |
变体:\varepsilon、\vartheta、\varphi、\varrho、\varsigma
关系符号
| 符号 | 命令 | 符号 | 命令 |
|---|
| ≤ | \leq | ≥ | \geq |
| ≪ | \ll | ≫ | \gg |
| ≠ | \neq | ≈ | \approx |
| ∼ | \sim | ≃ | \simeq |
| ≡ | \equiv | ≅ | \cong |
| ∝ | \propto | ∈ | \in |
| ∉ | \notin | ⊂ | \subset |
| ⊆ | \subseteq | ⊃ | \supset |
| ⊇ | \supseteq | ⊄ | \not\subset |
| ≺ | \prec | ≻ | \succ |
| ⊥ | \perp | ∥ | \parallel |
| ⊢ | \vdash | ⊣ | \dashv |
| ⊨ | \models | | |
二元运算符
| 符号 | 命令 | 符号 | 命令 |
|---|
| ± | \pm | ∓ | \mp |
| × | \times | ÷ | \div |
| · | \cdot | ∗ | \ast |
| ⊕ | \oplus | ⊗ | \otimes |
| ∪ | \cup | ∩ | \cap |
| ∨ | \vee | ∧ | \wedge |
| ∘ | \circ | • | \bullet |
| † | \dagger | ‡ | \ddagger |
箭头
| 符号 | 命令 | 符号 | 命令 |
|---|
| → | \to / \rightarrow | ← | \leftarrow |
| ⇒ | \Rightarrow | ⇐ | \Leftarrow |
| ↔ | \leftrightarrow | ⇔ | \Leftrightarrow |
| ↦ | \mapsto | ⟶ | \longrightarrow |
| ↑ | \uparrow | ↓ | \downarrow |
| ⇑ | \Uparrow | ⇓ | \Downarrow |
| ↗ | \nearrow | ↘ | \searrow |
| ⟹ | \implies | ⟸ | \impliedby |
| ⟺ | \iff | | |
| ↪ | \hookrightarrow | ↠ | \twoheadrightarrow |
大型运算符
| 符号 | 命令 | 符号 | 命令 |
|---|
| ∑ | \sum | ∏ | \prod |
| ∫ | \int | ∮ | \oint |
| ∬ | \iint | ∭ | \iiint |
| ⋃ | \bigcup | ⋂ | \bigcap |
| ⨁ | \bigoplus | ⨂ | \bigotimes |
| ⋁ | \bigvee | ⋀ | \bigwedge |
| ∐ | \coprod | | |
省略号
| 命令 | 输出 | 用法 |
|---|
\cdots | ⋯ | 运算符之间:$a + \cdots + z$ |
\ldots | … | 列表中:$a_1, \ldots, a_n$ |
\vdots | ⋮ | 垂直 |
\ddots | ⋱ | 对角线(矩阵) |
其他符号(amssymb)
| 符号 | 命令 | 符号 | 命令 |
|---|
| ℝ | \mathbb{R} | ℂ | \mathbb{C} |
| ℤ | \mathbb{Z} | ℕ | \mathbb{N} |
| ℚ | \mathbb{Q} | ∅ | \emptyset / \varnothing |
| ∞ | \infty | ∂ | \partial |
| ∇ | \nabla | ℓ | \ell |
| ℏ | \hbar | ∀ | \forall |
| ∃ | \exists | ¬ | \neg |
| √ | \sqrt{x} | ⁿ√ | \sqrt[n]{x} |
| ∠ | \angle | △ | \triangle |
| ⊤ | \top | ⊥ | \bot |
| ♠ | \spadesuit | ♣ | \clubsuit |
重音符号
| 命令 | 结果 | 用法 |
|---|
\hat{a} | â | 单位向量 |
\bar{a} | ā | 平均值、共轭 |
\vec{a} | a⃗ | 向量 |
\dot{a} | ȧ | 时间导数 |
\ddot{a} | ä | 二阶导数 |
\tilde{a} | ã | 近似值 |
\widehat{abc} | 宽帽符号 | 跨越 |
\widetilde{abc} | 宽波浪号 | 跨越 |
\overline{abc} | 上划线 | 集合、共轭 |
\underline{abc} | 下划线 | |
\overbrace{a+b}^{n} | 上方大括号 | 分组 |
\underbrace{a+b}_{n} | 下方大括号 | 分组 |
定理环境(amsthm)
\usepackage{amsthm}
% Define theorem types (preamble)
\newtheorem{theorem}{Theorem}[section] % numbered by section
\newtheorem{lemma}[theorem]{Lemma} % shares theorem counter
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{proposition}[theorem]{Proposition}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}
% Usage
\begin{theorem}[Pythagoras]
For a right triangle: $a^2 + b^2 = c^2$.
\end{theorem}
\begin{proof}
Obvious. % ends with □ automatically
% Use \qedhere to place □ at end of equation:
% \[ a^2 + b^2 = c^2 \qedhere \]
\end{proof}
定理样式
| 样式 | 正文字体 | 标题 | 用途 |
|---|
plain(默认) | 斜体 | 粗体 | 定理、引理 |
definition | 正体 | 粗体 | 定义、示例 |
remark | 正体 | 斜体 | 说明、注释 |
常见问题
| 问题 | 原因 | 解决方法 |
|---|
$$...$$ | TeX 原语,间距不佳 | 使用 \[...\] 或 equation* |
eqnarray | = 周围的间距不佳 | 改用 align(始终如此) |
| 公式编号出现在错误行 | 最后一行后有 \\ | 删除末尾的 \\ |
\left...\right 跨行 | 无法跨越 \\ | 手动使用 \bigl...\bigr |
| 希腊字母加粗 | \mathbf 无效 | 使用 \boldsymbol{\alpha} |
缺少 \displaystyle | 行内分数太小 | 使用 \dfrac(或 \displaystyle\frac) |
:= 间距不正确 | 冒号被视为关系符号 | 使用 \coloneqq(mathtools) |
\mid、| 和 ` | ` | 间距各不相同 |
| 公式中的 hbox 溢出 | 公式太宽 | 使用 multline、split 或手动换行 |
| 证明框缺失 | 在环境内使用了 \qed | 在末尾使用 \qedhere |