Writing Equations

How to write mathematical equations in LaTeX using inline math, display math, and the amsmath package.

LaTeX is universally regarded as the best system for typesetting mathematics. It provides incredible precision and flexibility for even the most complex equations.

Inline Math

To write math within a sentence, wrap the mathematical expression in single dollar signs $ ... $.

The Pythagorean theorem states that $a^2 + b^2 = c^2$.

Display Math

If you want an equation to be centered on its own line, use double dollar signs $$ ... $$ or the \[ ... \] environment.

The roots of a quadratic equation are given by:
\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]

Numbered Equations

For academic papers, you often need to number your equations so you can refer to them later. Use the equation environment for this.

\begin{equation}
E = mc^2 \label{eq:einstein}
\end{equation}
 
As seen in Equation \ref{eq:einstein}, mass and energy are equivalent.

The Align Environment

When you have a series of equations or derivations, you usually want to align them at the equals sign. For this, you need the amsmath package and the align environment.

Use the ampersand & to specify the alignment point, and double backslash \\ for a new line.

\begin{align*}
f(x) &= (x+2)^2 - 4 \\
     &= (x^2 + 4x + 4) - 4 \\
     &= x^2 + 4x
\end{align*}
The Asterisk

Adding an asterisk * to \begin{align*} prevents LaTeX from automatically numbering every line of the equation block.