Mathematics in R Markdown

Write math formulas by LaTex in R Markdown.
Markdown
LaTex
Author

NING LI

Published

Feb 07, 2023

The ability to beautifully and accurately write mathematical expressions is crucial to many fields, particularly in scientific research. Luckily, R Markdown provides an excellent platform for doing this, thanks to its compatibility with LaTeX, a popular typesetting system renowned for high-quality typesetting of mathematical and scientific content.

Mathematical Formulas

To write mathematical formulas in R Markdown, we use LaTeX syntax. This is enclosed in dollar signs ($) for inline formulas and double dollar signs ($$) for display formulas1.

Inline mathematical

The formula for the area of a circle is $A = \pi r^2$.

And it will render as:

\[A = \pi r^2\]

Display Formula

The formula for the area of a circle is:

$$
A = \pi r^2
$$

Which will render as:

\[A = \pi r^2\]

Examples

  1. Fraction:
$$
\begin{align*}
\frac{a}{b}
\end{align*}
$$

renders as:

\[ \begin{align*} \frac{a}{b} \end{align*} \]

  1. Exponents and Subscripts:

we use ^ for superscripts (exponents) and _for subscripts.

$$
\begin{align*}
a^{b}\\
c_{d}
\end{align*}
$$

renders as:

\[ \begin{align*} a^{b}\\ c_{d} \end{align*} \]

  1. Roots and Logarithms:
$$
\begin{align*}
Square\ root: \sqrt{a}\\
nth\ root: \sqrt[n]{b}\\
natural\ log: \ln{c}\\
log\ base\ n: \log_{n}{d}
\end{align*}
$$

renders as:

\[ \begin{align*} Square\ root: \sqrt{a}\\ nth\ root: \sqrt[n]{b}\\ natural\ log: \ln{c}\\ log\ base\ n: \log_{n}{d} \end{align*} \]

  1. Quadratic equation:
$$
\begin{align*}
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
\end{align*}
$$

renders as:

\[ \begin{align*} x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \end{align*} \]

  1. Summation:
$$
\begin{align*}
\sum_{i=1}^{n} i = \frac{n*(n + 1)}{2}
\end{align*}
$$

renders as:

\[ \begin{align*} \sum_{i=1}^{n} i = \frac{n*(n + 1)}{2} \end{align*} \]

  1. Integral:
$$
\begin{align*}
\int_{0}^{\infty} e^{-x} dx = 1\\
\int_{a}^{b} f(x) \, dx
\end{align*}
$$

renders as:

\[ \begin{align*} \int_{0}^{\infty} e^{-x} dx = 1\\ \int_{a}^{b} f(x) \, dx \end{align*} \]

  1. Matrix representation:
$$
\begin{align*}
\begin{bmatrix}
a & b \\
c & d 
\end{bmatrix}
\end{align*}
$$

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

Renders as:

\[ \begin{align*} \begin{bmatrix} a & b \\ c & d \end{bmatrix} \end{align*} \] \[ \begin{align*} \begin{pmatrix} a & b \\ c & d \end{pmatrix} \end{align*} \]

  1. Trigonometric functions:
$$
\begin{align*}
Sine: \sin(x)\\ 
Cosine: \cos(x)\\ 
Tangent: \tan(x)\\ 
Cotangent: \cot(x)\\ 
Secant: \sec(x)\\ 
Cosecant: \csc(x) \\ 
Degree: 30^\circ
\end{align*}
$$

renders as:

\[ \begin{align*} Sine: \sin(x)\\ Cosine: \cos(x)\\ Tangent: \tan(x)\\ Cotangent: \cot(x)\\ Secant: \sec(x)\\ Cosecant: \csc(x) \\ Degree: 30^\circ \end{align*} \]

  1. Inverse trigonometric functions:
$$
\\begin{flalign}
Inverse\ sine (arcsine): \arcsin(x) &\\
Inverse\ cosine (arccosine): \arccos(x) &\\
Inverse\ tangent (arctangent): \arctan(x) &
\end{flalign}
$$

render as:

\[ \small \begin{flalign*} \text{Inverse sine (arcsine):} \quad & \arcsin(x) &\\ \text{Inverse cosine (arccosine):} \quad & \arccos(x) &\\ \text{Inverse tangent (arctangent):} \quad & \arctan(x) & \end{flalign*} \small \]

Mathematical Symbols

R Markdown (through LaTeX) provides a wealth of mathematical symbols. Here are some of the most commonly used ones:

Symbol Code
\(+\) +
\(-\) -
\(\times\) \times
\(\div\) \div
\(\pm\) \pm
\(\cdot\) \cdot
\(\circ\) \circ
\(\sqrt{x}\) \sqrt{x}
\(\frac{a}{b}\) \frac{a}{b}
\(\sum\) \sum
\(\prod\) \prod
\(\int\) \int
\(\partial\) \partial
\(\infty\) \infty
\(\forall\) \forall
\(\exists\) \exists
\(\Rightarrow\) \Rightarrow
\(\Leftrightarrow\) \Leftrightarrow
\(\to\) \to
\(\subset\) \subset
\(\supset\) \supset
\(\subseteq\) \subseteq
\(\supseteq\) \supseteq
\(\notin\) \notin
\(\neq\) \neq
\(\approx\) \approx
\(\equiv\) \equiv
\(\leq\) \leq
\(\geq\) \geq
\(\ll\) \ll
\(\gg\) \gg
\(\alpha\) \alpha
\(\beta\) \beta
\(\gamma\) \gamma
\(\delta\) \delta
\(\epsilon\) \epsilon
\(\zeta\) \zeta
\(\eta\) \eta
\(\theta\) \theta
\(\iota\) \iota
\(\kappa\) \kappa
\(\lambda\) \lambda.
\(\mu\) \mu
\(\nu\) \nu
\(\xi\) \xi
\(\pi\) \pi
\(\rho\) \rho
\(\sigma\) \sigma
\(\tau\) \tau
\(\upsilon\) \upsilon
\(\phi\) \phi
\(\chi\) \chi
\(\psi\) \psi
\(\omega\) \omega
\(\Gamma\) \Gamma
\(\Delta\) \Delta
\(\Theta\) \Theta
\(\Lambda\) \Lambda
\(\Xi\) \Xi
\(\Pi\) \Pi
\(\Sigma\) \Sigma
\(\Phi\) \Phi
\(\Psi\) \Psi
\(\Omega\) \Omega
Social share button
Back to top

Footnotes

  1. https://www.cnblogs.com/nowgood/p/latexstart.html#_nav_4↩︎

Reuse