3  Writing mathematical notation in quarto files

A quarto document can be used to combine code, code outputs (e.g., calculations, figures, and tables), and other technical content, including mathematical notation. Similar to other written languages, mathematical notation helps you develop and communicate ideas. In this course, we will use formal model descriptions to specify model assumptions. However, the technical aspects of writing mathematical notation are not obvious. Therefore, the following is an attempt to introduce \(\LaTeX\) syntax for mathematical notation. \(\LaTeX\) is a well-described system for writing technical documents, including mathematical notation, for example, in quarto files, but also in other source document formats.

3.1 Using \(\LaTeX\) syntax in quarto documents - The basics

We can add inline mathematics using $1 + 2 = 3$, this will render to \(1+2=3\). Using two $ signs will put the equation on a new line.

$$ 1 + 2 = 3$$

\[1+2=3\]

Mathematical operators are rendered as is, and from special operator commands (Table 3.1).

Table 3.1: Syntax for basic mathematical notation
Syntax Output
+ \(+\)
- \(-\)
\times \(\times\)
\frac{a}{b} \(\frac{a}{b}\)
\sqrt{a + b} \(\sqrt{a + b}\)
a^2 \(a^2\)
y_i \(y_i\)

Greek letters are similarly rendered using specific syntax (Table 3.2).

Table 3.2: Syntax for Greek letter
\alpha A \(\alpha A\) \beta B \(\beta B\) \gamma \Gamma \(\gamma \Gamma\) \delta \Delta \(\delta \Delta\)
\epsilon \varepsilon E\(\epsilon \varepsilon E\) \zeta Z\(\zeta Z\) \eta H \(\eta H\) \theta \vartheta \Theta\(\theta \vartheta \Theta\)
\iota I\(\iota I\) \kappa K \(\kappa K\) \lambda \Lambda \(\lambda \Lambda\) \mu M \(\mu M\)
\nu N \(\nu N\) \xi \Xi\(\xi \Xi\) o O\(o O\) \pi \Pi\(\pi \Pi\)
\rho \varrho P\(\rho \varrho P\) \sigma \Sigma \(\sigma \Sigma\) \tau T \(\tau T\) \upsilon \Upsilon \(\upsilon \Upsilon\)
\phi \varphi \Phi \(\phi \varphi \Phi\) \chi X \(\chi X\) \psi \Psi\(\psi \Psi\) \omega \Omega \(\omega \Omega\)

3.2 A language for describing models

McElreath (McElreath 2020, 77) uses multi-line equations to describe models. We can reproduce this output using the align environment in LaTeX.

$$
\begin{align}
y_i &\sim \text{Normal}(\mu_i,\sigma) \\
\mu_i &= \beta x_i \\
\beta &\sim \text{Normal}(0,10) \\
\sigma &\sim \text{Exponential}(1) \\
x_i &\sim \text{Normal}(0,1) \\
\end{align}
$$

\[ \begin{align} y_i &\sim \text{Normal}(\mu_i,\sigma) \\ \mu_i &= \beta x_i \\ \beta &\sim \text{Normal}(0,10) \\ \sigma &\sim \text{Exponential}(1) \\ x_i &\sim \text{Normal}(0,1) \\ \end{align} \]

In the example above, \\ will create a new line and using the align environment with the & operator creates alignment points for each line. I’ve put the alignment points on \sim and = operators to replicate the description in the book.

3.3 Operatorname and text

We will often use short-hand descriptions of probability distributions with corresponding parameters like \(\text{Normal}(\mu,\sigma)\), \(\text{Binomial}(N, p)\), or \(\text{Uniform}(\min, \max)\). In these examples (and the exmaple above), I’ve used \text{Normal}(\mu, \sigma) to define the distribution (Normal). Using \text can have consequences for readability and an alternative can be \operatorname which respect spacing between operators in an equation. Using \text or \operatorname will both render text.

$$
\text{Pr}(p|w,n) = 
  \frac{\text{Binomial}(w|n,p)\text{Uniform}(p|0,1)}
{\int\text{Binomial}(w|n,p)\text{Uniform}(p|0,1)dp}
$$

\[ \text{Pr}(p|w,n) = \frac{\text{Binomial}(w|n,p)\text{Uniform}(p|0,1)} {\int\text{Binomial}(w|n,p)\text{Uniform}(p|0,1)dp} \]

$$
\operatorname{Pr}(p|w,n) = \frac{\operatorname{Binomial}(w|n,p)\operatorname{Uniform}(p|0,1)}
{\int\operatorname{Binomial}(w|n,p)\operatorname{Uniform}(p|0,1)dp}
$$

\[ \operatorname{Pr}(p|w,n) = \frac{\operatorname{Binomial}(w|n,p)\operatorname{Uniform}(p|0,1)}{\int\operatorname{Binomial}(w|n,p)\operatorname{Uniform}(p|0,1)dp} \]

In the equations above, using the \operatorname instead of \text introduce a space between operators. This is also obvious when using built in operators, such as \log compared to writing \text{log}

a \log b

a \operatorname{log} b

a \text{log} b

\(a \log b\)

\(a \operatorname{log} b\)

\(a \text{log} b\)

3.4 Subscript and superscript

Subscripts and superscripts can be used to indicate indices or mathematical operations.

To create a subscript in math mode (using e.g., $), we can use the _ operator. E.g., the i:th observation of heights can be referenced as h_i, rendering to \(h_i\). Subscripts can be expanded to multi-character using curly brackets, e.g., h_{i,j} renders to \(h_{i,j}\). Subscripts can also be nested using e.g., h_{i_j}, rendering to \(h_{i_j}\). Note that McElreath often uses square brackets for nested subscripts to improve readability, e.g., \(\operatorname{logit}(p_i) = \alpha_{\text{TANK}[i]}\) (\operatorname{logit}(p_i) = \alpha_{\text{TANK}[i]}, (McElreath 2020, 403)).

Superscripts are create using the ^ operator in math mode. E.g., \(c^2 = a^2 + b^2\) (c^2 = a^2 + b^2). Similarly to the subscript syntax, superscripts can be nested and contain multiple characters and operators. Additionally, superscript and subscripts can be combined, also with mathematical operators.

n^{k+1}

h_{i,j}^2

\sum_{i=1}^n

\(n^{k+1}\)

\(h_{i,j}^{k+1}\)

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

Notice that some rendering will be different when switching from inline to display mode.

$\sum_{i=1}^n (x_i - \bar{x})^2$

$$ \sum_{i=1}^n (x_i - \bar{x})^2 $$

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

\[ \sum_{i=1}^n (x_i - \bar{x})^2 \]

McElreath, Richard. 2020. Statistical Rethinking: A Bayesian Course with Examples in R and Stan. Second edition. Chapman & Hall/CRC Texts in Statistical Science Series. Boca Raton: CRC Press.