How-to

How to Write a System of Equations in LaTeX

The fix

To write a system of equations in LaTeX, load \usepackage{amsmath}. For one left brace around the equations, put them in a cases environment inside \[ ... \]; for separately numbered lines aligned at the equals sign, use align. End each equation with \\ and mark the alignment point with &. To line up x, y and z in columns, use the systeme package.

Not sure it worked on your document? Paste it into LetX and compile it, free and in the browser.

Why it happens

LaTeX has no single "system" command; you choose an environment by what the reader needs to see. cases gives the brace. align lines up the equals signs and numbers each line. systeme and alignat also line up the individual variables, which matters in a linear system with three or more unknowns, where the reader compares coefficients down each column.

Example

\usepackage{amsmath}
% Braced system:
\[
\begin{cases}
  x + y = 2 \\
  x - y = 0
\end{cases}
\]
% Aligned and numbered:
\begin{align}
  2x + y &= 5 \\
  x - 3y &= -2
\end{align}

Lining up the variables: systeme or alignat

With three or more unknowns, readers expect the x terms, the y terms and the z terms in columns, with a gap where a variable is missing. The systeme package does this automatically: list the equations separated by commas, and it aligns the terms, the signs and the equals signs and adds the left brace.

With only amsmath, alignat* gives the same layout by hand. Every term and every sign gets its own column. Write a sign that sits alone in a cell as {}+{}, so LaTeX still spaces it as a binary operator instead of a cramped unary sign.

\usepackage{amsmath}
\usepackage{systeme}
% in the document:
\[
\systeme{2x + 3y - z = 1, x - y + 4z = 6, 3x + 2z = 4}
\]

\begin{alignat*}{4}
  2x &{}+{}& 3y &{}-{}&  z &{}={}& 1 \\
   x &{}-{}&  y &{}+{}& 4z &{}={}& 6 \\
  3x &     &    &{}+{}& 2z &{}={}& 4
\end{alignat*}

A brace and a number on each equation

cases inside an equation environment gives the whole system one number, and align numbers every line but draws no brace. To have both, use empheq, which loads mathtools and amsmath. Its left option adds a brace to any amsmath environment, and each line keeps its own number and \label. numcases from the cases package is the other way to do it.

\usepackage{empheq}
% in the document:
\begin{empheq}[left=\empheqlbrace]{align}
  x + y &= 2 \label{eq:sum} \\
  x - y &= 0 \label{eq:diff}
\end{empheq}
Adding \eqref{eq:sum} and \eqref{eq:diff} gives $x = 1$.

Matrix form and augmented matrices

A linear system is often written as Ax = b. bmatrix (amsmath) draws square brackets and pmatrix round ones; columns are separated by & and rows by \\. For an augmented matrix with a vertical bar before the constants, use an array with | in its column specification, inside \left[ ... \right].

\usepackage{amsmath}
% in the document:
\[
\begin{bmatrix} 2 & 3 & -1 \\ 1 & -1 & 4 \\ 3 & 0 & 2 \end{bmatrix}
\begin{bmatrix} x \\ y \\ z \end{bmatrix}
=
\begin{bmatrix} 1 \\ 6 \\ 4 \end{bmatrix}
\qquad
\left[\begin{array}{rrr|r}
  2 &  3 & -1 & 1 \\
  1 & -1 &  4 & 6 \\
  3 &  0 &  2 & 4
\end{array}\right]
\]

Common errors

"Misplaced alignment tab character &": an & outside an alignment environment, for example in \[ ... \] without cases or aligned around it, or anywhere when amsmath is not loaded.

"Extra alignment tab has been changed to \cr": too many & on one row. cases has exactly two columns, so a second & in one row triggers it.

"Paragraph ended before \align was complete": a blank line inside align. Delete it, or put a lone % on that line.

"Missing $ inserted": cases or bmatrix used outside math mode. Wrap it in \[ ... \] or an equation environment.

Frequently asked questions

How do I get a left brace around the system?

Use the cases environment from amsmath, or \systeme{...}, which adds the brace for you. For a custom brace, put an array inside \left\{ ... \right.

Should I use align or cases?

Use cases for one braced group presented as a set; use align when each equation should sit on its own line, aligned at = and individually numbered.

How do I align the x, y and z terms in columns?

Use \systeme{...} from the systeme package, which aligns the terms automatically, or alignat* from amsmath with one column for each term and each sign.

How do I number each equation in a braced system?

Use empheq: \begin{empheq}[left=\empheqlbrace]{align} ... \end{empheq}. Each line keeps its own number and can carry its own \label.

How do I write a system of equations in matrix form?

Write the coefficient matrix and the vectors with bmatrix, as in A x = b. For an augmented matrix, use an array with columns {rrr|r} inside \left[ ... \right].

Related LaTeX fixes

Browse all LaTeX problems & fixes, or start with the LaTeX guide.

Learn the topic properly

Try the fix in LetX

Open the editor, paste your code, and compile in seconds to see the error clear. Free, in your browser.

Open LetX Free