17  LaTeX for Scientific Documents

NoteLearning Objectives

After completing this chapter, you will be able to:

  • Explain what LaTeX is and why it’s valuable for scientific writing
  • Use LaTeX syntax for mathematical notation in Quarto documents
  • Write inline and display equations
  • Format common mathematical operators, Greek letters, and symbols
  • Create matrices and complex mathematical expressions
  • Understand the relationship between LaTeX, Markdown, and Quarto

17.1 What is LaTeX?

LaTeX (pronounced “Lah-tech” or “Lay-tech”—to rhyme with “Bertolt Brecht”) is a document preparation system for high-quality typesetting. Created by Leslie Lamport in 1984, it builds on Donald Knuth’s TeX typesetting system and has become the standard for scientific and technical documentation (Lamport, 1994). Quarto (Chapter 16) uses LaTeX under the hood whenever it renders to PDF (Posit Software, PBC, 2024).

Why LaTeX Matters

LaTeX excels in several key areas:

Precision in Mathematics
LaTeX produces beautifully typeset mathematical equations that are impossible to achieve in standard word processors.
Consistent Formatting
Document structure is controlled by logical commands, ensuring consistent formatting throughout.
Large Document Management
Cross-references, bibliographies, tables of contents, and indexes are handled automatically.
Multi-lingual Support
LaTeX handles complex scripts and languages with ease.
Professional Output
LaTeX is the standard for journal articles, theses, and technical books in mathematics, physics, computer science, and engineering.
Tip

You don’t need to learn full LaTeX to benefit from it! Quarto and R Markdown let you use LaTeX syntax for mathematical notation while handling document layout automatically (see Section 16.6).

LaTeX Resources

17.2 LaTeX in Quarto Documents

One of LaTeX’s greatest strengths is its seamless integration into Quarto and R Markdown documents. You can include mathematical expressions directly in your text without needing to learn the full LaTeX document system.

Inline vs. Display Equations

There are two ways to include mathematical expressions:

Inline equations appear within the text flow, surrounded by single dollar signs:

This equation, $y = \frac{1}{2}$, is included inline with the text.

Renders as: This equation, \(y = \frac{1}{2}\), is included inline with the text.

Display equations appear on their own line, centered, using double dollar signs:

This equation is displayed on a separate line:

$$y = \frac{1}{2}$$

Renders as: This equation is displayed on a separate line:

\[y = \frac{1}{2}\]

Note

Use inline equations for simple expressions mentioned in text. Use display equations for important formulas that deserve visual emphasis or are too complex to fit inline.

17.3 Basic Mathematical Notation

Operators and Arithmetic

LaTeX provides commands for mathematical operations:

Table 17.1: Basic arithmetic operators
Expression Code Description
\(a + b\) $a + b$ Addition
\(a - b\) $a - b$ Subtraction
\(a \times b\) $a \times b$ Multiplication
\(a \div b\) $a \div b$ Division
\(\frac{a}{b}\) $\frac{a}{b}$ Fraction
\(a^n\) $a^n$ Superscript (power)
\(a_n\) $a_n$ Subscript
\(a^{n+1}\) $a^{n+1}$ Multi-character superscript
\(a_{i,j}\) $a_{i,j}$ Multi-character subscript

Roots and Powers

Table 17.2: Roots and powers
Expression Code Description
\(\sqrt{x}\) $\sqrt{x}$ Square root
\(\sqrt[n]{x}\) $\sqrt[n]{x}$ nth root
\(x^2\) $x^2$ Square
\(e^x\) $e^x$ Exponential

Example with multiple operations:

$$ \large a^x, \sqrt[n]{x}, \vec{\jmath}, \tilde{\imath} $$

\[ \large a^x, \sqrt[n]{x}, \vec{\jmath}, \tilde{\imath} \]

17.4 Greek Letters

Greek letters are essential in scientific notation. LaTeX provides commands for all Greek letters (Table 17.3 and Table 17.4):

Lowercase Greek Letters

Table 17.3: Lowercase Greek letters
Letter Code Letter Code Letter Code
\(\alpha\) \alpha \(\iota\) \iota \(\rho\) \rho
\(\beta\) \beta \(\kappa\) \kappa \(\sigma\) \sigma
\(\gamma\) \gamma \(\lambda\) \lambda \(\tau\) \tau
\(\delta\) \delta \(\mu\) \mu \(\upsilon\) \upsilon
\(\epsilon\) \epsilon \(\nu\) \nu \(\phi\) \phi
\(\zeta\) \zeta \(\xi\) \xi \(\chi\) \chi
\(\eta\) \eta \(\pi\) \pi \(\psi\) \psi
\(\theta\) \theta \(\omega\) \omega

Example:

$$ \large \alpha, \beta, \gamma $$

\[ \large \alpha, \beta, \gamma \]

Uppercase Greek Letters

Table 17.4: Uppercase Greek letters
Letter Code Letter Code Letter Code
\(\Gamma\) \Gamma \(\Lambda\) \Lambda \(\Phi\) \Phi
\(\Delta\) \Delta \(\Xi\) \Xi \(\Psi\) \Psi
\(\Theta\) \Theta \(\Pi\) \Pi \(\Omega\) \Omega
\(\Sigma\) \Sigma \(\Upsilon\) \Upsilon
Tip

Many uppercase Greek letters look identical to Latin letters (A, B, E, H, I, K, M, N, O, P, T, X, Z) and don’t have separate LaTeX commands—just use the Latin letter. Several lowercase letters have a second form (\varepsilon \(\varepsilon\), \varphi \(\varphi\), \vartheta \(\vartheta\)), and a bold Greek letter needs \boldsymbol{\beta} rather than \mathbf. The names and pronunciations of all 24 letters are listed in Table G.3.

NoteGreek letters in R plot labels

LaTeX works in Quarto text, not inside an R plot. For an axis label or title that needs a Greek letter, use R’s expression() (or the latex2exp package, which translates LaTeX strings):

ggplot(df, aes(x, y)) +
  geom_point() +
  labs(x = expression(mu), y = expression(sigma^2),
       title = expression(paste("Mean = ", mu, ", SD = ", sigma)))

In matplotlib the label string itself can hold LaTeX: plt.xlabel(r"$\mu$").

17.5 Comparison and Relation Operators

Table 17.5: Comparison and relation operators
Expression Code Description
\(=\) $=$ Equal to
\(\neq\) $\neq$ Not equal to
\(<\) $<$ Less than
\(>\) $>$ Greater than
\(\leq\) $\leq$ Less than or equal
\(\geq\) $\geq$ Greater than or equal
\(\approx\) $\approx$ Approximately equal
\(\sim\) $\sim$ Similar to / distributed as
\(\nsim\) $\nsim$ Not similar to
\(\equiv\) $\equiv$ Equivalent / identical
\(\propto\) $\propto$ Proportional to
\(\in\) $\in$ Element of
\(\notin\) $\notin$ Not element of
\(\subset\) $\subset$ Subset of
\(\subseteq\) $\subseteq$ Subset or equal

Example:

$$ \large \approx, \neq, \nsim $$

\[ \large \approx, \neq, \nsim \]

17.6 Special Symbols

LaTeX provides a wide range of special mathematical symbols:

Table 17.6: Special mathematical symbols
Expression Code Description
\(\partial\) $\partial$ Partial derivative
\(\infty\) $\infty$ Infinity
\(\nabla\) $\nabla$ Del / gradient
\(\mathbb{R}\) $\mathbb{R}$ Real numbers
\(\mathbb{N}\) $\mathbb{N}$ Natural numbers
\(\mathbb{Z}\) $\mathbb{Z}$ Integers
\(\forall\) $\forall$ For all
\(\exists\) $\exists$ There exists
\(\emptyset\) $\emptyset$ Empty set
\(\cup\) $\cup$ Union
\(\cap\) $\cap$ Intersection
\(\to\) $\to$ Arrow (also \rightarrow, \leftarrow)
\(\Rightarrow\) $\Rightarrow$ Implies (\Leftrightarrow for “if and only if”)
\(\pm\) $\pm$ Plus or minus
\(\cdot\) $\cdot$ Centered dot
\(\ldots\) $\ldots$ Baseline dots (\(x_1, \ldots, x_n\))
\(\cdots\) $\cdots$ Horizontal dots (\(x_1 + \cdots + x_n\))
\(\vdots\) $\vdots$ Vertical dots
\(\ddots\) $\ddots$ Diagonal dots

Logic symbols, arrows, number sets, and many more are tabulated in Appendix G.

Example:

$$ \large \partial, \mathbb{R}, \infty $$

\[ \large \partial, \mathbb{R}, \infty \]

17.7 Sums, Products, and Integrals

Summation and Products

Table 17.7: Summation and product notation
Expression Code Description
\(\sum_{i=1}^{n} x_i\) $\sum_{i=1}^{n} x_i$ Summation
\(\prod_{i=1}^{n} x_i\) $\prod_{i=1}^{n} x_i$ Product
\(\bigcup_{i=1}^{n} A_i\) $\bigcup_{i=1}^{n} A_i$ Union
\(\bigcap_{i=1}^{n} A_i\) $\bigcap_{i=1}^{n} A_i$ Intersection

Integrals

Table 17.8: Integral notation
Expression Code Description
\(\int f(x) \, dx\) $\int f(x) \, dx$ Integral
\(\int_a^b f(x) \, dx\) $\int_a^b f(x) \, dx$ Definite integral
\(\iint f(x,y) \, dx \, dy\) $\iint f(x,y) \, dx \, dy$ Double integral
\(\iiint\) $\iiint$ Triple integral
\(\oint\) $\oint$ Contour integral

Example: Integral Equation

$$\iint xy^2\,dx\,dy = \frac{1}{6}x^2y^3$$

\[\iint xy^2\,dx\,dy = \frac{1}{6}x^2y^3\]

17.8 Statistical and Probability Equations

Scientific writing often requires statistical formulas. Here are common examples:

Binomial Distribution

The probability mass function for binomial sampling:

$$\large f(k) = {n \choose k} p^{k} (1-p)^{n-k}$$

\[\large f(k) = {n \choose k} p^{k} (1-p)^{n-k}\]

The binomial coefficient can equally be written \binom{n}{k}, which reads more clearly in source.

Poisson Distribution

The probability mass function:

$$\large P(Y=r) = \frac{e^{-\mu}\mu^r}{r!}$$

\[\large P(Y=r) = \frac{e^{-\mu}\mu^r}{r!}\]

Tip

Multi-letter names in math mode are typeset as a product of italic variables (Pr becomes \(Pr\), i.e. \(P \times r\)). Use \Pr, \text{Var}, \operatorname{Cov}, or \mathrm{SE} for upright function names: \(\Pr(Y = r)\), \(\text{Var}(X)\).

Normal Distribution

The probability density function:

\[f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2}\]

Sample Mean and Variance

Mean: \(\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i\)

Variance: \(s^2 = \frac{1}{n-1}\sum_{i=1}^{n}(x_i - \bar{x})^2\)

17.9 Matrices

LaTeX provides several environments for typesetting matrices:

Basic Matrix

Use the matrix environment for a matrix without brackets:

$$\begin{matrix}
a & b \\
c & d
\end{matrix}$$

\[\begin{matrix} a & b \\ c & d \end{matrix}\]

Bracketed Matrices

Table 17.9: Matrix environments
Environment Brackets Example
pmatrix Parentheses \(()\) \(\begin{pmatrix} a & b \\ c & d \end{pmatrix}\)
bmatrix Square brackets \([]\) \(\begin{bmatrix} a & b \\ c & d \end{bmatrix}\)
Bmatrix Curly braces \(\{\}\) \(\begin{Bmatrix} a & b \\ c & d \end{Bmatrix}\)
vmatrix Vertical bars (determinant) \(\begin{vmatrix} a & b \\ c & d \end{vmatrix}\)
Vmatrix Double vertical bars \(\begin{Vmatrix} a & b \\ c & d \end{Vmatrix}\)

Example: Tridiagonal Matrix

A more complex matrix with ellipses:

$$\begin{matrix}
  -2 & 1 & 0 & 0 & \cdots & 0  \\
  1 & -2 & 1 & 0 & \cdots & 0  \\
  0 & 1 & -2 & 1 & \cdots & 0  \\
  0 & 0 & 1 & -2 & \ddots & \vdots \\
  \vdots & \vdots & \vdots & \ddots & \ddots & 1  \\
  0 & 0 & 0 & \cdots & 1 & -2
\end{matrix}$$

\[\begin{matrix} -2 & 1 & 0 & 0 & \cdots & 0 \\ 1 & -2 & 1 & 0 & \cdots & 0 \\ 0 & 1 & -2 & 1 & \cdots & 0 \\ 0 & 0 & 1 & -2 & \ddots & \vdots \\ \vdots & \vdots & \vdots & \ddots & \ddots & 1 \\ 0 & 0 & 0 & \cdots & 1 & -2 \end{matrix}\]

Tip

In matrix notation: - Use & to separate columns - Use \\ to separate rows - Use \cdots for horizontal dots, \vdots for vertical, and \ddots for diagonal

17.10 Accents and Decorations

LaTeX provides various accents and decorations for variables:

Table 17.10: Mathematical accents and decorations
Expression Code Description
\(\hat{x}\) $\hat{x}$ Hat (estimate)
\(\bar{x}\) $\bar{x}$ Bar (mean)
\(\tilde{x}\) $\tilde{x}$ Tilde
\(\vec{x}\) $\vec{x}$ Vector arrow
\(\dot{x}\) $\dot{x}$ Single dot (derivative)
\(\ddot{x}\) $\ddot{x}$ Double dot (second derivative)
\(\mathbf{x}\) $\mathbf{x}$ Bold (vector/matrix)
\(\mathit{x}\) $\mathit{x}$ Italic
\(\mathrm{x}\) $\mathrm{x}$ Roman (upright)

17.11 Brackets and Delimiters

For equations with tall expressions, use \left and \right to automatically size delimiters:

$$\left( \frac{a^2}{b^2} \right)$$

\[\left( \frac{a^2}{b^2} \right)\]

Without automatic sizing:

$$( \frac{a^2}{b^2} )$$

\[( \frac{a^2}{b^2} )\]

Table 17.11: Bracket delimiters
Delimiter Code
Parentheses \left( ... \right)
Square brackets \left[ ... \right]
Curly braces \left\{ ... \right\}
Angle brackets \left\langle ... \right\rangle
Vertical bars \left| ... \right|
Double bars \left\| ... \right\|

17.12 Functions and Operators

LaTeX provides proper formatting for common functions:

Table 17.12: Mathematical functions
Function Code Rendered
\(\sin x\) $\sin x$ Sine
\(\cos x\) $\cos x$ Cosine
\(\tan x\) $\tan x$ Tangent
\(\log x\) $\log x$ Logarithm
\(\ln x\) $\ln x$ Natural log
\(\exp x\) $\exp x$ Exponential
\(\lim_{x \to 0}\) $\lim_{x \to 0}$ Limit
\(\max\) $\max$ Maximum
\(\min\) $\min$ Minimum
\(\arg\max\) $\arg\max$ Argument of maximum

17.13 Numbered and Aligned Equations

Quarto numbers a display equation when you attach an {#eq-...} label after the closing $$, and you refer to it with @eq-... exactly as for figures and tables (Section 16.9):

$$
E = mc^2
$$ {#eq-einstein}

@eq-einstein relates mass and energy.

\[ E = mc^2 \tag{17.1}\]

Equation 17.1 relates mass and energy.

A multi-step derivation belongs in an aligned environment inside the $$ pair. Each line ends with \\, and the & marks the point that lines up vertically (usually just before the =):

$$
\begin{aligned}
\bar{x} &= \frac{1}{n}\sum_{i=1}^{n} x_i \\
        &= \frac{x_1 + x_2 + \cdots + x_n}{n}
\end{aligned}
$$ {#eq-mean-aligned}

\[ \begin{aligned} \bar{x} &= \frac{1}{n}\sum_{i=1}^{n} x_i \\ &= \frac{x_1 + x_2 + \cdots + x_n}{n} \end{aligned} \tag{17.2}\]

The whole block receives one number (Equation 17.2). Use cases in the same way for a piecewise definition:

$$
f(x) =
\begin{cases}
  \lambda e^{-\lambda x} & x \geq 0 \\
  0                      & x < 0
\end{cases}
$$

\[ f(x) = \begin{cases} \lambda e^{-\lambda x} & x \geq 0 \\ 0 & x < 0 \end{cases} \]

17.14 Practical Examples

Linear Regression Model

\[Y_i = \beta_0 + \beta_1 X_{1i} + \beta_2 X_{2i} + \epsilon_i\]

where \(\epsilon_i \sim N(0, \sigma^2)\).

Maximum Likelihood

\[\hat{\theta} = \arg\max_\theta \prod_{i=1}^{n} f(x_i | \theta)\]

Bayes’ Theorem

\[P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)}\]

Hardy-Weinberg Equilibrium

For a biallelic locus with allele frequencies \(p\) and \(q = 1-p\):

\[p^2 + 2pq + q^2 = 1\]

17.15 Equations From Bioengineering

The equations above are the statistician’s staples. Here are a few from the kinds of models bioengineers actually write down, each with the LaTeX that produces it, chosen because together they use most of the notation in this chapter.

Enzyme kinetics is the natural starting point. The Michaelis-Menten rate law needs a fraction, a subscript that is a word rather than a single letter (so it is wrapped in \text{} or \mathrm{}), and square brackets for concentration:

$$v = \frac{V_{\max}[S]}{K_{\mathrm{M}} + [S]}$$

\[v = \frac{V_{\max}[S]}{K_{\mathrm{M}} + [S]}\]

The Hill equation generalizes it to cooperative binding and adds a variable exponent, which shows why multi-character superscripts must be grouped in braces ([S]^{n} rather than [S]^n when \(n\) is more than one character, and always when it is an expression):

$$\theta = \frac{[L]^{n}}{K_{d} + [L]^{n}}$$

\[\theta = \frac{[L]^{n}}{K_{d} + [L]^{n}}\]

Population and cell growth are described by differential equations, and the logistic model shows the derivative notation \frac{dN}{dt} together with a parenthesized factor that grows to fit its contents with \left( and \right):

$$\frac{dN}{dt} = rN\left(1 - \frac{N}{K}\right)$$

\[\frac{dN}{dt} = rN\left(1 - \frac{N}{K}\right)\]

Transport problems bring in partial derivatives and the Laplacian. Fick’s second law of diffusion uses \partial and \nabla^2; the second form, written out in one dimension, is the one you would actually discretize in code:

$$\frac{\partial C}{\partial t} = D\,\nabla^{2} C
  \qquad\text{or, in one dimension,}\qquad
  \frac{\partial C}{\partial t} = D\frac{\partial^{2} C}{\partial x^{2}}$$

\[\frac{\partial C}{\partial t} = D\,\nabla^{2} C \qquad\text{or, in one dimension,}\qquad \frac{\partial C}{\partial t} = D\frac{\partial^{2} C}{\partial x^{2}}\]

Finally, the normalization used on the course dataset in Chapter 15 is itself an equation worth writing down, because doing so makes the pseudocount explicit in a way the code never quite does. For gene \(g\) in sample \(i\), with raw count \(c_{gi}\) and library size \(N_i = \sum_g c_{gi}\),

$$\text{CPM}_{gi} = \frac{c_{gi}}{N_i}\times 10^{6},
  \qquad
  y_{gi} = \log_{2}\!\left(\text{CPM}_{gi} + 1\right)$$

\[\text{CPM}_{gi} = \frac{c_{gi}}{N_i}\times 10^{6}, \qquad y_{gi} = \log_{2}\!\left(\text{CPM}_{gi} + 1\right)\]

A methods section that carries this equation, with the code that implements it in the same Quarto document, is the kind of reproducibility the last chapters of this book are about.

TipSee it all in one working document

The Week 7 example manuscript on the course Example Files page uses this chapter end to end — inline and display math, numbered equations referenced with @eq-..., an aligned block, and subscripted statistical notation — inside a Science/Nature-style PDF. Render it and compare each equation with its source.

17.16 Reading Equations You Did Not Write

You will spend more time reading LaTeX than writing it: in the methods sections of papers whose source you download, in the templates of the journal you submit to, and in code comments where an author has written the formula their function implements. Three habits make that reading easy. First, find the structure before the symbols: every \frac{...}{...} is one fraction with a numerator and a denominator, every \sum_{...}^{...} is one sum with a lower and an upper limit, and matching braces tell you where each piece begins and ends, so an expression that looks like a wall of backslashes is usually three or four nested pieces. Second, learn to see a dozen commands on sight, since they account for most of what appears in scientific writing: the Greek letters, \frac, \sqrt, \sum, \int, \partial, \hat, \bar, \left and \right, \text, and the subscript and superscript operators _ and ^. Third, when a symbol is unfamiliar, render it. Paste the expression between dollar signs in a Quarto document and preview it, or use one of the online detexify tools that let you draw a symbol and tell you its command. Appendix G is organized for exactly this kind of look-up.

NoteLaTeX beyond Quarto

Quarto lets you use LaTeX’s mathematics without writing a LaTeX document. You will meet the full system eventually: most journals in engineering and the quantitative sciences supply a LaTeX template, theses at many universities are written in it, and Overleaf (https://www.overleaf.com/learn) is a browser-based editor that many collaborations use so that nobody has to install anything. The mathematics you learn here transfers unchanged; what a full LaTeX document adds is the document-level structure of \documentclass, \section, and \begin{figure} that Quarto has been writing for you behind the scenes whenever you render to PDF (Chapter 16).

17.17 Best Practices

When to Use LaTeX

  • Use inline math ($...$) for simple expressions in text
  • Use display math ($$...$$) for important formulas or complex expressions
  • Use display math when equations would break text flow

Formatting Tips

  1. Spacing: LaTeX ignores spaces in math mode. Use \, for a thin space (as before \(dx\) in an integral), \: for medium, \; for thick, \quad and \qquad for large gaps, and \! to pull symbols closer (full table in Section G.15)
  2. Text in equations: Use \text{...} for words: \(P(\text{event})\)
  3. Size: Use \large, \small, etc. for size adjustments
  4. Numbering: In Quarto, add {#eq-label} after display equations for cross-referencing (Section 17.13)

Common Mistakes

Warning

Avoid these common errors:

  • Forgetting to escape braces: \{ not { when you want literal braces
  • Missing closing delimiters with \left and \right
  • Using * for multiplication instead of \times or \cdot
  • Not grouping multi-character subscripts/superscripts with braces

17.18 Summary

LaTeX provides powerful tools for mathematical typesetting (a complete symbol reference is in Appendix G):

  • Inline ($...$) and display ($$...$$) modes for different contexts
  • Greek letters and special symbols with backslash commands
  • Fractions, roots, and powers for complex expressions
  • Sums, products, and integrals with limits
  • Matrices with various bracket styles
  • Automatic sizing of delimiters with \left and \right
  • Numbered ({#eq-...}) and aligned (aligned, cases) display equations

Integration with Quarto and R Markdown means you can include precise mathematical notation alongside code and narrative—essential for reproducible scientific communication.

Exercises

TipPractice Exercises

Exercise 1: Basic Notation

Write the LaTeX code to produce these expressions:

  1. \(x^2 + y^2 = z^2\)
  2. \(\frac{-b \pm \sqrt{b^2-4ac}}{2a}\)
  3. \(\sum_{i=1}^{n} x_i^2\)

Exercise 2: Statistical Formulas

Write the LaTeX for these statistical expressions:

  1. Sample correlation coefficient
  2. Chi-square test statistic
  3. A 95% confidence interval

Exercise 3: Matrix Operations

Create a \(3 \times 3\) identity matrix using bmatrix environment.

Exercise 4: Integrate into Quarto

Create a Quarto document that:

  1. Explains a statistical concept with inline equations
  2. Includes a centered display equation for the main formula
  3. Uses appropriate Greek letters and symbols