---
title: "Mathematical Expressions in R Markdown"
author: "Michael Tiefelsdorf & Yeamin Faria Chowdhury"
date: "`r Sys.Date()`"
output:
  html_document:
    toc: yes
    toc_float:
      collapsed: no
      smooth_scroll: no
    number_sections: yes
    toc_depth: 2
---

# Introduction

$\LaTeX$ was developed by Leslie Lamport in the early 1980's. It is built on Donald Knuth's $\TeX$, a computer scientist who needed _easy to use_ mathematical type-setting capabilities to write his ground-breaking multiple volumes "The Art of Computer Programming". Before the emergence of Donald Knuth's $\TeX$ and modern laser printers it was extremely cumbersome to write mathematical academic texts with a type-writer. One needed to swap wheels for Brother type-writers or heads for IBM type-writers with different fonts for mathematical symbols and characters as well as position the type locations manually up and down or back and forth. These were the golden days of _TipEx_, which was used to extensively to cover-up any minor type-setting mishaps.

$\LaTeX$ stands for lazy-$\TeX$ and it comprises of $\TeX$ macros allowing an author to write professionally type-set academic publications with "minimal" effort. $\LaTeX$ is the _de facto_ standard language for symbolic mathematical type-setting and many printing houses prefer manuscript submissions in $\LaTeX$. Many newer WYSIWYG typesetting systems such as  MS Word's Equation Editor (you find it in Words's `Insert` tab) can easily transcribe and render $\LaTeX$ commands. Aside from the robustness of $\LaTeX$ commands, a major advantage of symbolic type-setting languages over WYSIWYG type-setting is the ease of recycling and revising already type-set equations.

# $\LaTeX$ Typesetting Basics
* The dollar symbol `$...$` starts and ends the mathematical _inline_ typesetting mode of R Markdown. For instance, `$A=\pi*r^{2}$` is being rendered into the equation of the area of a circle $A=\pi \cdot r^{2}$. The double dollar symbol `$$...$$` starts and ends mathematical expressions in the _display_ format. For the differences between the inline and display formats see the [next section](#eqFormats). 

* The backslash `\name` informs $\LaTeX$ of a pending _macro_ with the label `name`; macros provide specific mathematical formatting instructions to $\LaTeX$. Mathematical symbols themselves are macros. See the fraction macro `$\frac{1}{2}$` or the symbol `$\pi$`, which both render to $\frac{1}{2}$ and $\pi$, respectively.

* The underscore `_` and hat `^` characters generate sub- and superscripts, respectively. For instance, `$a_{ij}^{1/2}$` renders to the term $a_{ij}^{1/2}$. Notice that the dimension of the sub- and superscripts are automatically down-scaled.

* A pair of _curly brackets_ `{...}` has three uses in $\LaTeX$:
    1. They enclose arguments of $\LaTeX$ macros like the numerator `num` and the denominator `den` in the fraction macro `$r=\frac{nun}{den}$`.
    2. They enclose a _set_ of symbols. While for the single character `2` the statements `$r^2$` and `$r^{2}$` both result to be rendered into $r^2$, it is no longer the case for a set of symbols. For instance, the set of symbols `2/3` in `$r^2/3$` and `$r^{2/3}$` render to $r^2/3$ and $r^{2/3}$, respectively. Both expressionn have totally different meanings. Another example with an _optional_ parameter `[n]` --- here in _square_ brackets --- is the square-root macro `$\sqrt[n]{x+y}$`, which renders to $\sqrt[n]{x+y}$. Skipping the optional parameter `[n]` in `$\sqrt{x+y}$` renders to $\sqrt{x+y}$.
    3. Formatting inside _curly brackets_  applies to all symbols within the curly brackets, e.g, `${\bf abc}, {\em def}, {\rm ghi and}{\tt jkl}$` render in the math mode to the styles __boldface__, roman, _math italics_ and type-writer, that is, $${\bf abc} - {\rm ghi } - def - {\tt jkl}.$$ \ In mathematical expressions the macro `\mbox{plain text}` allows to enter standard text strings such as $$A=\pi \cdot r^{2} \mbox{ circle area with radius } r.$$

* Environment macros, which encompass several lines of $\LaTeX$ type-setting commands, _begin_ and _end_ using the syntax `$\begin{envMac} \ldots \end{envMac}$`. See the [example](#aligned) for a set of `=`-sign aligned display equations.

# Equation Formats {#eqFormats}

## Inline Equations

An inline equation is embedded within the paragraph of text. For example, $\hat{\sigma}^2=\frac{1}{n-1} \cdot \sum_{i=1}^n{(x_i-\bar{x})^2}$ is the equation of the estimate of the sample variance.

## Display Equations
Whereas, a display equation *stands out* and it is shown centered within its own line within a paragraph: 
$$
\hat{\sigma}^2=\frac{1}{n-1} \cdot \sum_{i=1}^n{(x_i-\bar{x})^2} 
$$ 
Notice the difference in the size of the fraction $\frac{1}{n-1}$ as well as the placement of the sub- and super-scripts for the summation symbol $\sum_{i=1}^n$ in the display equation mode versus in the inline equation mode.

## Numbered Display Equations in `bookdown`
The package [bookdown](https://bookdown.org/yihui/rmarkdown/books.html) allows you to extend the capabilities of `RMarkdown` by, for instance, adding reference numbers to figures, sections or display equations. However, in order to keep this document simple it has not been setup as `bookdown` document, which would require a different YAML header and an index file. Consequently, the following code just demonstrates the concept of adding and referring to the equation number `eq:binom` by its name:

```
\begin{equation}
f(x|\pi,n) = {n \choose x} \pi^{x} (1-\pi)^{n-x} (\#eq:binom)
\end{equation}
```

This code would produce a unique number at the right margin of the equation for the binomial distribution and allows to refer to this equation `\@ref(eq:binom)` in the text by its number.


## Aligned Set of Display Equations {#aligned}
Aligned equations are started and ended with the _environment macro_ `\begin{aligned}` and `\end{aligned}`. The equations are centered around the equal sign, which is symbolized here by `&=`. Either the left-hand or right-hand parts around `&=` can be left blank. A new equation line is started with `\\`. See for example
```
$$
\begin{aligned}
\hat{\sigma}^2 &= \cdots\\
               &= \cdots
\end{aligned}
$$
```
which renders for the definitional and computational variance expressions to
$$
\begin{aligned}
\hat{\sigma}^2 &= \frac{1}{n-1} \cdot \sum_{i=1}^n{(x_i-\bar{x})^2} \mbox{ definitional form} \\
&= \frac{1}{n-1} \cdot \left( \sum_{i=1}^n{x_i^2} - n \cdot \bar{x}^2 \right) \mbox{ computational form}
\end{aligned}
$$

# Selected Formatting Options

## Delimiters
Delimiter come as _left_ and _right_ __pairs__ to enclose mathematical expressions.

* _Parentheses_ are obtained by `\left( \cdots \right)`, i.e, $\left( \cdots \right)$.

* _Vertical bars_ are generated by `\left| \cdots \right|`, i.e, $\left| \cdots \right|$.

* _Brackets_ are obtained by `\left[ \cdots \right]`, i.e, $\left[ \cdots \right]$, and `\left\{ \cdots \right\}`, i.e, $\left\{ \cdots \right\}$. Note that in order to obtain the curly bracket symbol the backslash syntax `\{` and `\}` needs to be used.

* Nested delimiters scale dynamically such as `\frac{1} {\left( \frac{2} {\left( \frac{3}{4} \right)} \right) }`, which is render to $$\frac{1}{\left( \frac{2}{\left( \frac{3}{4} \right)} \right) }$$.

* Either a left or right delimiter can be _suppressed_ by `$\left.$` or `$\right.$`, respectively. For instance, the definition of the density function $f(x)$ for the uniform distribution in the range $x \in [0,2]$ is
$$
f(x) \equiv \left\{ 
               \begin{array}{ll}
                 \frac{1}{2} & \mbox{if } 0 \leq x \leq 2 \\
                 0 & \mbox{otherwise}
               \end{array}
             \right.
$$
is obtained with the syntax
```
f(x) \equiv \left\{ 
               \begin{array}{ll}
                 \frac{1}{2} & \mbox{if } 0 \leq x \leq 2 \\
                 0 & \mbox{otherwise}
               \end{array}
             \right.
```
See the section [Vectors and Matrices](#array) section for the syntax of the `\begin{array}` and `\end{array}` _environment macro_ in the above syntax.
    

## Narrow and Wide Accents
_Narrow_ accents covering just one character are obtained by the macros `\hat{a}`, i.e, $\hat{a}$, --- `\bar{a}`, i.e., $\bar{a}$, --- `\dot{a}`, i.e., $\dot{a}$, --- `\vec{a}`,i.e., $\vec{a}$, --- and `\tilde{a}`, i.e., $\tilde{a}$.

In contrast, _wide_ accents cover more than one symbol and they are produce by the macros `\widehat{abc}`, i.e., $\widehat{abc}$, --- `\overline{abc}`, i.e., $\overline{abc}$, --- and  `\widetilde{abc}`, i.e., $\widetilde{abc}$.

## Over- and Under-bracing
The macros for _over_ and _under-bracing_ are `\overbrace{a^2+b^2}^{=c^2}`, i.e., $\overbrace{a^2+b^2}^{=c^2}$, and `\underbrace{a^2+b^2}_{=c^2}`, i.e., $\underbrace{a^2+b^2}_{=c^2}$, respectively, are shown here for the Pythagorean theorem of right-angled triangles. 

Over- and under-bracing can be nested:
$$
\underbrace{a + \overbrace {b + \cdots + y}^{24} + z}_{26} \mbox{ with } a=b=\cdots=z=1
$$

## Vectors and Matrices {#array}
A _vector_ is a column of numbers, which are stacked on top of each other and enclosed by parentheses. Vectors are denoted by _lower-case upright bold letters_ and if necessary for clarification their dimension as one column of $n$ rows is provided, i.e., ${\bf x}_{n \times 1}$.

To generate a vector the `\begin{array}{c}` and `\end{array}` environment macro is used where the option `{c}` denotes that the values in the column are centered. Like for the aligned equations, each new row of the vector is separated by the symbol `\\`. The follow syntax generates a vector
```
{\bf x}_{n \times 1} = \left( \begin{array}{c}
    x_1 \\ x_2 \\  \vdots \\ x_n 
\end{array} \right)
```
and it is rendered to
$$
{\bf x}_{n \times 1} = \left( \begin{array}{c}
    x_1 \\ x_2 \\  \vdots \\ x_n 
\end{array} \right)
$$
Since this takes substantial vertical space on the page, vectors can also written in their _transposed form_, i.e., ${\bf x}=(x_1,x_2, \cdots, x_n)^T$, on a single line.

_Matrices_ are a side-by-side collection $m$ of vectors with identical length $n$. The _first subscript_ of each matrix element denotes the $i^{th}$ row whereas the _second subscript_ addresses the $j^{th}$ column. Matrices are denoted by _upper-case upright bold letters_ 

The `\begin{array}` and `\end{array}` environment of vectors extends to matrices with the option `{c...c}` denoting $m$ centered columns. Again new rows are started by `\\` and the $m$ elements $a_{ij}$ of the $i^{th}$ row are separated by  `&`. The syntax
```
{\bf X }_{n \times m} =
\left(
  \begin{array}{cccc}
    x_{11} & x_{12} & \cdots & x_{1m} \\
    x_{21} & x_{22} & \cdots & x_{2m} \\
    \vdots & \vdots & \ddots & \vdots \\
    x_{n1} & x_{n2} & \cdots & x_{nm}
  \end{array}
\right)
```
is rendered to the matrix

$$
{\bf X }_{n \times m} =
\left(
  \begin{array}{cccc}
    x_{11} & x_{12} & \cdots & x_{1m} \\
    x_{21} & x_{22} & \cdots & x_{2m} \\
    \vdots & \vdots & \ddots & \vdots \\
    x_{n1} & x_{n2} & \cdots & x_{nm}
  \end{array}
\right)
$$

Aside from the centered alignment `c` option, other alignment options for the columns are `r` for _right_ alignment and `l` for _left_ alignment, respectively.

# Special Letters

## Lower Case Greek

Lower case Greek letters are denoted by their names as macro:

|         |         |         |         |         |         |
|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
|$\alpha$ `\alpha` |$\beta$ `\beta` |$\gamma$ `\gamma`|$\delta$ `\delta`|$\epsilon$ `\epsilon`|$\varepsilon$ `\varepsilon`|
|$\zeta$ `\zeta` |$\eta$ `\eta`   |$\theta$ `\theta` |$\vartheta$ `\vartheta`|$\iota$ `\iota`|$\lambda$ `\lambda`|
|$\mu$  `\mu`  |$\nu$ `\nu`   |$\xi$ `\xi`   |$o$ `o`     |$\pi$ `\pi`   |$\varpi$ `\varpi`|
|$\rho$ `\rho`  |$\varrho$ `\varrho`|$\sigma$ `\sigma`|$\varsigma$ `\varsigma`|$\tau$ `\tau` |$\upsilon$ `\upsilon`|
|$\phi$  `\phi` |$\varphi$ `\varphi`|$\chi$ `\chi`  |$\psi$ `\psi`  |$\omega$ `\omega` |         |

## Upper Case Greek

Upper case Greek letters start with the first letter of their names capitalized:

|         |         |         |         |         |         |
|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
|$\Gamma$ `\Gamma` |$\Delta$ `\Delta` |$\Theta$ `\Theta` |$\Lambda$ `\Lambda`|$\Xi$ `\Xi`   |$\Pi$ `\Pi`   |
|$\Sigma$ `\Sigma`|$\Upsilon$ `\Upsilon`|$\Phi$ `\Phi` |$\Psi$ `\Psi`  |$\Omega$ `\Omega` |         |

## Calligraphic
The twenty-six uppercase calligraphic, also called script letters, are available in the mathematics mode with the macro `\cal`:
$$
{\cal A, B, C,} \ldots {\cal, Z}
$$

#  Selected Mathematical Functions and Symbols

## Functions
In general, the functions are obtained as macro by their names. In contrast to mathetical symbols, functions are rendered non-italics:

|         |         |         |         |         |         |
|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
|$\cos$ `\cos`|$\arccos$ `\arccos`|$\cosh$ `\cosh`|$\sin$ `\sin`|$\arcsin$ `\arcsin`|$\sinh$ `\sinh`|
|$\tan$ `\tan`|$\arctan$ `\arctan`|$\coth$ `\coth`|$\cot$ `\cot`| $\min$ `\min` | $\max$ `\max` |
|$\exp$ `\exp` | $\log$ `\log` | $\ln$ `\ln` | $\Pr$ `\Pr` |$\inf$ `\inf` |$\lim$ `\lim` |

## Binary Operation Symbols

Binary operations like `2 \times 3` have arguments at their left and right sides:

|         |         |         |         |         |         |
|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
|$\pm$ `\pm`|$\mp$ `\mp`|$\times$ `\times`|$\div$ `\div`|$\circ$ `\circ`|$\cdot$ `\cdot`| $\cap$ `\cap`|
|$\cup$ `\cup`|$\setminus$ `\setminus`|$\oplus$ `\oplus`|$\ominus$ `\ominus`|$\otimes$ `\otimes`|$\odot$ `\odot`|

## Relation Symbols

Relation symbols like `6 = 2 \times 3` have arguments at their left and right sides:

|         |         |         |         |         |         |
|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
|$\leq$ `\leq`|$\geq$ `\geq`|$\equiv$ `\equiv`|$\sim$ `\sim`|$\subset$ `\subset`| $\subseteq$ `\subseteq`|
|$\supset$ `\supset`| $\approx$ `\approx`|$\neq$ `\neq`|$\in$ `\in`| $\ni$ `\ni`| $\propto$ `\propto`|

## Arrows

Arrows come in different length and directions:

|         |         |         |         |
|:-------:|:-------:|:-------:|:-------:|
|$\leftarrow$ `\leftarrow`|$\Leftarrow$ `\Leftarrow`|$\rightarrow$ `\lrightarrow`|$\Rightarrow$ `\Rightarrow`|
|$\leftrightarrow$ `\leftrightarrow`|$\Leftrightarrow$ `\Leftrightarrow`|$\Longleftarrow$ `\Longleftarrow`|$\Longrightarrow$ `\Longrightarrow`|
|$\Longleftrightarrow$ `\Longleftrightarrow`|$\uparrow$ `\uparrow`|$\downarrow$ `\downarrow`|$\updownarrow$ `\updownarrow`|

## Miscellaneous Symbols

Miscellaneous symbols predominately cover set and number theory:

|         |         |         |         |         |         |
|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
|$\prime$ `\prime`|$\forall$ `\forall`|$\infty$ `\infty`|$\emptyset$ `\emptyset`|$\exists$ `\exists`|$\Re$ `\Re`|
|$\Im$ `\Im`|$\|$ `\|`|$\backslash$ `\backslash`|$\angle$ `\angle`|$\partial$ `\partial`|

## Variable-sized Symbols

Variable-sized symbols adjust to being either in the display of inline equation mode and the height of argument that follow them. They can have sub- and superscripts:

|         |         |         |         |         |         |
|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
|$\sum$ `\sum`|$\prod$ `\prod`|$\int$ `\int`| $\bigcap$ `\bigcap`|$\bigcup$ `\bigcup`|$\biguplus$ `\biguplus`|
