Writing math in Markdown and LaTeX

A working reference for the formulas health data work needs: where the dollar signs go in each tool, the commands you will use every day, how to number equations, and the small mistakes that break a document. Every example shows its source next to the result.

Where math goes

All of these tools read the same LaTeX commands. What changes is how you mark where a formula starts and ends: inline math sits inside a sentence, display math gets its own centred line.

Where you are writingInlineDisplayNote
LaTeX document\( … \) or $ … $\[ … \] or an equation environment$$ works but is discouraged in LaTeX; use \[ … \].
R Markdown and Quarto$ … $$$ … $$Pandoc rules: no space just inside the dollar signs.
Jupyter notebooks$ … $$$ … $$Rendered by MathJax in Markdown cells.
GitHub Markdown$ … $ or $`… `$$$ … $$ or a ```math blockThe backtick form protects underscores and asterisks.

In Markdown

The admission rate was $p = 0.194$ across $n = 30{,}000$ visits.

$$
\hat{p} = \frac{5810}{30000}
$$

In a LaTeX document

The admission rate was \(p = 0.194\) across \(n = 30{,}000\) visits.

\[
\hat{p} = \frac{5810}{30000}
\]

Writing 30{,}000 inside math stops the comma from adding a space after it.

Everyday syntax

The commands that cover most formulas in a methods section. Copy the source on the left; the right shows how it renders.

Subscripts and superscripts

x_i^2 + x_{ij} + e^{-\lambda t}
x_i^2 + x_{ij} + e^{-\lambda t}

Use braces when a sub- or superscript has more than one character.

Fractions and roots

\frac{a}{b} \qquad \dfrac{\sum x_i}{n} \qquad \sqrt{n} \qquad \sqrt[3]{x}
\frac{a}{b} \qquad \dfrac{\sum x_i}{n} \qquad \sqrt{n} \qquad \sqrt[3]{x}

\dfrac keeps a fraction full size inside running text.

Greek letters, hats and bars

\alpha,\ \beta,\ \mu,\ \sigma^2,\ \hat{\beta},\ \bar{x},\ \tilde{y},\ \Delta
\alpha,\ \beta,\ \mu,\ \sigma^2,\ \hat{\beta},\ \bar{x},\ \tilde{y},\ \Delta

Capital Greek letters use a capital first letter: \Delta, \Sigma, \Omega.

Sums, products and integrals

\sum_{i=1}^{n} x_i \qquad \prod_{j=1}^{k} p_j \qquad \int_{0}^{\infty} f(t)\,dt
\sum_{i=1}^{n} x_i \qquad \prod_{j=1}^{k} p_j \qquad \int_{0}^{\infty} f(t)\,dt

\, adds a thin space before dt.

Brackets that grow

\left( \frac{p}{1-p} \right) \qquad \left[ \sum_i w_i \right] \qquad \left\{ x : x > 0 \right\}
\left( \frac{p}{1-p} \right) \qquad \left[ \sum_i w_i \right] \qquad \left\{ x : x > 0 \right\}

Every \left needs a matching \right.

Functions and operators

\log(x),\ \exp(\eta),\ \operatorname{logit}(p),\ \operatorname{Var}(Y),\ \max_i x_i
\log(x),\ \exp(\eta),\ \operatorname{logit}(p),\ \operatorname{Var}(Y),\ \max_i x_i

Upright names with correct spacing. Avoid typing log or logit as plain letters.

Probability and expectation

P(Y = 1 \mid X = x) \qquad \mathbb{E}[Y \mid X] \qquad Y \sim \operatorname{Bernoulli}(p)
P(Y = 1 \mid X = x) \qquad \mathbb{E}[Y \mid X] \qquad Y \sim \operatorname{Bernoulli}(p)

Use \mid for 'given', not a typed vertical bar.

Relations

a \le b,\ a \ge b,\ a \ne b,\ a \approx b,\ a \propto b,\ A \subseteq B,\ x \in \mathbb{R}
a \le b,\ a \ge b,\ a \ne b,\ a \approx b,\ a \propto b,\ A \subseteq B,\ x \in \mathbb{R}

Vectors and matrices

\boldsymbol{\beta} = \begin{pmatrix} \beta_0 \\ \beta_1 \end{pmatrix}, \quad \mathbf{X}^\top \mathbf{X}
\boldsymbol{\beta} = \begin{pmatrix} \beta_0 \\ \beta_1 \end{pmatrix}, \quad \mathbf{X}^\top \mathbf{X}

pmatrix gives round brackets, bmatrix square ones; \\ starts a new row and & separates columns.

Words inside math

\text{LOS}_i = \beta_0 + \beta_1\,\text{ED time}_i + \varepsilon_i
\text{LOS}_i = \beta_0 + \beta_1\,\text{ED time}_i + \varepsilon_i

\text keeps normal spacing between words.

Piecewise definitions

Y_i = \begin{cases} 1 & \text{if admitted} \\ 0 & \text{otherwise} \end{cases}
Y_i = \begin{cases} 1 & \text{if admitted} \\ 0 & \text{otherwise} \end{cases}

Multi-line and numbered equations

Use & to mark the point each line lines up on, usually just before the equals sign, and \\ to end a line.

Aligned lines inside Markdown

$$
\begin{aligned}
\hat{p}_1 &= \frac{1}{n} \sum_{i=1}^{n} \hat{P}(Y_i = 1 \mid A_i = 1, \mathbf{X}_i) \\
\hat{p}_0 &= \frac{1}{n} \sum_{i=1}^{n} \hat{P}(Y_i = 1 \mid A_i = 0, \mathbf{X}_i)
\end{aligned}
$$
\begin{aligned} \hat{p}_1 &= \frac{1}{n} \sum_{i=1}^{n} \hat{P}(Y_i = 1 \mid A_i = 1, \mathbf{X}_i) \\ \hat{p}_0 &= \frac{1}{n} \sum_{i=1}^{n} \hat{P}(Y_i = 1 \mid A_i = 0, \mathbf{X}_i) \end{aligned}

In Markdown, R Markdown, Quarto, Jupyter and GitHub, put aligned inside $$ … $$.

Numbering and cross-references

LaTeX document: align numbers each line; \nonumber skips one

\begin{align}
\operatorname{logit} P(Y_{ij} = 1) &= \beta_0 + \beta_1 \text{Asthma}_{ij} + u_j \label{eq:glmm} \\
u_j &\sim N(0, \tau^2) \nonumber
\end{align}

Equation~\eqref{eq:glmm} adds a hospital random intercept.

R Markdown with bookdown

\begin{equation}
\text{RR} = \frac{p_1}{p_0}
(\#eq:rr)
\end{equation}

The risk ratio in Equation \@ref(eq:rr) ...

Quarto

$$
\text{RR} = \frac{p_1}{p_0}
$$ {#eq-rr}

The risk ratio in @eq-rr ...

Plain R Markdown (html_document) and plain Markdown do not number equations. You need bookdown output formats such as bookdown::html_document2, or Quarto.

Statistics examples from Project 001

The models behind the ED-to-Inpatient Pathways memos, written out. They combine most of the syntax above.

A03: logistic regression for admission

\operatorname{logit}\, P(Y_i = 1) = \beta_0 + \beta_1\, \text{ED time}_i + \sum_{k=2}^{5} \gamma_k\, \mathbf{1}[\text{CTAS}_i = k]
\operatorname{logit}\, P(Y_i = 1) = \beta_0 + \beta_1\, \text{ED time}_i + \sum_{k=2}^{5} \gamma_k\, \mathbf{1}[\text{CTAS}_i = k]

Admission on ED time before the disposition decision, with CTAS 1 as the reference level.

A05: linear regression for length of stay

\text{LOS}_i = \beta_0 + \beta_1\, \text{ED time}_i + \sum_{k=2}^{5} \gamma_k\, \mathbf{1}[\text{CTAS}_i = k] + \varepsilon_i, \quad \varepsilon_i \sim N(0, \sigma^2)
\text{LOS}_i = \beta_0 + \beta_1\, \text{ED time}_i + \sum_{k=2}^{5} \gamma_k\, \mathbf{1}[\text{CTAS}_i = k] + \varepsilon_i, \quad \varepsilon_i \sim N(0, \sigma^2)

Inpatient days in the linked cohort.

A07: propensity score

e(\mathbf{x}_i) = P(A_i = 1 \mid \mathbf{X}_i = \mathbf{x}_i)
e(\mathbf{x}_i) = P(A_i = 1 \mid \mathbf{X}_i = \mathbf{x}_i)

The probability of an asthma visit (A = 1) given baseline characteristics, used for 1:1 matching within hospital.

A08: risk difference and risk ratio

\widehat{\text{RD}} = \hat{p}_1 - \hat{p}_0, \qquad \widehat{\text{RR}} = \frac{\hat{p}_1}{\hat{p}_0}
\widehat{\text{RD}} = \hat{p}_1 - \hat{p}_0, \qquad \widehat{\text{RR}} = \frac{\hat{p}_1}{\hat{p}_0}

Standardized admission risks for the asthma and control scenarios.

A09: logistic GLMM with a hospital random intercept

\operatorname{logit}\, P(Y_{ij} = 1) = \beta_0 + \beta_1\, \text{Asthma}_{ij} + \boldsymbol{\gamma}^\top \text{Region}_j + u_j, \qquad u_j \sim N(0, \tau^2)
\operatorname{logit}\, P(Y_{ij} = 1) = \beta_0 + \beta_1\, \text{Asthma}_{ij} + \boldsymbol{\gamma}^\top \text{Region}_j + u_j, \qquad u_j \sim N(0, \tau^2)

Patient i in hospital j; u_j captures how patients at the same hospital resemble each other.

Common mistakes

Most broken formulas come from one of these.

  • A space just inside the dollar signs

    In R Markdown, Quarto and other Pandoc-based tools, $ x $ is not treated as math. The opening $ must be followed directly by a character, and the closing $ must follow one directly.

    Write $x$, not $ x $
  • Dollar signs that mean money

    A sentence such as costs rose from $5 to $10 can turn into broken math. Escape the sign as \$ in Markdown.

    Costs rose from \$5 to \$10.
  • Percent signs in LaTeX

    In a LaTeX document % starts a comment and silently deletes the rest of the line. Write \%.

    Admission rate was 19.4\%.
  • Blank lines inside display math

    An empty line inside $$ … $$ or an equation environment ends the paragraph and breaks the formula. Keep display math free of blank lines.

  • Markdown eating underscores and asterisks

    Some renderers, GitHub among them, can read _ or * inside $…$ as emphasis. On GitHub, wrap inline math as $`…`$ or use a ```math block.

    $`x_{i} * y_{i}`$
  • align versus aligned

    align is a stand-alone environment for LaTeX documents and must not sit inside $$. Inside $$ … $$ in Markdown, use aligned.

  • Functions typed as letters

    log(x) renders as the italic product l·o·g. Use \log, \exp and \operatorname{logit}.

  • Commands your renderer does not know

    KaTeX, MathJax and full LaTeX support different command sets. If a formula works in your PDF but not on a web page, check the renderer's list of supported functions before rewriting the maths.

Try it

Type LaTeX math without dollar signs and see it render as you go. Rendering uses KaTeX, the same engine as this page.

Result