MathJax (a relative of \$\LaTeX\$) is now enabled on this site. Mathematics.SE has a MathJax basic tutorial and quick reference, but this question will be a cookbook for Game Development-specific mathematics formatting. Many posts in here will repeat or summarise content from that quick reference.

Contributing to this cookbook

Please do so! Answers should each be a specific topic, or set of closely related topics. If in doubt, group them by how game development mathematicians would think of them. You can help in several ways:

  • Revise existing answers to add or improve details, including formatting that isn't covered yet in that topic but ought to be.
  • Expand an answer with new subsections covering tightly related topics that aren't covered yet.
  • Post new answers about new topics, and link them in the table of contents in a position you feel is appropriate.

Table of Contents

  1. Basic MathJax and Mathematics
  2. Vectors and Matrices
  3. Boolean Algebra and Logic
  4. Tables and arrays
  5. Tag and reference equations
  • 3
    I find this really useful, even when you know most of the markdown. – Bálint Dec 13 '17 at 14:40

Boolean Algebra and Logic

  1. Quantifiers. use \forall and \exists: \$ \forall\ and \ \exists\ \$
  2. Operators. use \neg, \land and \lor: \$ \neg, \land\ and \lor\ \$

Vectors and Matrices

Basic symbols

  • \vec puts an arrow over the next symbol: \$\vec a\$. For larger groups, use \overrightarrow: \$\overrightarrow{abc}\$

  • \overleftrightarrow and \overleftarrow are also available: \$ \overleftrightarrow{abc}\$ and \$\overleftarrow{abc}\$

  • \vert and \Vert display single and double vertical bars: \$\vert a \vert\$ or \$\Vert a \Vert\$.

    • Strictly speaking, use \lvert \rvert and \lVert \rVert to display on the left and right sides of symbols respectively: \$\lvert a \rvert\$ and \$\lVert a \rVert\$. It may display more correctly certain formula renders.
  • \cdot represents the centered dot for dot products: \$x \cdot y\$

Matrices

The \begin{matrix} ... \end{matrix} environment facilitates display of a matrix. Separate matrix elements with & and create new matrix lines with \\. So the following code:

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

produces

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

To surround the matrix in brackets of some form, either use the \left and \right brackets from section 6 of the basic mathjax tutorial, or use pmatrix \$ \begin{pmatrix} a & b \\ c & d \end{pmatrix} \$ , bmatrix \$ \begin{bmatrix} a & b \\ c & d \end{bmatrix} \$ , Bmatrix \$ \begin{Bmatrix} a & b \\ c & d \end{Bmatrix} \$ , vmatrix \$ \begin{vmatrix} a & b \\ c & d \end{vmatrix} \$ , or Vmatrix \$ \begin{Vmatrix} a & b \\ c & d \end{Vmatrix} \$.

You can use \cdots \$\cdots\$, \ddots \$\ddots\$, and \vdots \$\vdots\$ to omit entries.

$$ \begin{pmatrix} a & b & \cdots & e \\ c & d & \cdots & f \\ \vdots & \vdots & \ddots & g \\ h & i & j & k \end{pmatrix} $$

Vectors

Use a one-dimensional matrix. For example:

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

$$ \vec v = \begin{pmatrix} a \\ b \\ c \end{pmatrix} $$

Basic MathJax and Mathematics

Displaying a formula

For inline formulas, use \$ ... \$. For display-mode formulas (i.e. multiline, centered formulas which take up their own paragraph), use $$ ... $$. Various symbols will be displaye differently in inline vs multiline mode.

For example, the equation \sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6} renders in inline mode (\$) as the following: \$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}\$

Meanwhile in display mode ($$) it displays as: $$ \sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6} $$

(Note the $$ also breaks it out into its own lines and centers it.)

New lines: Display formulas can have multiple lines. Insert a line break with \\.

Grouping

MathJax operates on symbols or groups of symbols. Usually, a MathJax operator that's expecting to do something fancy with some symbols will grab just the very first symbol available and nothing more. For example, a^bc will be displayed as \$a^bc\$.

If we want to represent that as \$a\$ to the power of \$bc\$ we instead need to group these symbols using curly braces, i.e. { }. So we'd write a^{bc} instead: \$a^{bc}\$.

You can get literal curly braces by escaping them: \{foo\} → \$ \{foo\} \$

Basic mathematical formatting

  1. Mathematical operations. +, - (hyphen), \times and \div: \$1 + 2 - 3 \times 4 \div 5\$.
    • alternately \cdot for the multiplication dot, \$ x \cdot y \$
    • \pm \mp for \$\pm \mp\$
  2. Comparison.
    • \gt or \lt for \$\gt \lt\$
    • \ge or \geq for \$\ge\$, or \geqslant for \$\geqslant\$. Similarly for less-than-or-equal: \le or \leq, \leqslant: \$\le \leqslant\$
    • Approximately equal: \approx \sim \simeq give you \$\approx \sim \simeq\$
  3. Superscripts and subscripts. use ^ and _. These can be combined. For example, x_i^2 renders as \$x_i^2\$.
  4. Fractions. There are two options:
    • \frac a b grabs the next two groups: \$\frac{a+1}{b+1}\$
    • You may instead prefer to use \over to write {a+1 \over b+1}: \${a+1 \over b+1}\$ when the equations on either side are complex.
  5. Greek letters. Use \alpha, \beta, …, \omega: \$\alpha, \beta \ldots \omega\$. For uppercase, use \Gamma, \Delta, …, \Omega: \$\Gamma, \Delta, \ldots \Omega\$.
  6. Plain text in equations: Usually all text is treated as symbols, so these are some words gets rendered as \$these are some words\$ despite the spaces. To tell MathJax to treat it as just ordinary text, use \text{stuff}: \$\text{these are some words}\$.
  7. Floor and ceiling. \lfloor x \rfloor for \$\lfloor x \rfloor\$, \lceil x \rceil for \$\lceil x \rceil\$

Equation alignment

You can use the \begin{align} ... \end{align} environment to align equations over multiple lines. The & symbol is an alignment marker in this environment. Use \\ to start new lines. The following example aligns on the equals sign:

$$
\begin{align}
a^2 &= b^2 + c^2 \\
a &= \sqrt{b^2 + c^2}
\end{align}
$$

$$ \begin{align} a^2 &= b^2 + c^2 \\ a &= \sqrt{b^2 + c^2} \end{align} $$

Tables and arrays

The array environment can do tables quite well.

$$
\begin{array}{r|lcl}
\text{Column One} & \text{Two} & \text{Three} & \text{Four} \\
\hline
foo & bar & baz & narf  \\
tinker & tailor & soldier & spy
\end{array}
$$

$$ \begin{array}{r|lcl} \text{Column One} & \text{Two} & \text{Three} & \text{Four} \\ \hline foo & bar & baz & narf \\ tinker & tailor & soldier & spy \end{array} $$

There's several parts to this.

First, we begin the array (\begin{array}), then immediately make a column declaration: {r|lcl}. r, l, and c define column alignment: right-aligned, left-aligned, centered. | establishes a vertical line between two columns. You can have |'s anywhere, including at the beginning or end, but multiple |s in a row are redundant.

Then we write out our rows. & separates the cells in a row. \\ marks the end of the row.

We can insert a horizontal separator with \hline. This line doesn't need a \\ at the end of it.

We end the array with \end{array}.

Boxing in a table

You can use | and \hline to completely box in a table, by placing lines at the start and end as well as between cells as you'd like:

$$ \begin{array}{|c|c|} \hline a & b \\ \hline c & d \\ \hline \end{array} $$


Advanced usage: you can use \rlap to right-overlap a heading across several cells

$$ \begin{array}{r|lll} & \rlap{\text{number of foo}} \\ \text{number of bar} & 0 & 1 & 2 \\ \hline 0 & 0.125 & 0.250 & 0.168 \\ 1 & 0.125 & 0.250 & 0.168 \\ 2 & 0.125 & 0.250 & 0.168 \end{array} $$

Tag and reference equations

Tagging

If you have multiple equations in the same post, you may want to tag them for reference using \tag. For example, \tag{1} adds this floaty \$(1)\$ on the right hand side here:

$$ c^2 = a^2 + b^2 \tag{1} \label{eq1} $$

$$ c^2 = a^2 + b^2 \tag{1} $$

You can also use letters, whole words, and so on.

Usually a MathJax block can only contain one tag. The Align environment lets you add multiple tags should you want:

$$ \begin{align} c^2 &= a^2 + b^2 \tag 1 \\ c &= \sqrt{a^2 + b^2} \tag 2 \end{align} $$

$$
\begin{align}
c^2 &= a^2 + b^2 \tag 1 \\
c &= \sqrt{a^2 + b^2} \tag 2
\end{align}
$$

Referencing equations in code

Your primary use case for tags might be referenceing your equations in code examples, to show what mathematics you're implementing:

python code implementing the pythagorean theorem for a triangle, with an "#equation 2" comment to reference the above equation

Referencing equations via a MathJax link

If you want to reference (and link back to) the equation elsewhere in your post, add a \label as well. I'll call this one eq3:

$$ c = \sqrt{a^2 + b^2} \tag{3} \label{eq3} $$

$$ c = \sqrt{a^2 + b^2} \tag{3} \label{eq3} $$

Then reference it using \eqref{eq3}: \eqref{eq3}. Note that the link text is \$(3)\$ because that is the tag text. If I had given this equation \tag{foo} instead then the eqref text here woud be \$(\text{foo})\$. The purpose of this link is to scroll the particular equation into view, so here's a link back to the very first equation on this post, to which I sneakily added a label, for a better example: \eqref{eq1}.

Labels must be unique: two equations can't share the same label.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .