Math inside RMarkdown

In side a text chunk, you can use mathematical notation if you surround it by dollar signs $ for “inline mathematics” and $$ for “displayed equations”. Do not leave a space between the $ and your mathematical notation.

Example: $\sum_{n=1}^{10} n^2$ is rendered as \(\sum_{n=1}^{10} n^2\).

Example: $$\sum_{n=1}^{10} n^2$$ is rendered as \[\sum_{n=1}^{10} n^2\].

The mathematical typesetting is based on LaTeX, so if you need to search for the way to make a particular symbol, include latex in your search. But note: Not all LaTeX macros are available without using additional packages, and those packages likely will only work if you are creating a PDF. On the plus side, if you are working in PDF, you can use additional packages that give much better control and/or easier syntax.

In LaTeX,

Mathematical Notation

Here are some common mathematical things you might use in statistics

\(x = y\) $x = y $
\(x < y\) $x < y $
\(x > y\) $x > y $
\(x \le y\) $x \le y $
\(x \ge y\) $x \ge y $
\(x^{n}\) $x^{n}$
\(x_{n}\) $x_{n}$
\(\overline{x}\) $\overline{x}$
\(\hat{x}\) $\hat{x}$
\(\tilde{x}\) $\tilde{x}$
\(\frac{a}{b}\) $\frac{a}{b}$
\(\frac{\partial f}{\partial x}\) $\frac{a}{b}$
\(\displaystyle \frac{\partial f}{\partial x}\) $\displaystyle \frac{a}{b}$
\(\binom{n}{k}\) $\binom{n}{k}$
\(x_{1} + x_{2} + \cdots + x_{n}\) $x_{1} + x_{2} + \cdots + x_{n}$
\(x_{1}, x_{2}, \dots, x_{n}\) $x_{1}, x_{2}, \dots, x_{n}$
\(\mathbf{x} = \langle x_{1}, x_{2}, \dots, x_{n}\rangle\) \mathbf{x} = \langle x_{1}, x_{2}, \dots, x_{n}\rangle$ (\bm from the bm pacakge would be better)
\(x \in A\) $x \in A$
\(|A|\) $|A|$
\(x \in A\) $x \in A$
\(A \subset B\) $x \subset B$
\(A \subseteq B\) $x \subseteq B$
\(A \cup B\) $A \cup B$
\(A \cap B\) $A \cap B$
\(X \sim {\sf Binom}(n, \pi)\) $X \sim {\sf Binom}(n, \pi)$ (sf for “slide font”)
\(\mathrm{P}(X \le x) = {\tt pbinom}(x, n, \pi)\) $\mathrm{P}(X \le x) = {\tt pbinom}(x, n, \pi)$ (tt for “typewriter type”)
\(P(A \mid B)\) $P(A \mid B)$
\(\mathrm{P}(A \mid B)\) $\mathrm{P}(A \mid B)$ (mathrm for “math roman font”
\(\{1, 2, 3\}\) $\{1, 2, 3\}$
\(\sin(x)\) $\sin(x)$
\(\log(x)\) $\log(x)$
\(\int_{a}^{b}\) $\int_{a}^{b}$
\(\left(\int_{a}^{b} f(x) \; dx\right)\) $\left(\int_{a}^{b} f(x) \; dx\right)$
\(\left[\int_{-\infty}^{\infty} f(x) \; dx\right]\) $\left[\int_{\-infty}^{\infty} f(x) \; dx\right]$
\(\left. F(x) \right|_{a}^{b}\) $\left. F(x) \right|_{a}^{b}$
\(\sum_{x = a}^{b} f(x)\) $\sum_{x = a}^{b} f(x)$
\(\prod_{x = a}^{b} f(x)\) $\prod_{x = a}^{b} f(x)$
\(\lim_{x \to \infty} f(x)\) $\lim_{x \to \infty} f(x)$
\(\displaystyle \lim_{x \to \infty} f(x)\) $\displaystyle \lim_{x \to \infty} f(x)$

Greek Letters

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

Aligning equations

If you want a sequence of aligned equations (often very useful for demonstrating algebraic manipulation or for plugging values into equations), use \begin{align*} ... \end{align*}. Separate lines with \\ and use & to mark where things should line up. Note: No dollar signs are needed when you use this method.

Example

This

\begin{align*}
a & = b \\
X &\sim {\sf Norm}(10, 3) \\
5 & \le 10
\end{align*}

produces

\[\begin{align*} a & = b \\ X &\sim {\sf Norm}(10, 3) \\ 5 & \le 10 \end{align*}\]