📝

KaTeXの数式環境一覧

2024/03/02に公開

はじめに

KaTeXでは数式環境を使って行列、表、場合分け、方程式などを表現できます。
この記事では、KaTeXに実装されている全ての数式環境について説明します。

https://katex.org/docs/supported

一覧表

環境 引数 結果
matrix 行列
matrix* [lcrから一つ] 行列
pmatrix (行列)
pmatrix* [lcrから一つ] (行列)
bmatrix [行列]
bmatrix* [lcrから一つ] [行列]
vmatrix |行列|
vmatrix* [lcrから一つ] |行列|
Vmatrix ‖行列‖
Vmatrix* [lcrから一つ] ‖行列‖
Bmatrix {行列}
Bmatrix* [lcrから一つ] {行列}
smallmatrix 小さな行列
array {lcr|:の重複順列}
darray {lcr|:の重複順列} ディスプレイスタイルの表
subarray {lcから一つ} 小さな表
cases {場合分け
dcases {ディスプレイスタイルの場合分け
rcases 場合分け}
drcases ディスプレイスタイルの場合分け}
split 行分割(インライン不可)
equation 番号付き方程式(インライン不可)
equation* 番号なし方程式(インライン不可)
align 番号付き&揃え(スペース付き、インライン不可)
align* 番号なし&揃え(スペース付き、インライン不可)
aligned 番号なし&揃え(スペース付き)
alignat {&の数/2} 番号付き&揃え(インライン不可)
alignat* {&の数/2} 番号なし&揃え(インライン不可)
alignedat {&の数/2} 番号なし&揃え
gather 番号付き中央揃え(インライン不可)
gather* 番号なし中央揃え(インライン不可)
gathered 番号なし中央揃え
CD 可換図式

共通事項

環境は\begin{環境名}で始まり\end{環境名}で終わります。

改行には\\\cr、および\\[distance]を使います。distance{1em}などのKaTeX単位です。

環境内では平行実線\hlineおよび平行点線\hdashlineが使えます。

\arraystretchは数式環境で通常の行の高さに掛けられる倍率です。\arraystretchの定義が全体に波及することを望まないなら、{}で定義を囲んで閉じ込めます。

{
    \def\arraystretch{1.5}
        \begin{array}{c:c:c}
        a & b & c \\ \hline
        d & e & f \\
        \hdashline
        g & h & i
    \end{array}
}
{ \def\arraystretch{1.5} \begin{array}{c:c:c} a & b & c \\ \hline d & e & f \\ \hdashline g & h & i \end{array} }

matrix系 - 行列

matrixpatrixbatrixvatrixVatrixBatrixは行列です。最初の文字に応じて括弧の種類が決まります。

\begin{matrix}
    a & b \\
    c & d
\end{matrix} \quad
\begin{pmatrix} % 丸括弧
    a & b \\
    c & d
\end{pmatrix} \quad
\begin{bmatrix} % 角括弧
    a & b \\
    c & d
\end{bmatrix} \quad
\begin{vmatrix} % 縦棒
    a & b \\
    c & d
\end{vmatrix} \quad
\begin{Vmatrix} % 二重縦棒
    a & b \\
    c & d
\end{Vmatrix} \quad
\begin{Bmatrix} % 波括弧
    a & b \\
    c & d
\end{Bmatrix}
\begin{matrix} a & b \\ c & d \end{matrix} \quad \begin{pmatrix} a & b \\ c & d \end{pmatrix} \quad \begin{bmatrix} a & b \\ c & d \end{bmatrix} \quad \begin{vmatrix} a & b \\ c & d \end{vmatrix} \quad \begin{Vmatrix} a & b \\ c & d \end{Vmatrix} \quad \begin{Bmatrix} a & b \\ c & d \end{Bmatrix}

これらの環境名に*を付けると揃え位置を指定できます。[l]は左揃え、[c]は中央揃え、[r]は右揃えです。

\begin{vmatrix*}[l]
    a_{11} - \lambda & a_{12} & a_{13} \\
    a_{21} & a_{22} - \lambda & a_{23} \\
    a_{31} & a_{32} & a_{33} - \lambda
\end{vmatrix*} \quad
\begin{vmatrix*}[c]
    a_{11} - \lambda & a_{12} & a_{13} \\
    a_{21} & a_{22} - \lambda & a_{23} \\
    a_{31} & a_{32} & a_{33} - \lambda
\end{vmatrix*} \quad
\begin{vmatrix*}[r]
    a_{11} - \lambda & a_{12} & a_{13} \\
    a_{21} & a_{22} - \lambda & a_{23} \\
    a_{31} & a_{32} & a_{33} - \lambda
\end{vmatrix*}
\begin{vmatrix*}[l] a_{11} - \lambda & a_{12} & a_{13} \\ a_{21} & a_{22} - \lambda & a_{23} \\ a_{31} & a_{32} & a_{33} - \lambda \end{vmatrix*} \quad \begin{vmatrix*}[c] a_{11} - \lambda & a_{12} & a_{13} \\ a_{21} & a_{22} - \lambda & a_{23} \\ a_{31} & a_{32} & a_{33} - \lambda \end{vmatrix*} \quad \begin{vmatrix*}[r] a_{11} - \lambda & a_{12} & a_{13} \\ a_{21} & a_{22} - \lambda & a_{23} \\ a_{31} & a_{32} & a_{33} - \lambda \end{vmatrix*}

smallmatrix は小さな行列です。*付きはありません。

\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}
\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}

array系 - 表

arraydarrayは数式を表の形に並べます。揃え位置lcrと垂直線位置|:の重複順列を設定できます。

\begin{array}{c|cc}
    a & b & c \\
    x & x + y & x + y + z
\end{array} \\
\begin{array}{l|cc:r}
    \text{} & \text{中央} & \text{中央} & \text{} \\ \hline
    x & x + y & x + y + z & x + y + z + w \\ \hdashline
    x^2 & x^2 + y^2 & x^2 + y^2 + z^2 & x^2 + y^2 + z^2 + w^2
\end{array}
\begin{array}{c|cc} a & b & c \\ x & x + y & x + y + z \end{array} \\ \begin{array}{l|cc:r} \text{左} & \text{中央} & \text{中央} & \text{右} \\ \hline x & x + y & x + y + z & x + y + z + w \\ \hdashline x^2 & x^2 + y^2 & x^2 + y^2 + z^2 & x^2 + y^2 + z^2 + w^2 \end{array}

subarrayは小さな表です。揃え位置の{l}または{c}一つだけを指定します。&は使えません。

\sum_{
    \begin{subarray}{l}
        i \in \Lambda \\
        0 < j < m
    \end{subarray}
}
\sum_{ \begin{subarray}{l} i \in \Lambda \\ 0 < j < m \end{subarray} }

cases系 - 場合分け

casesdcasesrcasesrdcasesは場合分けです。各行の&で区切られた最初の二項は左揃え、それ以降は中央揃えです。
casesdcasesは左側に波括弧を付けます。casesはテキストスタイル、dcasesはディスプレイスタイルです。

\begin{cases}
    na & (r = 1) \\
    \frac{a(1 - r^n)}{1 - r} & (r \ne 1)
\end{cases} \quad
\begin{dcases}
    na & (r = 1) \\
    \frac{a(1 - r^n)}{1 - r} & (r \ne 1)
\end{dcases}
\begin{cases} na & (r = 1) \\ \frac{a(1 - r^n)}{1 - r} & (r \ne 1) \end{cases} \quad \begin{dcases} na & (r = 1) \\ \frac{a(1 - r^n)}{1 - r} & (r \ne 1) \end{dcases}

rcasesrdcasesは右側に波括弧を付けます。rcasesはテキストスタイル、rdcasesはディスプレイスタイルです。

\begin{rcases}
    na & (r = 1) \\
    \frac{a(1 - r^n)}{1 - r} & (r \ne 1)
\end{rcases} \quad
\begin{drcases}
    na & (r = 1) \\
    \frac{a(1 - r^n)}{1 - r} & (r \ne 1)
\end{drcases}
\begin{rcases} na & (r = 1) \\ \frac{a(1 - r^n)}{1 - r} & (r \ne 1) \end{rcases} \quad \begin{drcases} na & (r = 1) \\ \frac{a(1 - r^n)}{1 - r} & (r \ne 1) \end{drcases}

split - 行分割

split は式を複数の行に分割します。
式を揃える位置として、各行に&を一つだけ置けます。インラインモードでは使えません。

\begin{split}
    a &= b + c \\
    &= d + e
\end{split}
\begin{split} a &= b + c \\ &= d + e \end{split}

equation系 - 番号付き方程式

equationは方程式に番号を振ります。インラインモードでは使えません。

\begin{equation}
    a^n + b^n = c^n
\end{equation}
\begin{equation} a^n + b^n = c^n \end{equation}

これ単体では改行できず、&も使えません。改行にはsplitが必要です。

\begin{equation}
    \begin{split}
        a &= b + c \\
        &= d + e
    \end{split}
\end{equation}
\begin{equation} \begin{split} a &= b + c \\ &= d + e \end{split} \end{equation}

equation*は番号を振りません。

\begin{equation*}
    a^n + b^n = c^n
\end{equation*}
\begin{equation*} a^n + b^n = c^n \end{equation*}

align系 - 行揃え

alignalign*alignedは複数の行を各行の奇数番目の&で揃え、偶数番目の&でスペースを入れて区切ります。

  • alignは各行に番号を振ります。インラインモードでは使えません。
  • align*は番号を振りません。インラインモードでは使えません。
  • alignedは番号を振りません。インラインモードでも使えます。
\begin{align}
    a &= x + 2 & b &= 2x & c &= x^2 \\
    a &= y & b &= y - 1 & g &= \frac{y}{3}
\end{align}
\begin{align} a &= x + 2 & b &= 2x & c &= x^2 \\ a &= y & b &= y - 1 & g &= \frac{y}{3} \end{align}
\begin{align*}
    a &= x + 2 & b &= 2x & c &= x^2 \\
    a &= y & b &= y - 1 & g &= \frac{y}{3}
\end{align*}
\begin{align*} a &= x + 2 & b &= 2x & c &= x^2 \\ a &= y & b &= y - 1 & g &= \frac{y}{3} \end{align*}
\begin{aligned}
    a &= x + 2 & b &= 2x & c &= x^2 \\
    a &= y & b &= y - 1 & g &= \frac{y}{3}
\end{aligned}
\begin{aligned} a &= x + 2 & b &= 2x & c &= x^2 \\ a &= y & b &= y - 1 & g &= \frac{y}{3} \end{aligned}

番号を振りたくない個別の行には\notagを指定します。

\begin{align}
    a = b \\
    c = d \notag \\
    e = f
\end{align}
\begin{align} a = b \\ c = d \notag \\ e = f \end{align}

alignat系 - 余白のない行揃え

alignatalignat*alignedat{&の数/2}を引数に取り、複数の行を各行の奇数番目の&で揃えます。位置を揃えるのに必要な最低限のスペースを偶数番目の&に挿入します。

  • alignatは各行に番号を振ります。インラインモードでは使えません。
  • alignat*は番号を振りません。インラインモードでは使えません。
  • alignedatは番号を振りません。インラインモードでも使えます。
\begin{alignat}{2}
    10&x +& 3&y = 2 \\
    3&x +& 13&y = 4
\end{alignat}
\begin{alignat}{2} 10&x +& 3&y = 2 \\ 3&x +& 13&y = 4 \end{alignat}
\begin{alignat*}{2}
    10&x +& 3&y = 2 \\
    3&x +& 13&y = 4
\end{alignat*}
\begin{alignat*}{2} 10&x +& 3&y = 2 \\ 3&x +& 13&y = 4 \end{alignat*}
\begin{alignedat}{2}
    10&x +& 3&y = 2 \\
    3&x +& 13&y = 4
\end{alignedat}
\begin{alignedat}{2} 10&x +& 3&y = 2 \\ 3&x +& 13&y = 4 \end{alignedat}

&の存在によって記号周りの空白が想定通りに入らない場合、空の{}を挟んで調整するテクニックがあります。

\begin{alignat}{2}
    10&x +{}& 3&y = 2 \\
    3&x +{}& 13&y = 4
\end{alignat}
\begin{alignat}{2} 10&x +{}& 3&y = 2 \\ 3&x +{}& 13&y = 4 \end{alignat}

gather系 - 中央揃え

gathergather*gatheredは複数の行を中央揃えします。&は空白を挿入する位置です。

  • gatherは各行に番号を振ります。インラインモードでは使えません。
  • gather*は番号を振りません。インラインモードでは使えません。
  • gatheredは式中で使えます。インラインモードでも使えます。
\begin{gather}
    a & b & c & d & e \\
    a = x + 2 & b = 2x & c = x^2 & d = 2x^2 + 2 \\
    a = y & b = y - 1 & g = \frac{y}{3}
\end{gather}
\begin{gather} a & b & c & d & e \\ a = x + 2 & b = 2x & c = x^2 & d = 2x^2 + 2 \\ a = y & b = y - 1 & g = \frac{y}{3} \end{gather}
\begin{gather*}
    a & b & c & d & e \\
    a = x + 2 & b = 2x & c = x^2 & d = 2x^2 + 2 \\
    a = y & b = y - 1 & g = \frac{y}{3}
\end{gather*}
\begin{gather*} a & b & c & d & e \\ a = x + 2 & b = 2x & c = x^2 & d = 2x^2 + 2 \\ a = y & b = y - 1 & g = \frac{y}{3} \end{gather*}
\begin{gathered}
    a & b & c & d & e \\
    a = x + 2 & b = 2x & c = x^2 & d = 2x^2 + 2 \\
    a = y & b = y - 1 & g = \frac{y}{3}
\end{gathered}
\begin{gathered} a & b & c & d & e \\ a = x + 2 & b = 2x & c = x^2 & d = 2x^2 + 2 \\ a = y & b = y - 1 & g = \frac{y}{3} \end{gathered}

CD - 可換図式

CDは可換図式です。
@<<<@>>>は左右の矢印で、@<上ラベル<下ラベル<@>上ラベル>下ラベル>とすると矢印の上下にラベルを付けます。
@AAA@VVVは上下の矢印で、@A左ラベルA右ラベルA@V左ラベルV右ラベルVとすると矢印の左右にラベルを付けます。
斜めの矢印はありません。
@|は縦長イコール、@=は横長イコールで、@.は何も表示しません。

\begin{CD}
    A @<<< B @>>> C \\
    @AAA @| @A\text{left}A\text{right}A \\
    C @= D @. E \\
    @VVV @. @VV\text{right}V \\
    F @<\text{top}<\text{bottom}< G @>>\text{bottom}> H
\end{CD} \\[1em]
\begin{CD}
    A @>f>> B @>g>> C @>h>> D @>j>> E \\
    @VVlV @VVmV @VVnV @VVpV @VVqV \\
    A' @>>r> B' @>>s> C' @>>t> D' @>>u> E'
\end{CD}
\begin{CD} A @<<< B @>>> C \\ @AAA @| @A\text{left}A\text{right}A \\ C @= D @. E \\ @VVV @. @VV\text{right}V \\ F @<\text{top}<\text{bottom}< G @>>\text{bottom}> H \end{CD} \\[1em] \begin{CD} A @>f>> B @>g>> C @>h>> D @>j>> E \\ @VVlV @VVmV @VVnV @VVpV @VVqV \\ A' @>>r> B' @>>s> C' @>>t> D' @>>u> E' \end{CD}

関連記事

https://zenn.dev/artakase/articles/698b37287fc2b5

https://zenn.dev/artakase/articles/3d32a132fed178


ご清読ありがとうございました。

Discussion