LaTeX matrices, cases and aligned equations in Markdown

Writing in Markdown6 min read

Write multi-row LaTeX structures inside a $$...$$ display block, separate columns with &, and end each row with \\. Pick pmatrix, bmatrix, vmatrix, or cases for the surrounding delimiters you want, use aligned when splitting a derivation across several lines, and switch to smallmatrix when a tiny matrix has to sit inside a sentence. Syntax errors in Markdown notes rarely come from TeX itself: they happen when a plain CommonMark parser strips one backslash from \\ before KaTeX sees the formula, or when a bare \begin{align} sits outside math delimiters.

Matrix environments and bracket styles

KaTeX supports the six standard amsmath matrix environments out of the box. Every one of them centers its columns by default and sizes its outer delimiters to the height of the grid, so you do not need \left and \right around a basic matrix.

$$
A = \begin{pmatrix}
1 & 2 & 3 \\
0 & -1 & 4
\end{pmatrix},
\qquad
\det(A^\top A) = \begin{vmatrix}
1 & 2 \\
2 & 21
\end{vmatrix}
$$
Environment Delimiters drawn Typical use
matrix None Bare grids or custom \left and \right wrappers
pmatrix ( ) Vectors, Jacobians, transformation matrices
bmatrix [ ] Linear algebra notes, state-space matrices
Bmatrix { } Sets of tuples or braced arrays
vmatrix Single vertical bars Determinants
Vmatrix Double vertical bars Matrix norms

When a matrix mixes positive and negative entries, centered columns look ragged because minus signs push digits right. KaTeX supports the mathtools starred variants (pmatrix*, bmatrix*, and the rest), which take [l], [c], or [r] to align columns:

$$
R = \begin{bmatrix*}[r]
1 & -2 & 14 \\
0 & 3 & -5 \\
-10 & 0 & 1
\end{bmatrix*}
$$

For $n \times n$ matrices, combine \cdots, \vdots, and \ddots, listed with other dot commands in LaTeX math symbols. Inside a sentence, full-size matrices stretch line spacing, so switch to smallmatrix and add compact delimiters of your own:

The rotation $\bigl(\begin{smallmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{smallmatrix}\bigr)$ preserves length.

Augmented and partitioned matrices with array

Standard matrix environments cannot draw internal column or row lines. For Gaussian elimination or block-partitioned matrices, wrap array in \left[ and \right]:

$$
\left[\begin{array}{cc|c}
2 & -1 & 3 \\
1 & 4 & 10
\end{array}\right]
\qquad
\left[\begin{array}{cc:c}
I_2 & 0 & b_1 \\
\hdashline
0 & 1 & b_2
\end{array}\right]
$$

Each letter (l, c, r) sets one column's alignment. Between letters, | draws a solid vertical rule and : draws a dashed one; before a row, \hline draws a solid horizontal line and \hdashline draws a dashed line. All four render in KaTeX 0.16.47.

Watch one Markdown edge case inside GFM pipe tables: an unshielded | splits the table cell before TeX runs. MiaoYan masks pipes inside math, while on plain GFM parsers you should keep array blocks outside Markdown tables or write \vert for single bars.

Piecewise functions with cases, dcases, and rcases

Piecewise definitions use cases, which draws a left curly brace and sets two left-aligned columns: the expression on the left of &, and the condition on the right.

$$
\operatorname{ReLU}(x) = \begin{cases}
x & \text{if } x > 0, \\
0 & \text{otherwise.}
\end{cases}
$$

Wrap words inside the second column in \text{...}, including the trailing space in \text{if }. Without \text, TeX reads otherwise as the product of nine italic variables with zero word spacing.

Standard cases sets every row in \textstyle, which keeps polynomials compact yet squeezes fractions and integrals into tiny glyphs. Switch to dcases when a branch contains a fraction, applying \displaystyle to each row automatically:

$$
f(x) = \begin{dcases}
\frac{\sin x}{x} & \text{if } x \neq 0, \\
1 & \text{if } x = 0.
\end{dcases}
$$

KaTeX also supports rcases, placing the brace on the right so simultaneous equations lead into one conclusion:

$$
\begin{rcases}
x = r \cos\theta \\
y = r \sin\theta
\end{rcases}
\implies x^2 + y^2 = r^2
$$

Multi-line equations: aligned, gathered, and align

When an equation is too wide for a note or a derivation takes three steps, split it inside $$...$$ with aligned. Place the & right before the relation sign (&=, &\le, &\approx), not after it: TeX inserts binary-relation spacing to the right of = only when & precedes the operator.

$$
\begin{aligned}
\mathcal{L}(\theta) &= \frac{1}{n} \sum_{i=1}^{n} (y_i - X_i \theta)^2 \\[4pt]
&= \frac{1}{n} (y - X\theta)^\top (y - X\theta)
\end{aligned}
$$

An optional length such as \\[4pt] adds vertical room when fractions or superscripts nearly touch. When several equations should stack centered without aligning on an operator, use gathered instead.

Two differences explain why aligned inside $$...$$ beats \begin{align}...\end{align} in Markdown:

  1. Delimiter detection. KaTeX's standalone auto-render recognizes bare \begin{align} in HTML, whereas Markdown tools expect explicit math delimiters. MiaoYan scans four pairs ($$...$$, $...$, \[...\], \(...\)), and GitHub's MathJax also requires $$...$$ around multi-line blocks; see LaTeX math in Markdown. Wrapping aligned inside $$...$$ works in MiaoYan, GitHub, Obsidian, and Pandoc.
  2. Row numbering and \tag{}. Inside $$...$$, KaTeX's align increments its katexEqnNo counter to print (1), (2) on each row (align* suppresses them, \notag skips one row). aligned is a sub-environment: it never auto-numbers, sits beside outer terms like Z = \begin{aligned}...\end{aligned}, and accepts at most one \tag{1} for the whole $$ block. Two \tag commands inside aligned throw KaTeX parse error: Multiple \tag, so switch to align* inside $$...$$ when separate lines need \tag{1a} and \tag{1b}.

Why double backslashes break in plain CommonMark

When a three-row matrix collapses onto one horizontal line, the cause sits between the Markdown parser and TeX. In CommonMark, a backslash before ASCII punctuation is an escape, turning \\ into a single \ in HTML. To KaTeX or MathJax, \ before a space or newline is a control space rather than a row break. On pipelines that parse Markdown before finding math, writers had to type \\\\ at the end of every row or put $$ on separate lines to hit a block-math rule.

MiaoYan avoids \\\\ with its protectMarkdownMath pre-pass before cmark-gfm runs. After masking code blocks and inline code, it locates balanced math delimiters and encodes formula characters so \\, \{, \}, _, and | survive Markdown untouched. It also marks internal newlines so matrices and cases render identically under both Line Breaks modes (⌘,, Editor tab: Every Newline or GitHub Style).

Since V4.12.0, MiaoYan bundles KaTeX 0.16.47 offline on Mac, iPhone, and iPad, and display matrices wider than the column scroll sideways in the preview. Print and PDF pages cannot scroll, so split wide derivations with aligned before exporting. You can check any matrix in the browser with the Markdown viewer, or convert a note in Markdown to Word when a handout needs .docx output with editable Word equations. MiaoYan is on the App Store for Mac, iPhone, and iPad, with the Mac source on GitHub.

FAQ

Why did my matrix flatten into a single row?

Either rows ended with \ instead of \\, or your Markdown parser turned \\ into \ before TeX ran. MiaoYan shields \\ automatically; in plain CommonMark pipelines, put $$ on their own lines or test \\\\.

Why does \begin{align} show as plain text without $$?

Markdown parsers only shield regions inside math delimiters. Wrap \begin{aligned}...\end{aligned} (or align*) with $$ on the lines above and below.

How do I right-align negative numbers inside a matrix?

Add [r] to a starred matrix such as \begin{bmatrix*}[r] ... \end{bmatrix*}, or use \left[\begin{array}{rrr} ... \end{array}\right].

Why does KaTeX report Multiple \tag inside aligned?

aligned forms a single sub-formula, so only one \tag{...} can label its $$...$$ block. Switch to align* inside $$...$$ when individual rows each need a tag.