Latex语法
0 常见
插入公式
$\LaTeX$ 的数学公式有两种:行中公式和行间公式。行中公式放在文中与其它文字混编,独立公式单独成行。
行中公式:$ function $
$function$
行间公式:$$ function $$
$$function$$
功能符号
{ }
:单个模块{\rm d}
和\mathrm{d}
等价:将括号内的字母由数学斜体变为正体\textstyle
:让公式以行内公式格式显示\displaystyle
:让公式以行间公式的格式排版,可以防止压扁\stackrel
:多行极限\tag{1}
:放在公式末尾,给公式编号
1 函数、符号及特殊字符
标准函数
指数
\exp_a b = a^b, \exp b = e^b, 10^m
${\displaystyle \exp _{a}b=a^{b},\exp b=e^{b},10^{m}}$
对数
\ln c, \lg d = \log e, \log_{10} f
${\displaystyle \ln c,\lg d=\log e,\log _{10}f}$
三角函数
\sin a, \cos b, \tan c, \cot d, \sec e, \csc f
${\displaystyle \sin a,\cos b,\tan c,\cot d,\sec e,\csc f}$
\arcsin a, \arccos b, \arctan c
${\displaystyle \arcsin a,\arccos b,\arctan c}$
\arccot d, \arcsec e, \arccsc f
${\displaystyle \operatorname {arccot} d,\operatorname {arcsec} e,\operatorname {arccsc} f}$
最大值,最小值
`\min x, \max y
${\displaystyle \min x,\max y}$
\min(x,y), \max(x,y)
${\displaystyle \min(x,y),\max(x,y)}$
极限
\lim u
${\displaystyle \lim u}$
\lim_{x \to \infty} \frac{1}{n(n+1)}
${\displaystyle \lim_{x \to \infty} \frac{1}{n(n+1)}}$
\lim_{n \to \infty}x_n
${\displaystyle \lim {n\to \infty}x{n}}$
\begin{matrix} \lim_{n \to \infty}x_n \end{matrix}
${\displaystyle {\begin{matrix}\lim {n\to \infty }x{n}\end{matrix}}}$
模长
\lvert z \rvert
$\lvert z \rvert$
\lVert z \rVert
$\lVert z \rVert$
微分及导数
dt, \mathrm{d}t, \partial t, \nabla\psi
${\displaystyle dt,\mathrm {d} t,\partial t,\nabla \psi }$
x'
${\displaystyle x’}$
x^\prime
${\displaystyle x^{\prime}}$
dy/dx, \mathrm{d}y/\mathrm{d}x, \frac{dy}{dx}, \frac{\mathrm{d}y}{\mathrm{d}x}, \frac{\partial^2}{\partial x_1\partial x_2}y
$\partial$ 偏导数
${\displaystyle dy/dx,\mathrm {d} y/\mathrm {d} x,{\frac {dy}{dx}},{\frac {\mathrm {d} y}{\mathrm {d} x}},{\frac {\partial ^{2}}{\partial x_{1}\partial x_{2}}}y}$
\prime, \backprime, f^\prime, f', f'', f^{(3)}, \dot y, \ddot y
${\displaystyle \prime ,\backprime ,f^{\prime},f’,f’’,f^{(3)}!,{\dot {y}},{\ddot {y}}}$
取模
a \bmod b
${\displaystyle a \bmod b}$
根号
\sqrt{2}, \sqrt[3]{\frac{x^3+y^3}{2}}
${\displaystyle {\sqrt {2}},{\sqrt[{3}]{\frac {x^{3}+y^{3}}{2}}}}$
加减乘除
+, -, \pm, \mp, \dotplus
${\displaystyle +,-,\pm ,\mp ,\dotplus}$
\times, \div, /, \backslash
${\displaystyle \times ,\div ,/,\backslash}$
点乘
`\cdot
${\displaystyle a \cdot b }$
集合
中括号
`{ }
${\displaystyle {}}$
关系符号
等号=, \ne, \neq, \equiv, \not\equiv
${\displaystyle =,\neq ,\neq ,\equiv ,\not \equiv}$
\approx
约等
${\displaystyle \approx }$
\sim, \nsim, \backsim, \thicksim, \simeq, \backsimeq, \eqsim, \cong, \ncong
${\displaystyle \sim ,\nsim ,\backsim ,\thicksim ,\simeq ,\backsimeq ,\eqsim ,\cong ,\ncong}$
不等式
< \leqslant, \nleqslant
${\displaystyle < ,\leqslant ,\nleqslant }$
> \geqslant, \ngeqslan
${\displaystyle >,\geqslant ,\ngeqslant}$
角度
#垂直\perp, \angle, \sphericalangle, \measuredangle, 45^\circ
${\displaystyle \perp ,\angle ,\sphericalangle ,\measuredangle ,45^{\circ}}$
箭头
\Rightarrow, \nRightarrow, \Leftarrow, \nLeftarrow, \Leftrightarrow
$\Rightarrow, \nRightarrow, \Leftarrow, \nLeftarrow, \Leftrightarrow$
\rightarrow \to, \nrightarrow, \longrightarrow
${\displaystyle \rightarrow \to ,\nrightarrow ,\longrightarrow}$
\leftarrow \gets, \nleftarrow, \longleftarrow
${\displaystyle \leftarrow \gets ,\nleftarrow ,\longleftarrow}$
\leftrightarrow, \nleftrightarrow, \longleftrightarrow
${\displaystyle \leftrightarrow ,\nleftrightarrow ,\longleftrightarrow}$
左箭头\overleftarrow{a b}
${\displaystyle {\overleftarrow {ab}}}$
右箭头\overrightarrow{c d}
${\displaystyle {\overrightarrow {cd}}}$
双箭头\overleftrightarrow{a b}
${\displaystyle {\overleftrightarrow {ab}}}$
省略号
\ldots
表示与文本底线对齐的省略号,\cdots
表示与文本中线对齐的省略号。
$\ldots, \cdots$
上弧
\widehat{e f g}
${\displaystyle {\widehat {efg}}}$
\overset{\frown} {AB}
${\displaystyle {\overset {\frown}{AB}}}$
上括号下括号
\overbrace{1+2+\cdots+100}
${\displaystyle \overbrace {1+2+\cdots +100} }$
\begin{matrix} 5050 \\ \overbrace{ 1+2+\cdots+100 } \end{matrix}
${\displaystyle {\begin{matrix}5050\\overbrace {1+2+\cdots +100} \end{matrix}}}$
\underbrace{a+b+\cdots+z}
${\displaystyle \underbrace {a+b+\cdots +z} }$
\begin{matrix} \underbrace{ a+b+\cdots+z } \\ 26 \end{matrix}
${\displaystyle {\begin{matrix}\underbrace {a+b+\cdots +z} \26\end{matrix}}}$
上划线下划线
overline
$\overline{a}$
\underline
$\underline{b}$
上下标
上标
a^2
${\displaystyle a^{2}}$
a^{2+2}
${\displaystyle a^{2+2}}$
下标
a_2
${\displaystyle a_{2}}$
a_{i,j}
${\displaystyle a_{i,j}}$
结合上下标
x_2^3
${\displaystyle x_{2}^{3}}$
前置上下标
{}_1^2\!X_3^4
${\displaystyle {}{1}^{2}!X{3}^{4}}$
向量
\vec{c}
$\vec{c}$
括号
()
、[]
和 |
表示符号本身,使用 \{\}
来表示 {}
。
短括号
\frac{1}{2}
${\displaystyle ({\frac {1}{2}})}$
长括号
\left(\frac{1}{2} \right)
${\displaystyle \left({\frac {1}{2}}\right)}$
使用 \left
和 \right
来创建自动匹配高度的 (圆括号),[方括号] 和 {花括号} 。
圆括号,小括号
\left( \frac{a}{b} \right)
${\displaystyle \left({\frac {a}{b}}\right)}$
方括号,中括号
\left[ \frac{a}{b} \right]
${\displaystyle \left[{\frac {a}{b}}\right]}$
花括号,大括号
\left\{ \frac{a}{b} \right\}
${\displaystyle \left<!–swig17–>\right}}$
角括号
\left \langle \frac{a}{b} \right \rangle
${\displaystyle \left\langle {\frac {a}{b}}\right\rangle }$
混合括号
\left[ 0,1 \right)
${\displaystyle \left[0,1\right)}$
单竖线,绝对值
\left| \frac{a}{b} \right|
${\displaystyle \left| \frac{a}{b} \right|}$
双竖线,范
\left \| \frac{a}{b} \right \|
${\displaystyle \left|{\frac {a}{b}}\right|}$
取整函数
\left \lfloor \frac{a}{b} \right \rfloor
${\displaystyle \left\lfloor {\frac {a}{b}}\right\rfloor }$
取顶函数
\left \lceil \frac{c}{d} \right \rceil
${\displaystyle \left\lceil {\frac {c}{d}}\right\rceil }$
如果括号只有一边,要用 \left.
或 \right.
匹配另一边。
单左括号
\left \{\frac{a}{b} \right.
${\displaystyle \left<!–swig18–>\right.}$
单右括号
\left. \frac{a}{b} \right \}
${\displaystyle \left.{\frac {a}{b}}\right}}$
可以使用
\big, \Big, \bigg, \Bigg
控制括号的大小,比如代码\Bigg ( \bigg [ \Big \{ \big \langle \left | \| \frac{a}{b} \| \right | \big \rangle \Big \} \bigg ] \Bigg )
$$\Bigg ( \bigg [ \Big { \big \langle \left | | \frac{a}{b} | \right | \big \rangle \Big } \bigg ] \Bigg )$$空格
quad 空格
\alpha\quad\beta
${\displaystyle \alpha \quad \beta}$
\alpha\qquad\beta
${\displaystyle \alpha \qquad \beta}$
大空格
\alpha\ \beta
${\displaystyle \alpha \ \beta}$
中等空格
\alpha\;\beta
${\displaystyle \alpha ;\beta}$
小空格
\alpha\,\beta
${\displaystyle \alpha ,\beta}$
紧贴
\alpha\!\beta
${\displaystyle \alpha !\beta}$
2 算术运算
累加累乘
求和(累加)
\sum_{k=1}^N k^2
${\displaystyle \sum _{k=1}^{N}k^{2}}$
\begin{matrix} \sum_{k=1}^N k^2 \end{matrix}
${\displaystyle {\begin{matrix}\sum _{k=1}^{N}k^{2}\end{matrix}}}$
行间公式使用 \sum\limits
可以让上下限固定在符号上下方,其他类似的符号也适用:
${\displaystyle {\begin{matrix}\sum\limits _{k=1}^{N}k^{2}\end{matrix}}}$
求积(累乘)
\prod_{i=1}^N x_i
${\displaystyle \prod {i=1}^{N}x{i}}$
\begin\limits{matrix} \prod_{i=1}^N x_i \end{matrix}
${\displaystyle {\begin{matrix}\prod {i=1}^{N}x{i}\end{matrix}}}$
积分
积分
\int_{-N}^{N} e^x\, {\rm d}x
${\displaystyle \int _{-N}^{N}e^{x},{\rm d} x}$
本例中 \,
和 {\rm d}
部分可省略,但建议加入,能使式子更美观。{\rm d}
可以用\mathrm{d}
等价替换。
\begin{matrix} \int_{-N}^{N} e^x\, \mathrm{d}x \end{matrix}
(矩阵中积分符号变小)
${\displaystyle {\begin{matrix}\int _{-N}^{N}e^{x},\mathrm {d} x\end{matrix}}}$
双重积分
\iint_{D}^{W} \, \mathrm{d}x\,\mathrm{d}y
${\displaystyle \iint _{D}^{W},\mathrm {d} x,\mathrm {d} y}$
三重积分
\iiint_{E}^{V} \, \mathrm{d}x\,\mathrm{d}y\,\mathrm{d}z
${\displaystyle \iiint _{E}^{V},\mathrm {d} x,\mathrm {d} y,\mathrm {d} z}$
闭合的曲线、曲面积分
\oint_{C} x^3\, \mathrm{d}x + 4y^2\, \mathrm{d}y
${\displaystyle \oint _{C}x^{3},\mathrm {d} x+4y^{2},\mathrm {d} y}$
分数
通常使用 \frac {分子} {分母}
命令产生一个分数,分数可嵌套。
便捷情况可直接输入 \frac ab
来快速生成一个 $\frac ab$ 。
如果分式很复杂,亦可使用 分子 \over 分母
命令,此时分数仅有一层。
\tfrac
设置分数为 textstyle
\dfrac
设置分数为 displaystyle
\frac
根据上下文决定使用 \tfrac
还是 \dfrac
\cfrac
用于表示连续分数
分数
\frac{2}{4}=0.5
${\displaystyle {\frac {2}{4}}=0.5}$
小型分数
\tfrac{2}{4} = 0.5
${\displaystyle {\tfrac {2}{4}}=0.5}$
连分式(大型嵌套分式)\cfrac
用于表示连续分数
\cfrac{2}{c + \cfrac{2}{d + \cfrac{2}{4}}} = a
${\displaystyle {\cfrac {2}{c+{\cfrac {2}{d+{\cfrac {2}{4}}}}}}=a}$
大型不嵌套分式
\dfrac{2}{4} = 0.5 \qquad \dfrac{2}{c + \dfrac{2}{d + \dfrac{2}{4}}} = a
${\displaystyle {\dfrac {2}{4}}=0.5\qquad {\dfrac {2}{c+{\dfrac {2}{d+{\dfrac {2}{4}}}}}}=a}$
二项式
二项式系数
\dbinom{n}{r}=\binom{n}{n-r}=\mathrm{C}_n^r=\mathrm{C}_n^{n-r}
${\displaystyle {\dbinom {n}{r}}={\binom {n}{n-r}}=\mathrm {C} _{n}^{r}=\mathrm {C} _{n}^{n-r}}$
小型二项式系数
\tbinom{n}{r}=\tbinom{n}{n-r}=\mathrm{C}_n^r=\mathrm{C}_n^{n-r}
${\displaystyle {\tbinom {n}{r}}={\tbinom {n}{n-r}}=\mathrm {C} _{n}^{r}=\mathrm {C} _{n}^{n-r}}$
大型二项式系数
\binom{n}{r}=\dbinom{n}{n-r}=\mathrm{C}_n^r=\mathrm{C}_n^{n-r}
${\displaystyle {\binom {n}{r}}={\dbinom {n}{n-r}}=\mathrm {C} _{n}^{r}=\mathrm {C} _{n}^{n-r}}$
在以 e 为底的指数函数、极限和积分中尽量不要使用 \frac
符号:它会使整段函数看起来很怪,而且可能产生歧义。也正是因此它在专业数学排版中几乎从不出现。
横着写这些分式,中间使用斜线间隔 /
(用斜线代替分数线)。
- 例子:
1 | \begin{array}{cc} |
- 显示:
$$\begin{array}{cc} \mathrm{Bad} & \mathrm{Better} \ \hline \ e^{i\frac{\pi}2} \quad e^{\frac{i\pi}2}& e^{i\pi/2} \ \int_{-\frac\pi2}^\frac\pi2 \sin x,dx & \int_{-\pi/2}^{\pi/2}\sin x,dx \ \end{array}$$
矩阵
1 | \begin{类型} |
类型可以是:矩阵 matrix
pmatrix
bmatrix
Bmatrix
vmatrix
Vmatrix
、条件表达式 cases
、多行对齐方程式 aligned
、数组 array
。
在公式内容中:在每一行中插入 &
来指定需要对齐的内容,在每行结尾处使用 \\
换行。
无框矩阵
在开头使用 begin{matrix}
,在结尾使用 end{matrix}
,在中间插入矩阵元素,每个元素之间插入 &
,并在每行结尾处使用 \\
。
1 | \begin{matrix} |
${\displaystyle {\begin{matrix}x&y\z&v\end{matrix}}}$
有框矩阵
在开头将 matrix
替换为 pmatrix
bmatrix
Bmatrix
vmatrix
Vmatrix
。
1 | \begin{vmatrix} |
${\displaystyle {\begin{vmatrix}x&y\z&v\end{vmatrix}}}$
1 | \begin{Vmatrix} |
${\displaystyle {\begin{Vmatrix}x&y\z&v\end{Vmatrix}}}$
使用 \cdots
$\cdots$ , \ddots
$\ddots$ , \vdots
$\vdots$ 来输入省略符号。
1 | \begin{bmatrix} |
${\displaystyle {\begin{bmatrix}0&\cdots &0\\vdots &\ddots &\vdots \0&\cdots &0\end{bmatrix}}}$
1 | \begin{Bmatrix} |
${\displaystyle {\begin{Bmatrix}x&y\z&v\end{Bmatrix}}}$
1 | \begin{pmatrix} |
${\displaystyle {\begin{pmatrix}x&y\z&v\end{pmatrix}}}$
增广矩阵
1 | $$ |
$$
A\rightarrow
\left [
\begin{array}{c:c}
\begin{matrix}
1 & 1 & -1 & 1 \
0 & 2 & 1 & -6 \
0 & 0 & 0 & 0
\end{matrix}&
\begin{matrix}
-2 \
3 \
0
\end{matrix}
\end{array}
\right ]
$$
条件表达式
条件表达式
使用 \text {文字}
来添加注释文本(注释文本不会被识别为公式,不用斜体显示)。\text {文字}
中仍可以使用 $公式$
插入其它公式。
1 | f(n) = |
${\displaystyle f(n)={\begin{cases}n/2,&{\text{if }}n{\text{ is even}}\3n+1,&{\text{if }}n{\text{ is odd}}\end{cases}}}$
多行等式、同余式
人们经常想要一列整齐且居中的方程式序列。使用 \begin{aligned}…\end{aligned}
。
1 | \begin{aligned} |
${\displaystyle {\begin{aligned}f(x)&=(m+n)^{2}\&=m^{2}+2mn+n^{2}\\end{aligned}}}$
方程组
1 | \begin{cases} |
$${\displaystyle {\begin{cases}3x+5y+z\7x-2y+4z\-6x+3y+2z\end{cases}}}$$
或
1 | \left\{\begin{aligned} |
$$\left{\begin{aligned} 3x + 5y + z \ 7x - 2y + 4z \ -6x + 3y + 2z \end{aligned}\right.$$
3 数组与表格
通常,一个格式化后的表格比单纯的文字或排版后的文字更具有可读性。数组和表格均以 \begin{array}
开头,并在其后定义列数及每一列的文本对齐属性,c
l
r
分别代表居中、左对齐及右对齐。若需要插入垂直分割线,在定义式中插入 |
,若要插入水平分割线,在下一行输入前插入 \hline
。与矩阵相似,每行元素间均须要插入 &
,每行元素以 \\
结尾,最后以 \end{array}
结束数组。
- 例子:
1 | \begin{array}{c|lcr} |
- 显示:
$$\begin{array}{c|lcr} n & \text{左对齐} & \text{居中对齐} & \text{右对齐} \ \hline 1 & 0.24 & 1 & 125 \ 2 & -1 & 189 & -8 \ 3 & -20 & 2000 & 1+10i \end{array}$$
- 例子:
1 | \begin{array}{lcl} |
- 显示:
${\displaystyle {\begin{array}{lcl}z&=&a\f(x,y,z)&=&x+y+z\end{array}}}$
- 例子:
1 | \begin{array}{lcr} |
- 显示:
${\displaystyle {\begin{array}{lcr}z&=&a\f(x,y,z)&=&x+y+z\end{array}}}$
- 例子:
1 | \begin{array}{ccc} |
- 显示:
$${\displaystyle {\begin{array}{ccc}a&b&S\\hline 0&0&1\0&1&1\1&0&1\1&1&0\\end{array}}}$$
4 字体
输入 \小写希腊字母英文全称
和 \首字母大写希腊字母英文全称
来分别输入小写和大写希腊字母。
\Alpha \Beta \Gamma \Delta \Epsilon \Zeta \Eta \Theta
${\displaystyle \mathrm {A} \mathrm {B} \Gamma \Delta \mathrm {E} \mathrm {Z} \mathrm {H} \Theta }$
\Iota \Kappa \Lambda \Mu \Nu \Xi \Omicron \Pi
${\displaystyle \mathrm {I} \mathrm {K} \Lambda \mathrm {M} \mathrm {N} \mathrm {O} \Xi \Pi }$
\Rho \Sigma \Tau \Upsilon \Phi \Chi \Psi \Omega
${\displaystyle \mathrm {P} \Sigma \mathrm {T} \Upsilon \Phi \mathrm {X} \Psi \Omega }$
\alpha \beta \gamma \delta \epsilon \zeta \eta \theta
${\displaystyle \alpha \beta \gamma \delta \epsilon \zeta \eta \theta}$
\iota \kappa \lambda \mu \nu \omicron \xi \pi
${\displaystyle \iota \kappa \lambda \mu \nu \mathrm {o} \xi \pi }$
\rho \sigma \tau \upsilon \phi \chi \psi \omega
${\displaystyle \rho \sigma \tau \upsilon \phi \chi \psi \omega}$
部分字母有变量专用形式,以 \var-
开头。
\varepsilon \digamma \varkappa \varpi
${\displaystyle \varepsilon \digamma \varkappa \varpi}$
\varrho \varsigma \vartheta \varphi
${\displaystyle \varrho \varsigma \vartheta \varphi}$
5 颜色
方法一:
`\color