How-to

How to Write a Matrix in LaTeX

The fix

To write a matrix in LaTeX, load \usepackage{amsmath} and use a matrix environment inside math mode: pmatrix for round brackets, bmatrix for square brackets, vmatrix for determinant bars. Separate column entries with & and end each row with \\.

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

Example

\usepackage{amsmath}
\[
\begin{bmatrix}
  1 & 2 & 3 \\
  4 & 5 & 6
\end{bmatrix}
\quad
\begin{pmatrix} a \\ b \end{pmatrix}
\]

Frequently asked questions

What is the difference between pmatrix and bmatrix?

pmatrix draws round parentheses ( ), bmatrix draws square brackets [ ], vmatrix draws single bars for a determinant, and Vmatrix draws double bars. All come from amsmath.

How do I add dots inside a matrix?

Use \cdots (horizontal), \vdots (vertical), and \ddots (diagonal): e.g. a_{11} & \cdots & a_{1n} on the top row and \ddots down the diagonal.

How do I control column alignment in a matrix?

Plain pmatrix/bmatrix centre entries. For custom alignment use the array environment inside \left( ... \right), or pmatrix* / bmatrix* from mathtools with an optional alignment argument.

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