Skip to content

Instantly share code, notes, and snippets.

@jeroen
Last active September 14, 2021 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroen/32b8d15c7a0d373ab29714c4e5b479fe to your computer and use it in GitHub Desktop.
Save jeroen/32b8d15c7a0d373ab29714c4e5b479fe to your computer and use it in GitHub Desktop.
Test Rmd math server-side, borrowed from https://rpruim.github.io/s341/S19/from-class/MathinRmd.html
---
title: "Rending math to HTML in R using katex"
output:
html_document:
katex: true
self_contained: false
vignette: >
%\VignetteIndexEntry{Rending math to HTML in R using katex}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<style type="text/css">
td
{
padding:5px 10px 5px 10px;
}
</style>
## 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,
* macros begin with a backslash (`\`)
* curly braces (`{` and `}`) are used to surround items that are
to be considered as one object from LaTeX's perspective.
Without them, usually the next letter or digit will be used,
but that isn't usually what you want. For example
`$$\sum_x=1^10 x^2$$` produces
$$\sum_x=1^10 x^2$$
### Mathematical Notation
Here are some common mathematical things you might use in statistics
<table>
<tr><td>$x = y$</td><td> `$x = y $` </td> </tr>
<tr><td>$x < y$</td><td> `$x < y $` </td> </tr>
<tr><td>$x > y$</td><td> `$x > y $` </td> </tr>
<tr><td>$x \le y$</td><td> `$x \le y $` </td> </tr>
<tr><td>$x \ge y$</td><td> `$x \ge y $` </td> </tr>
<tr><td>$x^{n}$</td><td> `$x^{n}$` </td> </tr>
<tr><td>$x_{n}$</td><td> `$x_{n}$` </td> </tr>
<tr><td>$\overline{x}$</td><td> `$\overline{x}$` </td> </tr>
<tr><td>$\hat{x}$</td><td> `$\hat{x}$` </td> </tr>
<tr><td>$\tilde{x}$</td><td> `$\tilde{x}$` </td> </tr>
<tr><td>$\frac{a}{b}$ </td><td> `$\frac{a}{b}$`</td> </tr>
<tr><td>$\frac{\partial f}{\partial x}$ </td><td> `$\frac{\partial f}{\partial x}$`</td> </tr>
<tr><td>$\displaystyle \frac{\partial f}{\partial x}$ </td><td> `$\displaystyle \frac{\partial f}{\partial x}$`</td> </tr>
<tr><td>$\binom{n}{k}$ </td><td> `$\binom{n}{k}$`</td> </tr>
<tr><td>$x_{1} + x_{2} + \cdots + x_{n}$</td><td> `$x_{1} + x_{2} + \cdots + x_{n}$` </td> </tr>
<tr><td>$x_{1}, x_{2}, \dots, x_{n}$</td><td> `$x_{1}, x_{2}, \dots, x_{n}$` </td> </tr>
<tr><td>$\mathbf{x} = \langle x_{1}, x_{2}, \dots, x_{n}\rangle$</td><td> `\mathbf{x} = \langle x_{1}, x_{2}, \dots, x_{n}\rangle$` (`\bm` from the `bm` pacakge would be better) </td> </tr>
<tr><td>$x \in A$</td><td> `$x \in A$` </td> </tr>
<tr><td>$|A|$</td><td> `$|A|$` </td> </tr>
<tr><td>$x \in A$</td><td> `$x \in A$` </td> </tr>
<tr><td>$A \subset B$</td><td> `$x \subset B$` </td> </tr>
<tr><td>$A \subseteq B$</td><td> `$x \subseteq B$` </td> </tr>
<tr><td>$A \cup B$</td><td> `$A \cup B$` </td> </tr>
<tr><td>$A \cap B$</td><td> `$A \cap B$` </td> </tr>
<tr><td>$X \sim {\sf Binom}(n, \pi)$</td><td> `$X \sim {\sf Binom}(n, \pi)$` (`sf` for "slide font")</td> </tr>
<tr><td>$\mathrm{P}(X \le x) = {\tt pbinom}(x, n, \pi)$</td><td> `$\mathrm{P}(X \le x) = {\tt pbinom}(x, n, \pi)$` (`tt` for "typewriter type")</td> </tr>
<tr><td>$P(A \mid B)$</td><td> `$P(A \mid B)$` </td> </tr>
<tr><td>$\mathrm{P}(A \mid B)$</td><td> `$\mathrm{P}(A \mid B)$` (`mathrm` for "math roman font"</td> </tr>
<tr><td>$\{1, 2, 3\}$</td><td> `$\{1, 2, 3\}$` </td> </tr>
<tr><td>$\sin(x)$ </td><td> `$\sin(x)$`</td> </tr>
<tr><td>$\log(x)$ </td><td> `$\log(x)$`</td> </tr>
<tr><td>$\int_{a}^{b}$</td><td> `$\int_{a}^{b}$` </td> </tr>
<tr><td>$\left(\int_{a}^{b} f(x) \; dx\right)$</td><td> `$\left(\int_{a}^{b} f(x) \; dx\right)$` </td> </tr>
<tr><td>$\left[\int_{-\infty}^{\infty} f(x) \; dx\right]$</td><td> `$\left[\int_{\-infty}^{\infty} f(x) \; dx\right]$` </td> </tr>
<tr><td>$\left. F(x) \right|_{a}^{b}$</td><td> `$\left. F(x) \right|_{a}^{b}$` </td> </tr>
<tr><td>$\sum_{x = a}^{b} f(x)$ </td><td> `$\sum_{x = a}^{b} f(x)$`</td> </tr>
<tr><td>$\prod_{x = a}^{b} f(x)$ </td><td> `$\prod_{x = a}^{b} f(x)$`</td> </tr>
<tr><td>$\lim_{x \to \infty} f(x)$ </td><td> `$\lim_{x \to \infty} f(x)$`</td> </tr>
<tr><td>$\displaystyle \lim_{x \to \infty} f(x)$ </td><td> `$\displaystyle \lim_{x \to \infty} f(x)$`</td> </tr>
</table>
### Greek Letters
<table>
<tr><td>$\alpha A$</td><td>`$\alpha A$`</td><td>$\nu N$</td><td>`$\nu N$`</td></tr>
<tr><td>$\beta B$</td><td>`$\beta B$`</td><td>$\xi\Xi$</td><td>`$\xi\Xi$`</td></tr>
<tr><td>$\gamma \Gamma$</td><td>`$\gamma \Gamma$`</td><td>$o O$</td><td>`$o O$` (omicron)</td></tr>
<tr><td>$\delta \Delta$</td><td>`$\delta \Delta$`</td><td>$\pi \Pi$</td><td>`$\pi \Pi$`</td></tr>
<tr><td>$\epsilon \varepsilon E$</td><td>`$\epsilon \varepsilon E$`</td><td>$\rho\varrho P$</td><td>`$\rho\varrho P$`</td></tr>
<tr><td>$\zeta Z$</td><td>`$\zeta Z \sigma \,\!$`</td><td>$\Sigma$</td><td>`$\sigma \Sigma$`</td></tr>
<tr><td>$\eta H$</td><td>`$\eta H$`</td><td>$\tau T$</td><td>`$\tau T$`</td></tr>
<tr><td>$\theta \vartheta \Theta$</td><td>`$\theta \vartheta \Theta$`</td> <td>$\upsilon \Upsilon$</td><td>`$\upsilon \Upsilon$`</td></tr>
<tr><td>$\iota I$</td><td>`$\iota I$`</td><td>$\phi \varphi \Phi$</td><td>`$\phi \varphi \Phi$`</td></tr>
<tr><td>$\kappa K$</td><td>`$\kappa K$`</td><td>$\chi X$</td><td>`$\chi X$`</td></tr>
<tr><td>$\lambda \Lambda$</td><td>`$\lambda \Lambda$`</td><td>$\psi \Psi$</td><td>`$\psi \Psi$`</td></tr>
<tr><td>$\mu M$</td><td>`$\mu M$`</td><td>$\omega \Omega$</td><td>`$\omega \Omega$`</td></tr>
</table>
### 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*}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment