圓州率
🌐

Feature Image

如何使用 LaTeX\LaTeX 數學模式

LaTeX\LaTeX, 數學
教你怎麼用 LaTeX\LaTeX 的數學模式。
   最後更新:

工具

推薦一些工具能快速上手LaTeX\LaTeX

  1. Overleaf:免安裝線上LaTeX\LaTeX編輯器,也有各式現成模板可以直接使用。
  2. Overleaf 教學網站LaTeX\LaTeX語法使用教學。
  3. Codecogs:數學方程式編輯器,提供可視化的介面,是不記得數學指令的新手福音。
  4. Table Generator:在LaTeX\LaTeX裡面打表格是一個相對麻煩的事情,有這個工具能快速幫你實現。
  5. List of LaTeX mathematical symbols:符號表。

建議設定

\documentclass{article}

% Aligning equations
\usepackage{amsmath}
\allowdisplaybreaks % 允許align煥頁

% Math fonts
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{mathrsfs}

\begin{document}
    Recommended settings
\end{document}
latex

接下來我們的內容會聚焦在數學模式,若無特別提及,則是指在\begin{document} … \end{document}裡操作

數學模式

數學模式分為2種,一種是「內文數學模式(Inline math mode)」,另一種是「展示數學模式(Display math mode)」。

差別在於內文數學模式會嘗試將數學符號縮小至與一般內文等高,否則會導致行高變化太多不好看

內文模式

行高相同 test test test test test i=1nxii!\sum_{i = 1}^{n} \frac{x^i}{i!} test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test 0f(x)dx\int_0^\infty f (x) dx test test test test test test test test test test test test test test test test test test test test test

展示模式

行高不同 test test test test test i=1nxii!\displaystyle \sum_{i = 1}^{n} \frac{x^i}{i!} test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test 0f(x)dx\displaystyle \int_0^\infty f (x) dx test test test test test test test test test test test test test test test test test test test test test

在內文模式中,我們會使用一個錢字符將內文包起來$ ... $,例如

$ f (x) = \sum_{i = 1}^{n} i^k $
latex

在展示模式中,我們會用兩個錢字符將內文包起來$$ ... $$,或者\begin{環境名稱} ... \end{環境名稱},例如

$$ f (x) = \sum_{i = 1}^{n} i^k $$

\begin{align}
    f (x) = \sum_{i = 1}^{n} i^k
\end{align}
latex

字體

一般來說內文會使用正體,數學模式內的符號會用斜體,例如f(x) = ax + b與f(x)=ax+bf(x) = ax + b的對比。而有公認定義的函數會用正體,例如sin(x)\sin (x)

我們有時候會需要在數學模式內輸入非數學符號,會希望用正體表示,可以用\text{…},例如

$$ f (x) > 0 \quad \text{for} \quad x > 0 $$
latex
f(x)>0forx>0 f (x) > 0 \quad \text{for} \quad x > 0

常用數學

接下來的操作,請直接放在$...$$$ … $$裡面。建議網站:Codecogs

操作

\pm \mp \times \dot \div±×÷\pm \mp \times \cdot \div
\cup \cap\cup \cap
\geq \leq \subset \supset \subseteq \supseteq\geq \leq \subset \supset \subseteq \supseteq

分數 frace

語法;\frac{分子}{分母}

ax+bc \frac{ax + b}{c}

根號 sqrt

語法:\sqrt{n}\sqrt[m]{n}

nnm \sqrt{n} \quad \sqrt[m]{n}

也能與其他符號結合,例如分數\sqrt{\frac{a}{b}}

ab \sqrt{\frac{a}{b}}

次方 ^

語法:a^b{a}^{b},例e^{-\frac{1}{2}}

abe12 a^b \quad e^{-\frac{1}{2}}

注意:次方符號預設只會將「下一個字元」放到次方位置上,例如123的456次方,要用{123}^{456}而非123^456

大型符號

語法:\sum \prod \int \bigcup \bigcap \lim

lim \sum \quad \prod \quad \int \quad \bigcup \quad \bigcap \quad \lim

上下標:\sum_{i = 1}^{n} \int_{a}^{b} \lim_{n \to \infty}

i=1nablimn \sum_{i = 1}^{n} \quad \int_{a}^{b} \quad \lim_{n \to \infty}

非英文符號

\alpha \beta \gamma \deltaαβγδ\alpha \beta \gamma \delta
\Gamma \DeltaΓΔ\Gamma \Delta

更多符號參考至LaTeX 符號表

大型括號

若想對大型符號做括號,例如分數的括號( \frac{a}{b} ),會發現括號太小

(ab) ( \frac{a}{b} )

語法:在括號左右兩側加上\left\right,括號就會跟著內容自動設定大小,例如

  1. \left| ... \right| ...\left| ... \right|
  2. \left( ... \right) (...)\left( ... \right)
  3. \left[ ... \right] [...]\left[ ... \right]
  4. \left\lbrace ... \right \rbrace {...}\left\lbrace ... \right \rbrace,由於大括號在LaTeX\LaTeX有重要功能,因此語法稍有不同
ad(ad)[ad]{ad} \left| \frac{a}{d} \right| \quad \left( \frac{a}{d} \right) \quad \left[ \frac{a}{d} \right] \quad \left\lbrace \frac{a}{d} \right \rbrace \quad

單邊括號

若想達成單邊括號,例如

axb) \left. \frac{ax}{b} \right)

語法:將不需要的那側,換成\left.\right.,例如\left. ... \right)

這項功能若組合則下標\left. \frac{ax}{b} \right|_{x = 0},則能做到微積分常使用的符號

axbx=0 \left. \frac{ax}{b} \right|_{x = 0}

多行操作 align

在展示數學模式中,若想達成多行對齊

(a+b)2b2=(a2+2ab+b2)b2=a2+2ab=a(a+2b) \begin{align*} (a + b)^2 - b^2 & = (a^2 + 2ab + b^2) - b^2 \\ & = a^2 + 2 ab \\ & = a (a + 2b) \end{align*}

可以使用alignalign*環境,差別在有align*無編號,而align

(a+b)2b2=(a2+2ab+b2)b2=a2+2ab=a(a+2b) \begin{align} (a + b)^2 - b^2 & = (a^2 + 2ab + b^2) - b^2 \\ & = a^2 + 2 ab \\ & = a (a + 2b) \end{align}

語法:以&作為對齊點

\begin{align} 
    (a + b)^2 - b^2 & = (a^2 + 2ab + b^2) - b^2 \\
    & = a^2 + 2 ab \\
    & = a (a + 2b)
\end{align}
latex

陣列 array

想要達成陣列或多行算式,都能用array環境做到

   ab   cd \begin{array}{cc}     a & b \\     c & d \end{array}

語法:以&作為對齊點。其中有個參數{cc}c代表「置中」、l表示「置左」、r表示「置右」。而第一個c表示第一行;第二個c表示第二行,以此類推。

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

若想做到「第一行置左,第二行置中,第三行置右」,則可以

123456789101112114151617 \begin{array}{lcr} 1 & 23 & 456 \\ 78910 & 11121 & 14151617 \end{array}
$$ \begin{array}{lcr}
    1 & 23 & 456 \\
    78910 & 11121 & 14151617
\end{array} $$
latex

矩陣

加上水平逗點\cdots、垂直逗點\vdots與斜線逗點\ddots

\cdots \quad \vdots \quad \ddots

可以寫出下列陣列

$$ \begin{array}{cccc}
    a_{11} & a_{12} & \cdots & a_{1n} \\
    a_{21} & a_{22} & \cdots & a_{2n} \\
    \vdots & \vdots & \ddots & \vdots \\
    a_{n1} & a_{n2} & \cdots & a_{nn} 
\end{array} $$
latex
a11a12a1na21a22a2nan1an2ann \begin{array}{cccc} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n1} & a_{n2} & \cdots & a_{nn} \end{array}

最後在左右兩側加上大括號,就能寫出線性代數常用的矩陣形式

$$ \left( \begin{array}{cccc}
    a_{11} & a_{12} & \cdots & a_{1n} \\
    a_{21} & a_{22} & \cdots & a_{2n} \\
    \vdots & \vdots & \ddots & \vdots \\
    a_{n1} & a_{n2} & \cdots & a_{nn} 
\end{array} \right) $$
latex
(a11a12a1na21a22a2nan1an2ann) \left( \begin{array}{cccc} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n1} & a_{n2} & \cdots & a_{nn} \end{array} \right)

多行函數

以絕對值函數為例

$$ \begin{array}{lll}
    x & \text{if} & x \geq 0 \\
    -x & \text{if} & x < 0
\end{array} $$
latex
xifx0xifx<0 \begin{array}{lll} x & \text{if} & x \geq 0 \\ -x & \text{if} & x < 0 \end{array}

再加上一個單側大括號\left\lbrace ... \right.

$$ |x| = 
\left\lbrace \begin{array}{lll}
    x & \text{if} & x \geq 0 \\
    -x & \text{if} & x < 0
\end{array} \right. $$
latex
x={xifx0xifx<0 |x| = \left\lbrace \begin{array}{lll} x & \text{if} & x \geq 0 \\ -x & \text{if} & x < 0 \end{array} \right.

Amsmath Package

介紹一個常用且好用的包,在設定區引入amsmath

\usepackage{amsmath}
latex

矩陣 matrix

amsmath的矩陣,內容預設置中,且只要微幅修改就能改變外圈括號的類型,例如pmatrix

\begin{pmatrix}
  a & b \\
  c & d \\
\end{pmatrix}
latex
(abcd) \begin{pmatrix} a & b \\ c & d \\ \end{pmatrix}

另外還有數種類型的括號可以選,只要將pmatrix換成下列任一項

  1. pmatrix

    (abcd) \begin{pmatrix} a & b \\ c & d \\ \end{pmatrix}
  2. bmatrix

    [abcd] \begin{bmatrix} a & b \\ c & d \\ \end{bmatrix}
  3. Bmatrix

    {abcd} \begin{Bmatrix} a & b \\ c & d \\ \end{Bmatrix}
  4. vmatrix

    abcd \begin{vmatrix} a & b \\ c & d \\ \end{vmatrix}
  5. Vmatrix

    abcd \begin{Vmatrix} a & b \\ c & d \\ \end{Vmatrix}

多行 cases

再以絕對值為範例,用cases就能快速的打出來

|x| = 
\begin{cases}
    x & \text{if } x \geq 0 \\
    -x & \text{if } x < 0
\end{cases} 
latex
{xif x0xif x<0 \begin{cases} x & \text{if } x \geq 0 \\ -x & \text{if } x < 0 \end{cases}