常用的 LaTeX 數學式語法

太雜了總是忘記😕整理起來!

wyatthoho
9 min readJan 21, 2024
Photo by Thomas T on Unsplash

🛠️ 使用套件

我在 LaTeX 撰寫數學式會需要用到以下幾組套件:

  • amsmath: 用來建立各種常見數學式的環境 🔗
  • amssymb: 提供數學式所需的字體、符號等 🔗
  • mathtools: 基於 amsmath 的不足進行改進之套件 🔗

因此 .tex 的文件架構如下。

\documentclass{article}

% Packages for mathematical symbols and formatting
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}

% Other settings or customizations
% ...

\begin{document}

% Content of the document starts here
% ...

\end{document}

🎎 LaTeX 數學式的兩種模式

LaTeX 的數學式的長相與一般文字不同,而數學式又分為 Inline 和 Display 兩種模式, Inline 是呈現在文字敘述當中的數學式,而 Display 則是單獨呈現,會與上下文區隔出不同空間。它們支援的語法如下:

Inline

  • \(...\)
  • $...$
  • \begin{math}...\end{math}

Display

  • \[...\] ( 不會附上編號 )
  • \begin{displaymath}...\end{displaymath} ( 不會附上編號 )
  • \begin{eqution}...\end{equation} ( 附上編號 )
  • \begin{eqution*}...\end{equation*} ( 不會附上編號 )
  • $$...$$ ( 不建議使用 )

請不要使用 TeX 裡頭的 $$...$$ 指令撰寫 Display 數學式,這在 LaTeX 並沒有完整去重定義它,這在某些 LaTeX 指令的效果上會沒有作用。(大家來學 LaTeX)

以下是一個範例 ( 參考自 Overleaf ) :

\documentclass{article}
\begin{document}
The mass-energy equivalence is described by the famous equation

\[E=mc^2\]

discovered in 1905 by Albert Einstein.
In natural units ($c = 1$), the formula expresses the identity

\begin{equation}
E=m
\end{equation}
\end{document}
Basic math example, Overleaf

這段文章中的 c = 1 即屬 Inline 模式,而 E = mc2 和 E = m 則屬 Display 模式。

😶‍🌫️ 希臘字母

小寫希臘字母
大寫希臘字母

👒字體型態變化

其中 \mathbb 是透過 amssymb 套件才能在數學式環境中執行。

💁‍♀️上下標

🐾分數

🍡陣列

\[
\left(
\begin{array}{ccc}
a & b & c \\
d & e & f \\
g & h & i
\end{array}
\right)
\]

指令當中出現了 {ccc} ,這三個字母恰好對應三個欄位 (column) 內容的排列方式,可用的控制選項如下:

  1. l: 置左
  2. c: 置中
  3. r: 置右

另外,陣列的分隔符應置於 \left\right 之後,可用的分隔符包含:

若指定為 . 表示該側無分隔符。可以更進一步在控制選項中放置 | 進行垂直分隔,如下。

\[
\left[
\begin{array}{cc|r}
3 & 4 & 5 \\
1 & 3 & 729
\end{array}
\right]
\]

🧮運算子

📐標準函式

💥 常用運算

➡️ 箭號

💦 點點點

以下是點點點的常用情境:

\[
\left[
\begin{array}{ccc}
\sigma_{11} & \cdots & \sigma_{1n} \\
\vdots & \ddots & \vdots \\
\sigma_{n1} & \cdots & \sigma_{nn}
\end{array}
\right]
\]

\(\sigma_{ij} = \sigma_{ji}\), for \(i, j = 1, 2, \ldots, n\)

📝多行方程式

Split

以下範例參考自 Overleaf

\[
\begin{split}
A &= \frac{\pi r^2}{2}\\
&= \frac{1}{2} \pi r^2
\end{split}
\]

使用 split 語法可以用 & 來對齊兩個以上的式子,並以 \\ 進行分行,但此語法有一些限制。例如一行只能放一個式子,且必須在 Display 模式中使用。

Align

若使用 align 則更加彈性,以下參考自 Overleaf

\begin{align*}
x&=y & w&=z & a&=b+c\\
2x&=-y & 3w&=\frac{1}{2}z & a&=b\\
-4+5x&=2+y & w+2&=-1+w & ab&=cb
\end{align*}

其中 * 表示不須替數學式編號。

Gather

若不需針對特定字元進行對齊排列,那麼使用 gather 即可,式子們會自動置中對齊。以下參考自 Overleaf

\begin{gather*} 
2x - 5y = 8 \\
3x^2 + 9y = 3a + c
\end{gather*}

Cases

使用 cases 撰寫分段函式 (piecewise functions) 。

\[
f(x) =
\begin{cases}
e^{-2x} & \text{if } x \geq 0\\
0 & \text{otherwise}
\end{cases}
\]

若方程式中含有積分或分數等內容,則可以使用 dcase 來呈現。以下比較了 casedcase 的差異。

\[
\begin{cases}
E=mc^2 & c \approx 3.00\times 10^{8} \text{ m/s} \\
\int x-3\, dx & \text{Integral is text style}
\end{cases}
\]
\[
\begin{dcases}
E=mc^2 & c \approx 3.00\times 10^{8} \text{ m/s} \\
\int x-3\, dx & \text{Integral is display style}
\end{dcases}
\]

指令 dcase 是由 mathtools 套件提供。

🗒 進階呈現方式

Displaystyle

在 Inline 模式或是 Display 模式的某些情況, LaTeX 會以較緊湊的方式呈現數學式,若不喜歡這樣的外觀可以加上 \displaystyle 確保式子完整呈現。

\begin{align*}
I &= \frac{n}{\sum_{i=1}^n \sum_{j=1}^n w_{ij}}
\frac{\sum_{i=1}^n \sum_{j=1}^n w_{ij}(x_i - \bar{x})(x_j - \bar{x})}
{\sum_{i=1}^n (x_i - \bar{x})^2}\\
I &= \frac{n}{\displaystyle\sum_{i=1}^n \sum_{j=1}^n w_{ij}}
\frac{\displaystyle\sum_{i=1}^n \sum_{j=1}^n w_{ij}(x_i - \bar{x})(x_j - \bar{x})}
{\displaystyle\sum_{i=1}^n (x_i - \bar{x})^2}
\end{align*}

Verb

使用 \verb|...| 讓特定內容呈現原始的純文字,而不會進行任何格式化。

Typing
\verb|\[x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}\]|
yields
\[x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}\]

Text

在數學式環境中輸入的文字會預設為變數名稱,因此以斜體字呈現。若輸入的文字為敘述用途而非變數名稱,那麼應將其放入 \text{...} 指令。

\begin{align*}
BMI &= \frac{weight in kilogram}{height in meter^2}\\
\text{BMI} &= \frac{\text{weight in kilogram}}{\text{height in meter}^2}
\end{align*}

--

--

wyatthoho

在混亂的宇宙裡,我透過寫程式來認識秩序並建立安定。wyatthoho@gmail.com