Skip to content

LaTeX 公式 ​

这页记录本站写公式时常用的 LaTeX 语法,示例由 MathJax 渲染。MathJax 支持的是一部分 TeX 数学语法,不能代替 LaTeX 编译器。独立 .tex 文档中的 \text、aligned 和矩阵环境需要 amsmath,\mathbb 等符号可通过 amssymb 提供。

1 数学环境 ​

1.1 行内公式 ​

使用一对 $ 包裹公式,公式会跟在正文中排版。

示例:

latex
$x = y + 1$

效果:

x=y+1

1.2 块公式 ​

在本站 Markdown 中,使用一对 $$ 包裹公式,公式会独立排版。

示例:

latex
$$
x = y + 1
$$

效果:

x=y+1

1.3 独立 LaTeX 文档 ​

在 .tex 文档中,行内公式也可写为 \(...\)。不编号的独立公式使用 \[...\],需要编号和交叉引用时使用 equation。不要把 Markdown 中的 $$...$$ 直接当作 LaTeX 文档的推荐写法。

下面是带编号公式的完整示例:

latex
\documentclass{article}
\usepackage{amsmath,amssymb}

\begin{document}

The path cost is
\begin{equation}\label{eq:path-cost}
C(P) = \sum_{e \in P} w(e).
\end{equation}

Equation~\eqref{eq:path-cost} defines the total cost.

\end{document}

\eqref 自动给公式编号加圆括号。修改标签或引用后,通常需要重新编译两遍。

2 数学符号 ​

2.1 希腊字母 ​

大写字形与拉丁字母相同的,直接输入对应的大写字母。小写 omicron 通常也直接写为拉丁字母 o。

小写写法效果大写写法效果
\alphaαAA
\betaβBB
\gammaγ\GammaΓ
\deltaδ\DeltaΔ
\epsilonϵEE
\zetaζZZ
\etaηHH
\thetaθ\ThetaΘ
\iotaιII
\kappaκKK
\lambdaλ\LambdaΛ
\muμMM
\nuνNN
\xiξ\XiΞ
ooOO
\piπ\PiΠ
\rhoρPP
\sigmaσ\SigmaΣ
\tauτTT
\upsilonυ\UpsilonΥ
\phiϕ\PhiΦ
\chiχXX
\psiψ\PsiΨ
\omegaω\OmegaΩ

2.2 上下标 ​

_ 表示下标,^ 表示上标。上下标超过一个字符时,需要用 {} 括起来。

示例效果
x_1x1
x_{ij}xij
x^2x2
x^{n+1}xn+1
x_n^2xn2

2.3 分数 ​

示例:\frac{a}{b}

效果:ab

2.4 根号 ​

  • 平方根:\sqrt{x},效果:x
  • n 次根:\sqrt[n]{x},效果:xn

2.5 求和与积分 ​

示例效果
\sum_{i=1}^n a_i∑i=1nai
\prod_{i=1}^n a_i∏i=1nai
\int_a^b f(x)\,\mathrm{d}x∫abf(x)dx
\iint_D f(x,y)\,\mathrm{d}x\,\mathrm{d}y∬Df(x,y)dxdy
\iiint_V f(x,y,z)\,\mathrm{d}x\,\mathrm{d}y\,\mathrm{d}z∭Vf(x,y,z)dxdydz
\oint_C f(z)\,\mathrm{d}z∮Cf(z)dz

2.6 极限 ​

示例:\lim_{x\to\infty} f(x)

效果:limx→∞f(x)

2.7 微分与偏导 ​

示例效果
\frac{\mathrm{d}y}{\mathrm{d}x}dydx
\frac{\partial u}{\partial t}∂u∂t
\nabla f∇f

3 矩阵与数组 ​

3.1 简单矩阵 ​

示例:

latex
$$
\begin{matrix}
a & b \\
c & d
\end{matrix}
$$

效果:

abcd

3.2 带括号矩阵 ​

示例:

latex
$$
\begin{pmatrix}  % 圆括号
a & b \\
c & d
\end{pmatrix}
$$

$$
\begin{bmatrix}  % 方括号
a & b \\
c & d
\end{bmatrix}
$$

$$
\begin{vmatrix}  % 单竖线
a & b \\
c & d
\end{vmatrix}
$$

$$
\begin{Vmatrix}  % 双竖线
a & b \\
c & d
\end{Vmatrix}
$$

效果:

(abcd)[abcd]|abcd|‖abcd‖

3.3 一般矩阵 ​

示例:

latex
$$
\begin{pmatrix}
a_{11} & a_{12} & \cdots & a_{1n} \\
a_{21} & a_{22} & \cdots & a_{2n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{m1} & a_{m2} & \cdots & a_{mn}
\end{pmatrix}
$$

效果:

(a11a12⋯a1na21a22⋯a2n⋮⋮⋱⋮am1am2⋯amn)

3.4 数组环境 ​

示例:

latex
$$
\begin{array}{ccc}
a & b & c \\
d & e & f \\
g & h & i
\end{array}
$$

效果:

abcdefghi

列对齐选项:l(左对齐)、c(居中)、r(右对齐)。

4 分段函数 ​

示例:

latex
$$
f(x) =
\begin{cases}
x^2, & x \geq 0 \\
-x, & x < 0
\end{cases}
$$

效果:

f(x)={x2,x≥0−x,x<0

5 多行公式 ​

& 标记对齐位置,\\ 表示换行。在 $$...$$ 中可以使用 aligned:

latex
$$
\begin{aligned}
a^2 + b^2 &= c^2 \\
e^{i\pi} + 1 &= 0
\end{aligned}
$$

效果:

a2+b2=c2eiπ+1=0

6 特殊符号 ​

6.1 集合符号 ​

写法效果
\in∈
\subset⊂
\subseteq⊆
\supset⊃
\supseteq⊇
\cup∪
\cap∩
\setminus∖
\emptyset∅
\mathbb{N}N
\mathbb{Z}Z
\mathbb{Q}Q
\mathbb{R}R
\mathbb{C}C

6.2 逻辑符号 ​

写法效果
\land∧
\lor∨
\lnot¬
\Rightarrow⇒
\Leftrightarrow⇔
\forall∀
\exists∃

6.3 关系符号 ​

写法效果
<<
>>
\leq≤
\geq≥
\ll≪
\gg≫
\approx≈
\equiv≡
\sim∼
\simeq≃
\propto∝

6.4 箭头符号 ​

写法效果写法效果
\rightarrow→\Rightarrow⇒
\leftarrow←\Leftarrow⇐
\leftrightarrow↔\Leftrightarrow⇔
\mapsto↦\hookrightarrow↪

6.5 特殊运算符 ​

写法效果
\pm±
\mp∓
\times×
\div÷
\cdot⋅
\ast∗
\star⋆
\circ∘
\bullet∙
\diamond⋄
\oplus⊕
\otimes⊗
\odot⊙

7 高级技巧 ​

7.1 自定义命令 ​

在导言区定义常用命令:

latex
\newcommand{\abs}[1]{\left| #1 \right|}
\newcommand{\norm}[1]{\left\| #1 \right\|}
\newcommand{\pfrac}[2]{\left( \frac{#1}{#2} \right)}

这些命令在数学模式中使用:

latex
$\abs{x}, \norm{v}, \pfrac{a}{b}$

这里的 \left、\right 会按内容调整括号大小,两者必须配对。定义在 .tex 导言区的命令不会自动成为本站 MathJax 的全局命令。

7.2 空间控制 ​

latex
\,     % 小空格
\:     % 中空格
\;     % 大空格
\!     % 负空格
\quad  % 较大空格
\qquad % 很大空格

7.3 文本插入 ​

普通说明文字用 \text{}。其余命令主要用于改变数学字符的字形,其中的空格不会按正文空格处理。\mathbf 通常不能给希腊字母加粗,需要时可用 amsmath 的 \boldsymbol。

示例效果
\text{some text}some text
\mathrm{text}text
\mathbf{bold}bold
\mathit{italic}italic
\mathsf{sans-serif}sans−serif
\mathtt{typewriter}typewriter

8 常见问题 ​

8.1 分数显示过小 ​

行内分数太小时,可以用 \dfrac 强制采用独立公式的尺寸。不过,这会增大行高,正文中不宜处处使用:

latex
\dfrac{a}{b}  % 显示更大的分数

效果:

ab

8.2 矩阵元素对齐 ​

使用 array 环境并指定每一列的对齐方式:

latex
\begin{array}{rcl}
a+b & = & c \\
x & \leq & y
\end{array}

效果:

a+b=cx≤y

8.3 长公式换行 ​

在自然的运算位置断行,并用 & 对齐续行:

latex
$$
\begin{aligned}
S ={}& a + b + c + d + e + f \\
     &{}+ g + h + i + j + k + l
\end{aligned}
$$

效果:

S=a+b+c+d+e+f+g+h+i+j+k+l

8.4 多行公式的编号 ​

aligned 用在已有数学环境内,不单独生成编号。在 LaTeX 文档中,若需要每行分别编号,可使用 align。不需要编号时使用 align*,不要在这两个环境外再套 $、$$ 或 equation。

Released under the MIT License.