Math Example

Use LaTeX notation in Markdown for maths. The wiki renders it with KaTeX.

Inline and display

Use single dollar signs for inline maths, such as O(nlog⁡n)O(n \log n):

merge sort runs in $O(n \log n)$ time

Use double dollar signs for a display equation:

$$
\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
$$

Rendered:

−b±b2−4ac2a\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

Display equations also support sums:

∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2}

Notation that comes up in computer science

An asymptotic bound:

f(n)=O(g(n))  ⟺  ∃ c>0, n0∈N  such that  f(n)≤c g(n)  for all n≥n0f(n) = O(g(n)) \iff \exists\, c > 0,\ n_0 \in \mathbb{N} \ \text{ such that } \ f(n) \le c\,g(n) \ \text{ for all } n \ge n_0

A geometric sum:

∑i=0n−12i=2n−1\sum_{i=0}^{n-1} 2^i = 2^n - 1

A harmonic number:

Hn=∑k=1n1k≈ln⁡n+γH_n = \sum_{k=1}^{n} \frac{1}{k} \approx \ln n + \gamma

Multi-line derivations use the aligned environment, with & marking the alignment point and \\ ending each line:

T(n)=2 T(n/2)+Θ(n)=4 T(n/4)+2 Θ(n)=…=Θ(nlog⁡n)\begin{aligned} T(n) &= 2\,T(n/2) + \Theta(n) \\ &= 4\,T(n/4) + 2\,\Theta(n) \\ &= \dots \\ &= \Theta(n \log n) \end{aligned}

Matrices and cases work too:

A=(1011)sgn(x)={1if x>00if x=0−1if x<0A = \begin{pmatrix} 1 & 0 \\ 1 & 1 \end{pmatrix} \qquad \mathrm{sgn}(x) = \begin{cases} 1 & \text{if } x > 0 \\ 0 & \text{if } x = 0 \\ -1 & \text{if } x < 0 \end{cases}

Dollar signs in code

Dollar signs inside code blocks and inline code remain literal:

eval $(opam env)
echo "${HOME}/bin"
The area of a circle is $\pi r^2$.

And inline: $HOME, $(pwd), $1.

Everything else KaTeX supports

Check the KaTeX supported functions if a command does not render, and preview the page before publishing.