owned this note
owned this note
Published
Linked with GitHub
# ECS132 LaTeX Math Reference
LaTeX can be daunting to get started with, and the documentation dense. I put this notepad together to demo a subset of LaTeX relevant to the course; and because it's on HackMD, it also demos some Markdown at the same time. I hope it helps you out!
## Entering Math Mode
To enter math mode, surround your LaTeX code with `$` for inline and `$$` for long form. For example, `$P$` gives you $P$, while `$$P$$` will put it on its own line.
You can also use the LaTeX commands directly:
```
\begin{equation}
P(X)
\end{equation}
```
renders as:
\begin{equation*}
P(X)
\end{equation*}
Most Markdown-based editors -- like Jupyter -- will recognize both forms.
## Some Simple Equations
Many basic equations don't require any special syntax. Upper-case letters will be converted to their "mathy" formats, and operations and symbols will be properly spaced.
* `$P(A)-1$`: $P(A)-1$
* `$P(A)+P(B)$`: $P(A)+P(B)$
* `$P(A)P(B)$`: $P(A)P(B)$
Some symbols need to be escaped with `\` because they have special meaning to LaTeX. For example, curly braces: `$S = \{1,2,3,4,5,6\}$`: $S = \{1,2,3,4,5,6\}$.
Some operations have special commands. For example, the "x" **multiplication** symbol is invoked with `\times`: `$A \times B$` gives $A \times B$.
## Subscripts and Superscripts
**Subscripts** are defined with `_` and **superscripts** with `^`. For example:
* `$A_i$`: $A_i$
* `$A^i$`: $A^i$
If the sub or superscript has multiple symbols, it needs to be fenced with `{}`.
* `$(1-p)^{k-1}$`: $(1-p)^{k-1}$
* `$A_{i-1}$`: $A_{i-1}$
## Fractions
Long-form **fractions** use the `\frac{}{}` command: the numerator goes in between the first braces and denominator in the second. For example, `$$\frac{n!}{(n-k)!k!}$$` gives you: $$\frac{n!}{(n-k)!k!}$$
For **fractions with braces**, we can use the `\left(` and `\right)` commands. Note that both are necessary. For example, `$$\left( \frac{n-1}{n} \right)^(k-1)$$` gives: $$\left( \frac{n-1}{n} \right)^{k-1}$$
Also note that I threw an exponent on there by tacking on `^{k-1}` -- LaTeX is smart enough to apply it to the entire braced expression.
## Counting
**Counting** notation works similarly, but the command is either `\choose` or `\binom{}{}`. For example, `$$\binom{n}{k}$$` gives: $$\binom{n}{k}$$
and we get the same with `$$n \choose k$$`: $$n \choose k$$
Note that, with `\choose`, we need to wrap it in braces if there are other terms in order to avoid ambiguity. So, more precisely, we should do `$${n \choose k}$$`.
## Set Notation
We use **intersection** and **union** a lot in this class! These two symbols, like many other math operators, have their own commands:
* Union is `\cup`, like `$A \cup B$`: $A \cup B$
* Intersection is `\cap`, like `$A \cap B$`: $A \cap B$
We also want to be able to donate **membership**.
* "Is an element of" is `\in`, like `$a_i \in A$`: $a_i \in A$
* "Is not an element of" is `\notin`, like `$x \notin A$`: $x \notin A$
And of course, sub- and supersets:
* "Is a subset of" is `\subseteq`, like `$V \subseteq W$`: $V \subseteq W$
* "Is a *proper* subset of" is `\subset`, like `$V \subset W$`: $V \subset W$
## Comparisons
The comparison operators follow a somewhat standard format, with **equals**, **less than**, and **greater than** using their raw symbols and the combined operators having their own commands:
* **equals** uses the raw symbol `=`: `$A = B$` gives $A = B$
* **less than** uses the raw symbol `<`: `$A < B$` gives $A < B$
* **greater than** uses the raw symbol `>`: `$A > B$` gives $A > B$
* **less than or equal to** uses `\leq` or `\leq`: `$A \leq B$` gives $A \leq B$
* **greater than or equal to** is similar, `\geq` or `ge`: `$A \geq B$` gives $A \geq B$
* **not equal to** is, you guess it, `\neq`: `$A \neq B$` gives $A \neq B$
The [On-Line Encyplopedia of Integer Sequences](https://oeis.org/wiki/List_of_LaTeX_mathematical_symbols) has a nice chart of many additional unary and binary operators for your reference.
## Additional Notation
The **complement** symbol has its own command, `\complement`. We can use it along with the superscript command like so:
* `$P(A^\complement) = 1 - P(A)$`: $P(A^\complement) = 1 - P(A)$
**Conditional** probability uses a raw pipe `|` symbol. So `$$P(A|B) = \frac{P(A)P(B|A)}{P(B)}$$` (Baye's Rule!) gives: $$P(A|B) = \frac{P(A)P(B|A)}{P(B)}$$
## Compound Operators
**Summation** combines the sub and superscript syntax with its own command `\sum`. Intuitively, below the sum uses subscript, and above uses superscript. For example, `$$\sum_{i=0}^{10} n_i$$` gives: $$\sum_{i=0}^{10} n_i$$
**Products** work similarly. For example, `$$\prod_{i=0}^{k} n-i$$` gives: $$\prod_{i=0}^{k} n-i$$
**Integration** uses the `\int` command; `$$\int_{a}^{\infty} f_X (x) dx$$` gives: $$\int_{a}^{\infty} f_X (x) dx$$
Note how I slipped in that `\infty` to get $\infty$.
## Going Further
Note that all of these commands can be combined together! Just make sure to keep track of how many braces you've used -- just like in any other programming language (and yes, depressingly, LaTeX is Turing-complete), things will go haywire if you mismatch braces. Some examples:
* `\frac` and `\binom`: `$$\frac{1}{\binom{52}{13}}$$` gives $$\frac{1}{\binom{52}{13}}$$
### Equation Arrays
We can create (semi)-aligned arrays of equations with the `\eqnarray` command:
```
\begin{eqnarray}
a & = & b + c \\
x & = & y - z
\end{eqnarray}
```
$$
\begin{eqnarray}
a & = & b + c \\
x & = & y - z
\end{eqnarray}
$$
The `&` acts as a column-separator within each row, setting the alignment; the `\\` denotes the end of a row.
### Explicit Bracket Sizes
Sometimes you want finer control over bracket sizes. This can be achieved with the "big" family of commands: `$$\big\{ \big\} \Big\{ \Big\} \bigg\{ \bigg\} \Bigg\{ \Bigg\}$$` renders as: $$\big\{ \big\} \Big\{ \Big\} \bigg\{ \bigg\} \Bigg\{ \Bigg\}$$
Stick whichever sort of bracket after the `\big` as you want; for example, you can make a really big pipe with `$\Bigg|$`: $\Bigg|$
## Additional Resources
* **Detexify**: Martin pointed out this great resource that allows you to draw a symbol which will be run through a classifier to guess the appropriate command: https://detexify.kirelabs.org/classify.html. There is also an [Android app on the Play Store](https://play.google.com/store/apps/details?id=website.marty.detexify&hl=en_US&gl=US).