MATRICES & ARRAYS

How to write matrices in LaTeX

Examples checked with MathTexLab’s base and AMS math renderer · Display style

A LaTeX matrix has an environment around its entries, & between columns and \\ between rows. Choose pmatrix for parentheses, bmatrix for square brackets or vmatrix for determinant bars.

Understand rows and columns first

A plain matrix lays out the entries without adding surrounding delimiters. The first row below is 1 & 2. Two backslashes finish that row, and 3 & 4 forms the second row.

Entries without surrounding delimiters
\begin{matrix} 1 & 2 \\ 3 & 4 \end{matrix}
Open example in editor

For a regular rectangular matrix, use the same number of entries in each row. A row with three entries needs two column separators; a row with two entries needs one. A newline in your source improves readability but does not replace the \\ row separator.

MathTexLab includes the relevant AMS mathematics support. In a separate full LaTeX document, matrix environments generally come from amsmath. Don’t paste a document preamble into MathTexLab’s math-only editor.

Choose parentheses or square brackets

The entries and separators stay the same when you change the environment. The p in pmatrix selects parentheses; bmatrix selects square brackets. The environment name after \begin must match the one after \end.

A two-by-two matrix in parentheses
A=\begin{pmatrix} a & b \\ c & d \end{pmatrix}
Open example in editor

A matrix does not have to be square. This example has two rows and three columns; changing its shape does not require changing the bmatrix environment.

A rectangular matrix in square brackets
B=\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}
Open example in editor

The Matrices toolbar supplies starting templates. You can add or remove entries and rows directly in the source editor instead of looking for a separate button for every possible matrix dimension.

Use bars for determinant notation

vmatrix adds single vertical bars. The familiar two-by-two determinant formula is shown here as a mathematical identity. MathTexLab displays the formula; it does not compute a determinant for arbitrary entries.

Determinant notation
\begin{vmatrix} a & b \\ c & d \end{vmatrix}=ad-bc
Open example in editor

Determinants are defined for square matrices. Using bars around a rectangular array does not make it a valid determinant, even if the notation can be typeset.

Add a third row and column

Each row in this three-by-three identity matrix contains three entries. The diagonal entries are one and the remaining entries are zero. There is no need to declare the dimensions separately.

A three-by-three identity matrix
I_3=\begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix}
Open example in editor

For a larger symbolic pattern, \cdots, \vdots and \ddots supply horizontal, vertical and diagonal dots. Keep the number of explicit columns consistent when adding them. Wide matrices may need horizontal scrolling in the preview.

Add a column separator with array

Use array when you need an explicit vertical separator. The specification {cc|c} means two centered columns, a vertical line, then one more centered column. The outer \left[ and \right] provide square brackets.

An augmented matrix using an array
\left[\begin{array}{cc|c} 1 & 2 & 3 \\ 4 & 5 & 6 \end{array}\right]
Open example in editor

This is a formatting example for an augmented matrix, not a solved linear system. Do not add {cc|c} to pmatrix; that environment does not use an array column specification in the same way.

Check these points when a matrix looks wrong

  • Use & only to separate columns, not as a literal word or punctuation mark.
  • Use two backslashes between rows. A single backslash followed by letters starts a command.
  • Match \begin{bmatrix} with \end{bmatrix}, not another environment name.
  • Keep fractions and other multi-part entries inside their own groups. Fraction grouping is useful inside matrix cells too.
  • Do not add packages or custom matrix commands from another editor. MathTexLab supports a defined subset of mathematical TeX.

After checking the rows, use the export guide to choose a format for your finished matrix.

Syntax references

Overleaf’s matrix documentation describes the AMS environments in full-document LaTeX. The MathJax command index records extension requirements. MathTexLab’s own renderer is used to check every runnable example on this page.