Skip to main content

LaTeX 指南

Summary

面向站点 KaTeX 环境的 LaTeX 语法指南。先梳理 TeX、LaTeX、KaTeX 三者的由来与关系:TeX 是高德纳开发的底层排版系统,LaTeX 是其上的高层宏包,KaTeX 是浏览器端快速渲染的公式库。随后按类别演示语法:行内与行间公式的写法,算术、比较、逻辑运算符号,求和、求积、积分、极限等巨运算符,递等式、分段函数、方程组、矩阵等多行公式,以及边框、颜色、正体与黑板粗体字体、自定义运算符和公式编号等进阶用法,每项都配可运行的效果预览。

参考资料​

简介​

TeX\TeX 是由高德纳(Donald Knuth)于 1978 年开发的底层排版系统,擅长高质量数学公式与科技文档排版。它既是一个排版引擎,也定义了一套排版用的宏语言,是后来众多数学排版工具的基础。

LaTeX\LaTeX 是 Leslie Lamport 于 1984 年在 TeX\TeX 之上构建的宏包,将 TeX\TeX 偏底层的命令封装为面向文档结构的高层接口,用于编写包含数学公式、图表、引用和章节结构的高质量文档。

KaTeX\KaTeX 是 Khan Academy 于 2014 年发布的 JavaScript 数学公式渲染库,可在浏览器中快速渲染使用 LaTeX\LaTeX 风格语法编写的数学公式。相比功能更全面的 MathJax,KaTeX\KaTeX 以同步渲染和速度见长,但仅支持 LaTeX\LaTeX 数学语法的子集。

(⊙∨⊙"≀")^‾\underline{\widehat{\dbinom{\odot_\vee\odot}{\raisebox{-8pt}{"}\wr\raisebox{-8pt}{"}}}}

推荐两个我常用的 LaTeX\LaTeX 排版工具网站:Overleaf 和 在线 LaTeX 公式编辑器。

warning

本文仅说明语法规则,不包含格式与排版建议。

公式类型​

行内公式​

行内公式(Inline Formula)是嵌入在文本中的公式,其两侧分别用 1 个 美元符号($)标记。

5 Blatex
$...$

行间公式​

行间公式(Display Formula)是独立成行并居中的公式,其前后分别用 2 个 美元符号($$)标记。

9 Blatex
$$
...
$$
tip

行内公式的「大小」(例如分数大小、巨运算符上下标位置)通常比行间公式小。

可以使用 \displaystyle 和 \textstyle 切换两种类型。

运算符号​

算术​

名称展示代码
加号+++
减号−--
乘号×\times\times
点乘⋅\cdot\cdot
除号÷\div\div
正负号±\pm\pm、\plusmn
负正号∓\mp\mp

比较​

名称展示代码
等号===、\eq
不等号≠\ne\ne、\neq
约等号≈\approx\approx
大于号>>>、\gt
小于号<<<、\lt
大于等于≥\ge\ge、\geq
小于等于≤\le\le、\leq
无穷∞\infty\infty
根号x\sqrt{x},xy\sqrt[y]{x}\sqrt{x},\sqrt[y]{x}
分数ab\frac{a}{b}\frac{a}{b}
上取整⌈\lceil、⌉\rceil\lceil、\rceil
下取整⌊\lfloor、⌋\rfloor\lfloor、\rfloor
大括号{\{,}\}\lbrace、\{,\rbrace、\}

逻辑​

名称展示代码
与∧\land\land
或∨\lor\lor
非¬\lnot\lnot
推出  ⟹  \implies,⇒\Rightarrow\implies,\Rightarrow
反推  ⟸  \impliedby,⇐\Leftarrow\impliedby,\Leftarrow
等价  ⟺  \iff,⇔\Leftrightarrow\iff,\Leftrightarrow
因为∵\because\because
所以∴\therefore\therefore

巨运算符​

求和​

15 Blatex
\sum_{i=1}^na_i
http://localhost:3000
∑i=1nai\sum_{i=1}^na_i

求积​

16 Blatex
\prod_{i=1}^na_i
http://localhost:3000
∏i=1nai\prod_{i=1}^na_i

积分​

23 Blatex
\int_a^bf(x)\mathrm{d}x
http://localhost:3000
∫abf(x)dx\int_a^bf(x)\mathrm{d}x

极限​

21 Blatex
\lim_{x\to\infty}f(x)
http://localhost:3000
lim⁡x→∞f(x)\lim_{x\to\infty}f(x)

多行公式​

递等式​

61 Blatex
\begin{aligned}
f(x) & =a \\
& =b \\
& =c
\end{aligned}
http://localhost:3000
f(x)=a=b=c\begin{aligned} f(x) & =a \\ & =b \\ & =c \end{aligned}

分段函数​

67 Blatex
f(x)=
\begin{cases}
a & x>0 \\
b & x=0 \\
c & x<0
\end{cases}
http://localhost:3000
f(x)={ax>0bx=0cx<0f(x)= \begin{cases} a & x>0 \\ b & x=0 \\ c & x<0 \end{cases}

线性方程组​

94 Blatex
\begin{cases}
a_1x+b_1y+c_1z=d_1 \\
a_2x+b_2y+c_2z=d_2 \\
a_3x+b_3y+c_3z=d_3
\end{cases}
http://localhost:3000
{a1x+b1y+c1z=d1a2x+b2y+c2z=d2a3x+b3y+c3z=d3\begin{cases} a_1x+b_1y+c_1z=d_1 \\ a_2x+b_2y+c_2z=d_2 \\ a_3x+b_3y+c_3z=d_3 \end{cases}

多行公式​

114 Blatex
\begin{array}{l}
\cos\pi=-1 \\
\sin^2\alpha+\cos^2\alpha=1 \\
\sin 2\alpha=2\sin\alpha\cos\alpha
\end{array}
http://localhost:3000
cos⁡π=−1sin⁡2α+cos⁡2α=1sin⁡2α=2sin⁡αcos⁡α\begin{array}{l} \cos\pi=-1 \\ \sin^2\alpha+\cos^2\alpha=1 \\ \sin 2\alpha=2\sin\alpha\cos\alpha \end{array}
tip

通过调整 \begin{array} 后面的参数设置对齐方式:

{l} 表示左对齐;{c} 表示居中对齐;{r} 表示右对齐。

矩阵​

134 Blatex
\begin{bmatrix}
1 & 0 & \dots & 0 \\
0 & 1 & \dots & 0 \\
\vdots & \vdots & \ddots & \vdots \\
0 & 0 & \dots & 1
\end{bmatrix}
http://localhost:3000
[10…001…0⋮⋮⋱⋮00…1]\begin{bmatrix} 1 & 0 & \dots & 0 \\ 0 & 1 & \dots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \dots & 1 \end{bmatrix}

更多功能​

边框​

40 Blatex
\boxed{x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}}
http://localhost:3000
x=−b±b2−4ac2a\boxed{x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}}

颜色​

42 Blatex
{\color{white}\colorbox{red}{FBI WARNING}}
http://localhost:3000
FBI WARNING{\color{white}\colorbox{red}{FBI WARNING}}

字体​

正体​

26 Blatex
g\approx 9.8\mathrm{m/s^2}
http://localhost:3000
g≈9.8m/s2g\approx 9.8\mathrm{m/s^2}

黑板粗体​

14 Blatex
x\in\mathbb{R}
http://localhost:3000
x∈Rx\in\mathbb{R}

自定义运算符​

26 Blatex
\operatorname{lcm}(6,8)=24
http://localhost:3000
lcm⁡(6,8)=24\operatorname{lcm}(6,8)=24

编号​

26 Blatex
\tag{1}(a+b)^2=a^2+2ab+b^2
http://localhost:3000
(a+b)2=a2+2ab+b2(1)\tag{1}(a+b)^2=a^2+2ab+b^2