Markdown 数学公式写作指南(三):矩阵、符号与排版技巧

文章目录

写论文级公式的最后一块拼图

前两篇我们掌握了基础语法(希腊字母、上下标)和数学运算(分数、求和、积分、根号)。但写到论文级别的公式时,还缺几样东西:矩阵怎么写?范数符号怎么打?条件公式怎么排版?多行公式怎么对齐?

这篇就是补上这块拼图。读完之后,你能用 Markdown 写出任何出现在 AI 论文中的公式形式。


一、矩阵

矩阵是 AI 公式中最复杂的排版元素,但语法其实很规律。

基本矩阵

markdown 复制代码
$$
\begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{bmatrix}
$$

渲染效果:

1 2 3 4 5 6 7 8 9 \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} 147258369

语法要点:& 分隔列,\\ 换行,整个矩阵放在 \begin{bmatrix}\end{bmatrix} 之间。

列向量与行向量

markdown 复制代码
$$
\mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}, \quad
\mathbf{y} = \begin{bmatrix} y_1 & y_2 & y_3 \end{bmatrix}
$$

渲染效果:

x = x 1 x 2 x 3 , y = y 1 y 2 y 3 \mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}, \quad \mathbf{y} = \begin{bmatrix} y_1 & y_2 & y_3 \end{bmatrix} x= x1x2x3 ,y=y1y2y3

\quad 是一个中等宽度的空格,用来在公式中分隔两个表达式。

带省略号的大矩阵

markdown 复制代码
$$
W = \begin{bmatrix}
w_{11} & w_{12} & \cdots & w_{1n} \\
w_{21} & w_{22} & \cdots & w_{2n} \\
\vdots & \vdots & \ddots & \vdots \\
w_{m1} & w_{m2} & \cdots & w_{mn}
\end{bmatrix}
$$

渲染效果:

W = w 11 w 12 ⋯ w 1 n w 21 w 22 ⋯ w 2 n ⋮ ⋮ ⋱ ⋮ w m 1 w m 2 ⋯ w m n W = \begin{bmatrix} w_{11} & w_{12} & \cdots & w_{1n} \\ w_{21} & w_{22} & \cdots & w_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ w_{m1} & w_{m2} & \cdots & w_{mn} \end{bmatrix} W= w11w21⋮wm1w12w22⋮wm2⋯⋯⋱⋯w1nw2n⋮wmn

三个省略号各有用途:\cdots 水平省略号,\vdots 竖直省略号,\ddots 对角省略号。

四种括号风格

markdown 复制代码
$\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}$  圆括号
$\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}$  方括号(最常用)
$\begin{vmatrix} 1 & 2 \\ 3 & 4 \end{vmatrix}$  竖线(行列式)
$\begin{Vmatrix} 1 & 2 \\ 3 & 4 \end{Vmatrix}$  双竖线(范数风格)

渲染效果: ( 1 2 3 4 ) \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} (1324), 1 2 3 4 \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} 1324, ∣ 1 2 3 4 ∣ \begin{vmatrix} 1 & 2 \\ 3 & 4 \end{vmatrix} 1324 , ∥ 1 2 3 4 ∥ \begin{Vmatrix} 1 & 2 \\ 3 & 4 \end{Vmatrix} 1324

AI 论文中 bmatrix(方括号)最常用,比如全连接层 a = W x + b \mathbf{a} = W\mathbf{x} + \mathbf{b} a=Wx+b 中的权重矩阵 W W W。


二、特殊运算符

指数与对数

markdown 复制代码
$\exp(x)$  $e^x$  $\ln(x)$  $\log_{2}(x)$  $\log_{10}(x)$

渲染效果: exp ⁡ ( x ) \exp(x) exp(x) e x e^x ex ln ⁡ ( x ) \ln(x) ln(x) log ⁡ 2 ( x ) \log_{2}(x) log2(x) log ⁡ 10 ( x ) \log_{10}(x) log10(x)

实战举例 :Softmax 中 exp ⁡ ( x i ) / ∑ j exp ⁡ ( x j ) \exp(x_i) / \sum_j \exp(x_j) exp(xi)/∑jexp(xj),交叉熵中 − log ⁡ ( y ^ ) -\log(\hat{y}) −log(y^)。

最大值、最小值与 argmax/argmin

markdown 复制代码
$\max_{i} x_i$  $\min_{i} x_i$  $\arg\max_{\theta} L(\theta)$  $\arg\min_{\theta} J(\theta)$

渲染效果: max ⁡ i x i \max_{i} x_i maxixi min ⁡ i x i \min_{i} x_i minixi arg ⁡ max ⁡ θ L ( θ ) \arg\max_{\theta} L(\theta) argmaxθL(θ) arg ⁡ min ⁡ θ J ( θ ) \arg\min_{\theta} J(\theta) argminθJ(θ)

绝对值与范数

markdown 复制代码
$|x|$  $\|\mathbf{x}\|$  $\|\mathbf{x}\|_1$  $\|\mathbf{x}\|_2$  $\|\mathbf{x}\|_\infty$

渲染效果: ∣ x ∣ |x| ∣x∣ ∥ x ∥ \|\mathbf{x}\| ∥x∥ ∥ x ∥ 1 \|\mathbf{x}\|_1 ∥x∥1 ∥ x ∥ 2 \|\mathbf{x}\|2 ∥x∥2 ∥ x ∥ ∞ \|\mathbf{x}\|\infty ∥x∥∞

实战举例 :L1 正则化 λ ∥ w ∥ 1 \lambda \|\mathbf{w}\|_1 λ∥w∥1,L2 正则化 λ ∥ w ∥ 2 2 \lambda \|\mathbf{w}\|_2^2 λ∥w∥22------这两个在机器学习论文中几乎必出现。

偏导与梯度

markdown 复制代码
$\frac{\partial L}{\partial w}$  $\nabla L$  $\nabla_{\theta} J(\theta)$

渲染效果: ∂ L ∂ w \frac{\partial L}{\partial w} ∂w∂L ∇ L \nabla L ∇L ∇ θ J ( θ ) \nabla_{\theta} J(\theta) ∇θJ(θ)


三、箭头与关系符号

常用箭头

markdown 复制代码
$\to$  $\rightarrow$  $\Rightarrow$  $\mapsto$  $\leftarrow$  $\Leftarrow$  $\leftrightarrow$  $\Leftrightarrow$

渲染效果: → \to → → \rightarrow → ⇒ \Rightarrow ⇒ ↦ \mapsto ↦ ← \leftarrow ← ⇐ \Leftarrow ⇐ ↔ \leftrightarrow ↔ ⇔ \Leftrightarrow ⇔

关系符号

markdown 复制代码
$\leq$  $\geq$  $\neq$  $\approx$  $\equiv$  $\propto$  $\sim$  $\doteq$

渲染效果: ≤ \leq ≤ ≥ \geq ≥ ≠ \neq = ≈ \approx ≈ ≡ \equiv ≡ ∝ \propto ∝ ∼ \sim ∼ ≐ \doteq ≐

实战举例 :概率分布 p ( y ∣ x ) ∝ exp ⁡ ( z ) p(y|x) \propto \exp(z) p(y∣x)∝exp(z),参数更新 θ ← θ − η ∇ L \theta \leftarrow \theta - \eta \nabla L θ←θ−η∇L,贝叶斯定理 P ( A ∣ B ) = P ( B ∣ A ) P ( A ) P ( B ) P(A|B) = \frac{P(B|A)P(A)}{P(B)} P(A∣B)=P(B)P(B∣A)P(A)。

集合符号

markdown 复制代码
$\in$  $\notin$  $\subset$  $\supset$  $\cup$  $\cap$  $\emptyset$  $\forall$  $\exists$

渲染效果: ∈ \in ∈ ∉ \notin ∈/ ⊂ \subset ⊂ ⊃ \supset ⊃ ∪ \cup ∪ ∩ \cap ∩ ∅ \emptyset ∅ ∀ \forall ∀ ∃ \exists ∃


四、排版进阶技巧

多行公式对齐

& 指定对齐位置,\\ 换行:

markdown 复制代码
$$
\mathcal{L} &= -\sum_{i=1}^{N} y_i \log \hat{y}_i \\
           &= -\sum_{i=1}^{N} y_i \log \text{softmax}(z_i)
$$

渲染效果:

KaTeX parse error: Expected 'EOF', got '&' at position 14: \mathcal{L} &̲= -\sum_{i=1}^{...

这在推导等式变换时非常常用------读者能清楚地看到每一步变化的是什么。

条件公式(分段函数)

markdown 复制代码
$$
f(x) = \begin{cases}
x^2 & \text{if } x \geq 0 \\
-x^2 & \text{if } x < 0
\end{cases}
$$

渲染效果:

f ( x ) = { x 2 if x ≥ 0 − x 2 if x < 0 f(x) = \begin{cases} x^2 & \text{if } x \geq 0 \\ -x^2 & \text{if } x < 0 \end{cases} f(x)={x2−x2if x≥0if x<0

ReLU 就是经典的条件函数: ReLU ( x ) = max ⁡ ( 0 , x ) \text{ReLU}(x) = \max(0, x) ReLU(x)=max(0,x),也可以写成 { x x ≥ 0 0 x < 0 \begin{cases} x & x \geq 0 \\ 0 & x < 0 \end{cases} {x0x≥0x<0。

自适应括号

\left\right 让括号自动适应内容大小:

markdown 复制代码
$$
\left\| \sum_{i=1}^{N} \mathbf{x}_i \right\| \leq \sum_{i=1}^{N} \|\mathbf{x}_i\|
$$

渲染效果:

∥ ∑ i = 1 N x i ∥ ≤ ∑ i = 1 N ∥ x i ∥ \left\| \sum_{i=1}^{N} \mathbf{x}i \right\| \leq \sum{i=1}^{N} \|\mathbf{x}_i\| i=1∑Nxi ≤i=1∑N∥xi∥

公式内文字与颜色标注

markdown 复制代码
$$
\text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}
$$

渲染效果:

Accuracy = T P + T N T P + T N + F P + F N \text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN} Accuracy=TP+TN+FP+FNTP+TN

markdown 复制代码
$$
\text{Loss} = \underbrace{\text{CE Loss}}_{\text{\color{blue}分类损失}} + \underbrace{\lambda \|\mathbf{w}\|^2}_{\text{\color{red}正则项}}
$$

渲染效果:

Loss = CE Loss ⏟ 分类损失 + λ ∥ w ∥ 2 ⏟ 正则项 \text{Loss} = \underbrace{\text{CE Loss}}{\text{\color{blue}分类损失}} + \underbrace{\lambda \|\mathbf{w}\|^2}{\text{\color{red}正则项}} Loss=分类损失 CE Loss+正则项 λ∥w∥2

\underbrace 给公式的一部分加底部注释,\color{颜色名} 给文字着色,\text{} 在公式中插入普通文字------三者搭配可以让复杂公式的结构一目了然。


完整速查表

语法 效果 用途
$x$ 行内公式 公式嵌在文字中
$$x$$ 块级公式 公式独占一行
\frac{a}{b} a b \frac{a}{b} ba 分数
x^{a} / x_{a} x a x^a xa / x a x_a xa 上标 / 下标
\sqrt{x} x \sqrt{x} x 平方根
\sum_{i=1}^{N} ∑ i = 1 N \sum_{i=1}^{N} ∑i=1N 求和
\prod_{i=1}^{N} ∏ i = 1 N \prod_{i=1}^{N} ∏i=1N 连乘
\int_{a}^{b} ∫ a b \int_{a}^{b} ∫ab 积分
\lim_{x \to \infty} lim ⁡ x → ∞ \lim_{x \to \infty} limx→∞ 极限
\nabla / \partial ∇ \nabla ∇ / ∂ \partial ∂ 梯度 / 偏导
\mathbf{x} x \mathbf{x} x 粗体向量
\mathcal{L} L \mathcal{L} L 花体(损失函数)
\mathbb{E} / \mathbb{R} E \mathbb{E} E / R \mathbb{R} R 期望算子 / 实数集
\hat{y} / \tilde{x} y ^ \hat{y} y^ / x ~ \tilde{x} x~ 帽子 / 波浪号
\leq / \geq / \neq ≤ \leq ≤ / ≥ \geq ≥ / ≠ \neq = 关系符号
\propto / \sim ∝ \propto ∝ / ∼ \sim ∼ 正比于 / 服从分布
\odot / \cdot ⊙ \odot ⊙ / ⋅ \cdot ⋅ 逐元素乘 / 点乘
\in / \forall / \exists ∈ \in ∈ / ∀ \forall ∀ / ∃ \exists ∃ 集合与逻辑
\left( ... \right) 自适应括号 括号跟随内容大小
\text{文字} 公式内文字 插入说明文字
\begin{cases} 条件分支 分段函数
\begin{bmatrix} 矩阵 方括号矩阵

小结

这篇是系列的"工具箱"篇。加上前两篇的内容,你现在能写出:

  • 完整的矩阵和向量(四种括号风格、大矩阵省略号)
  • 所有常见运算符(指数对数、范数、梯度、最值)
  • 箭头、关系符号和集合符号
  • 多行对齐、条件公式、自适应括号、颜色标注

语法部分到此结束。下一篇是综合实战------用 27 个 AI 经典算法公式把前三篇学的所有语法都用起来。