Mathematics

LaTeX 数学公式
完全指南

数学排版是 LaTeX 最强大的能力。无论是简单的行内公式,还是复杂的多行推导,LaTeX 都能生成出版级品质的数学表达式。

行内公式与独立公式

LaTeX 数学公式有两种基本形式:行内公式(inline)嵌入在文字段落中,独立公式(display)单独占一行居中显示。

math-modes.tex
\documentclass{article}
\usepackage{amsmath}  % 数学排版必备宏包

\begin{document}

% 行内公式:使用 $...$ 包裹
爱因斯坦的质能方程 $E = mc^2$ 揭示了质量与能量的等价关系。
函数 $f(x) = ax^2 + bx + c$ 是一个二次多项式。

% 独立公式(无编号):使用 \[...\]
二次方程的求根公式为
\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]

% 独立公式(带编号):使用 equation 环境
\begin{equation}
  \label{eq:euler}
  e^{i\pi} + 1 = 0
\end{equation}

欧拉公式(公式~\ref{eq:euler})被誉为数学中最优美的等式。

% 不推荐:$$...$$ 是 TeX 原始语法,amsmath 建议用 \[...\] 替代
% $$这样写也能工作,但间距处理不如 \[...\] 好$$

\end{document}
始终在导言区加载 amsmath 宏包。它是美国数学学会提供的标准数学排版扩展,几乎所有学术模板都默认包含它。

常用数学符号

LaTeX 内置了数百个数学符号命令。以下是学术写作中最常用的一组。掌握这些,你就能排版绝大多数论文中的公式。

上下标、分式与根号

basic-symbols.tex
% 上标(指数)与下标
$x^2$, $a^{n+1}$, $x_i$, $a_{ij}$, $x_i^2$

% 分式
$\frac{a}{b}$, $\frac{x^2 + 1}{x - 1}$

% 嵌套分式(独立公式中更清晰)
\[ \frac{1}{1 + \frac{1}{1 + \frac{1}{x}}} \]

% 根号
$\sqrt{2}$, $\sqrt[3]{8}$, $\sqrt{x^2 + y^2}$

求和、积分与极限

operators.tex
% 求和
\[ \sum_{i=1}^{n} x_i = x_1 + x_2 + \cdots + x_n \]

% 乘积
\[ \prod_{k=1}^{n} k = n! \]

% 积分
\[ \int_{0}^{\infty} e^{-x^2} \, dx = \frac{\sqrt{\pi}}{2} \]

% 多重积分
\[ \iint_{D} f(x, y) \, dx \, dy \]

% 极限
\[ \lim_{n \to \infty} \left(1 + \frac{1}{n}\right)^n = e \]

希腊字母与特殊符号

greek-symbols.tex
% 小写希腊字母
$\alpha$, $\beta$, $\gamma$, $\delta$, $\epsilon$,
$\theta$, $\lambda$, $\mu$, $\sigma$, $\omega$

% 大写希腊字母
$\Gamma$, $\Delta$, $\Theta$, $\Lambda$, $\Sigma$, $\Omega$

% 关系符号
$\leq$, $\geq$, $\neq$, $\approx$, $\equiv$,
$\subset$, $\supset$, $\in$, $\notin$

% 箭头
$\to$, $\Rightarrow$, $\Leftrightarrow$, $\mapsto$

% 其他常用
$\infty$, $\partial$, $\nabla$, $\forall$, $\exists$,
$\dots$, $\cdots$, $\vdots$, $\ddots$

括号与定界符

当公式中包含分式或大型结构时,普通括号会显得太小。使用 \left \right 让括号自动缩放。

delimiters.tex
% 自动缩放括号
\[ \left( \frac{a}{b} \right) \]

% 方括号和花括号
\[ \left[ \sum_{i=1}^{n} x_i \right]^2 \]
\[ \left\{ x \in \mathbb{R} \mid x > 0 \right\} \]

% 绝对值与范数
\[ \left| x - y \right|, \quad \left\| \mathbf{v} \right\| \]

矩阵与向量

矩阵是线性代数和机器学习论文中的常客。amsmath 提供了多种矩阵环境,区别仅在于外围定界符。

matrices.tex
\usepackage{amsmath}

% 无括号矩阵
\[ \begin{matrix}
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9
\end{matrix} \]

% 圆括号矩阵(最常用)
\[ \mathbf{A} = \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} \]

% 方括号矩阵
\[ \begin{bmatrix}
  1 & 0 \\
  0 & 1
\end{bmatrix} \]

% 行列式
\[ \det(\mathbf{A}) = \begin{vmatrix}
  a & b \\
  c & d
\end{vmatrix} = ad - bc \]

% 列向量
\[ \mathbf{x} = \begin{pmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{pmatrix} \]

多行公式对齐

推导过程通常需要多行公式,且在等号处对齐。align 环境是最强大的多行数学环境。

align.tex
\usepackage{amsmath}

% align 环境:每行带编号
\begin{align}
  (a + b)^2 &= (a + b)(a + b) \\
             &= a^2 + ab + ba + b^2 \\
             &= a^2 + 2ab + b^2
\end{align}

% align* 环境:不带编号
\begin{align*}
  \nabla \cdot \mathbf{E}  &= \frac{\rho}{\varepsilon_0}  & \text{(高斯定律)} \\
  \nabla \cdot \mathbf{B}  &= 0                              & \text{(磁场无散)} \\
  \nabla \times \mathbf{E} &= -\frac{\partial \mathbf{B}}{\partial t}  & \text{(法拉第定律)} \\
  \nabla \times \mathbf{B} &= \mu_0 \mathbf{J} + \mu_0 \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t}  & \text{(安培-麦克斯韦定律)}
\end{align*}

% 分段函数用 cases 环境
\[ f(x) = \begin{cases}
  x^2     & \text{if } x \geq 0 \\
  -x^2    & \text{if } x < 0
\end{cases} \]

% 长公式跨行(在运算符处断行)
\begin{multline}
  L(\theta) = -\frac{1}{N} \sum_{i=1}^{N}
    \Big[ y_i \log \hat{y}_i
         + (1 - y_i) \log(1 - \hat{y}_i) \Big] \\
    + \frac{\lambda}{2} \|\theta\|^2
\end{multline}

& 标记对齐位置(通常放在 = 号前面),\\ 表示换行。如果某一行不想编号,在该行末尾 \\ 前加 \nonumber

定理与证明

数学和理论计算机科学的论文经常包含定理、引理、证明等结构。LaTeX 的 amsthm 宏包提供了完善的定理环境支持。

theorems.tex
\documentclass{article}
\usepackage{amsmath, amssymb, amsthm}

% 在导言区定义定理类环境
\newtheorem{theorem}{定理}[section]    % 按 section 编号
\newtheorem{lemma}[theorem]{引理}      % 与定理共享编号
\newtheorem{corollary}[theorem]{推论}
\newtheorem{definition}{定义}[section]

% 非加粗标题风格(用于注记、例子)
\theoremstyle{remark}
\newtheorem{remark}{注记}
\newtheorem{example}{例}

\begin{document}

\section{基本不等式}

\begin{definition}
设 $a, b$ 为正实数,称 $\frac{a+b}{2}$ 为算术平均值,
$\sqrt{ab}$ 为几何平均值。
\end{definition}

\begin{theorem}[AM-GM 不等式]
\label{thm:amgm}
对任意正实数 $a, b$,有
\[ \frac{a + b}{2} \geq \sqrt{ab} \]
等号成立当且仅当 $a = b$。
\end{theorem}

\begin{proof}
考虑
\begin{align*}
  \frac{a+b}{2} - \sqrt{ab}
    &= \frac{a + b - 2\sqrt{ab}}{2} \\
    &= \frac{(\sqrt{a} - \sqrt{b})^2}{2} \\
    &\geq 0
\end{align*}
等号成立当且仅当 $\sqrt{a} = \sqrt{b}$,即 $a = b$。
\end{proof}

\begin{corollary}
对任意正实数 $a, b$,有 $(a+b)^2 \geq 4ab$。
\end{corollary}

\begin{remark}
AM-GM 不等式可推广到 $n$ 个正实数的情形。
\end{remark}

\end{document}

自定义命令

当你发现自己反复输入相同的公式片段时,是时候定义自己的命令了。\newcommand 让你创建快捷方式,既减少输入量,又提高一致性——需要修改时只改一处。

commands.tex
% 无参数命令
\newcommand{\R}{\mathbb{R}}           % 实数集
\newcommand{\N}{\mathbb{N}}           % 自然数集
\newcommand{\E}{\mathbb{E}}           % 期望

% 使用:$f: \R \to \R$

% 带参数命令
\newcommand{\norm}[1]{\left\| #1 \right\|}      % 范数
\newcommand{\abs}[1]{\left| #1 \right|}           % 绝对值
\newcommand{\inner}[2]{\left\langle #1, #2 \right\rangle}  % 内积
\newcommand{\diff}[2]{\frac{\partial #1}{\partial #2}}      % 偏导数

% 使用示例
\[ \norm{\mathbf{x} - \mathbf{y}} \leq \norm{\mathbf{x}} + \norm{\mathbf{y}} \]
\[ \inner{\mathbf{u}}{\mathbf{v}} = \sum_{i=1}^{n} u_i v_i \]
\[ \diff{L}{\theta} = 0 \]

% 带默认参数
\newcommand{\prob}[1][]{\operatorname{P}\left( #1 \right)}
% $\prob{A \mid B}$ → P(A | B)
建议在论文的导言区集中定义所有自定义命令,或者单独放在一个 macros.tex 文件中并用 \input 引入,方便团队共享和维护。

CoCraft 的公式功能

CoCraft 编辑器为数学公式提供了增强体验。当光标停留在公式环境中时,编辑器会实时渲染公式预览,让你在编写时就能看到效果,无需等待完整编译。

如果你手边有纸质文档或截图中的数学公式,CoCraft 的公式 OCR 功能可以直接将图片中的公式识别并转换为 LaTeX 代码。拍照上传,AI 会生成可直接使用的 LaTeX 代码——特别适合从课本或手写笔记中提取公式。

对于复杂的公式编写,你还可以在 AI 对话面板中用自然语言描述你想要的公式(比如"写一个 softmax 函数的公式"),AI 会直接生成对应的 LaTeX 代码。

准备好开始了吗?

在 CoCraft 中在线编写和编译你的 LaTeX 文档。

edit_document免费开始使用