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!
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.
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 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}\)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 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}$$
.
We use intersection and union a lot in this class! These two symbols, like many other math operators, have their own commands:
\cup
, like $A \cup B$
: \(A \cup B\)\cap
, like $A \cap B$
: \(A \cap B\)We also want to be able to donate membership.
\in
, like $a_i \in A$
: \(a_i \in A\)\notin
, like $x \notin A$
: \(x \notin A\)And of course, sub- and supersets:
\subseteq
, like $V \subseteq W$
: \(V \subseteq W\)\subset
, like $V \subset W$
: \(V \subset W\)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:
=
: $A = B$
gives \(A = B\)<
: $A < B$
gives \(A < B\)>
: $A > B$
gives \(A > B\)\leq
or \leq
: $A \leq B$
gives \(A \leq B\)\geq
or ge
: $A \geq B$
gives \(A \geq B\)\neq
: $A \neq B$
gives \(A \neq B\)The On-Line Encyplopedia of Integer Sequences has a nice chart of many additional unary and binary operators for your reference.
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)}\]
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\).
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}}\]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.
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|\)