All documentation

How to write a matrix in LaTeX

A matrix in LaTeX is written with one of the matrix environments of the amsmath package: & separates the columns, \\ ends each row, and the name of the environment chooses the brackets. This page shows each of them with the source to copy, and how Texifine writes the skeleton for you.

A first matrix

A = \begin{pmatrix}
  1 & 2 \\
  3 & 4
\end{pmatrix}

Two rows, two columns, in parentheses. The line breaks and the spaces of the source are there for you to read it: LaTeX ignores them inside a formula.

Choosing the brackets

The six environments take the same content. Only the delimiters change.

EnvironmentDelimitersTypical use
pmatrixParenthesesA matrix, a vector
bmatrixSquare bracketsA matrix, in many engineering texts
BmatrixBracesA set written as a grid
vmatrixSingle barsA determinant
VmatrixDouble barsA norm
matrixNoneA grid you surround yourself

A determinant, with vmatrix:

\det(A) = \begin{vmatrix}
  a & b \\
  c & d
\end{vmatrix} = ad - bc

Vectors

A column vector is a matrix with one column, a row vector a matrix with one row:

\mathbf{v} = \begin{pmatrix} x \\ y \\ z \end{pmatrix}
\qquad
\mathbf{w} = \begin{pmatrix} x & y & z \end{pmatrix}

A matrix of any size: the dots

\cdots draws horizontal dots, \vdots vertical ones and \ddots diagonal ones.

A = \begin{pmatrix}
  a_{11} & a_{12} & \cdots & a_{1n} \\
  a_{21} & a_{22} & \cdots & a_{2n} \\
  \vdots & \vdots & \ddots & \vdots \\
  a_{m1} & a_{m2} & \cdots & a_{mn}
\end{pmatrix}

The identity and a product

I_3 = \begin{bmatrix}
  1 & 0 & 0 \\
  0 & 1 & 0 \\
  0 & 0 & 1
\end{bmatrix}

Matrices sit in a formula like any other symbol, so a product is written by placing them side by side:

\begin{pmatrix}
  1 & 2 \\
  3 & 4
\end{pmatrix}
\begin{pmatrix}
  x \\
  y
\end{pmatrix}
=
\begin{pmatrix}
  x + 2y \\
  3x + 4y
\end{pmatrix}

An augmented matrix

The matrix environments draw no line between columns. For the vertical bar of an augmented matrix, use array: its argument gives one letter per column (c for centred, l for left, r for right) and a | where a line goes. The parentheses are added with \left( and \right), which grow to the height of what they enclose.

\left(\begin{array}{cc|c}
  1 & 2 & 5 \\
  3 & 4 & 6
\end{array}\right)

A small matrix inside a sentence

A full-size matrix in the middle of a line pushes the lines apart. smallmatrix is made for that case; it has no delimiters of its own, so add them:

\bigl(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\bigr)

A function defined by cases

cases follows the same rules — & between the value and its condition, \\ between rows — and draws the brace:

f(x) = \begin{cases}
  x^2 & \text{if } x \geq 0 \\
  -x  & \text{otherwise}
\end{cases}

In Texifine: the matrix builder

You do not have to type the skeleton.

  1. Insert an Equation block, or open a formula inside a sentence (Ctrl+M).
  2. Click Matrix, next to the source.
  3. Choose the number of rows and columns and the delimiters, then Insert.

The matrix is written at the caret with empty cells, one row per line. Fill them in: the preview above the source shows the matrix as the PDF will print it. A matrix can sit anywhere in a formula — after A =, inside a product, in a fraction.

Texifine adds the environment around the formula (equation, align…) and loads amsmath itself: you type the formula only. See Equation.

Common mistakes

  • A missing \\. Two rows written without \\ between them are read as one long row. LaTeX reports nothing: the matrix simply comes out with a row missing and another one too long.
  • Rows of different lengths. A matrix environment accepts them without an error and leaves the missing cells empty. If a column looks shifted, count the & of each row: a row of n cells has n − 1 of them.
  • More than 10 columns. Standard LaTeX accepts 10 columns in a matrix environment and stops beyond that on "Extra alignment tab has been changed to \cr". Texifine raises the limit to 20.
  • An array row with too many cells. Unlike the matrix environments, array knows how many columns it has from its argument: one cell more than announced stops the compilation on the same "Extra alignment tab" error.
  • A matrix outside a formula. The environments only exist in maths mode. In Texifine, text is never read as LaTeX: write the matrix in an Equation block or in a formula inside the sentence.

When a document does not compile, the PDF tab shows the first error and its line. See Fixing compilation errors.

See also