Matrix in LaTeX
Write a matrix in math mode with an amsmath environment: \begin{bmatrix} a & b \\ c & d \end{bmatrix}. & separates columns, \\ ends a row, and the environment name picks the brackets: pmatrix for ( ), bmatrix for [ ], vmatrix for a determinant | |.
Matrix environments
\begin{matrix} a & b \\ c & d \end{matrix}No brackets.
\begin{pmatrix} a & b \\ c & d \end{pmatrix}Parentheses ( ).
\begin{bmatrix} a & b \\ c & d \end{bmatrix}Square brackets [ ].
\begin{Bmatrix} a & b \\ c & d \end{Bmatrix}Curly braces { }.
\begin{vmatrix} a & b \\ c & d \end{vmatrix}Single bars: a determinant.
\begin{Vmatrix} a & b \\ c & d \end{Vmatrix}Double bars: a matrix norm.
\left(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right)A small matrix that fits inside a line of text; add the brackets yourself.
Try it live
Change the formula and the preview redraws as you type. Copy the LaTeX, download a PNG or SVG, copy it for Word, or use Edit here on any example below to load it.
Matrix examples
Each result below is drawn from the LaTeX under it. Copy the code, edit it on this page, or open it in the full editor.
2×2 matrix
LaTeX\begin{pmatrix} a & b \\ c & d \end{pmatrix}3×3 identity matrix
LaTeXI_3 = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}Determinant
LaTeX\det(A) = \begin{vmatrix} a & b \\ c & d \end{vmatrix} = ad - bcn×n matrix with dots
LaTeXA = \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}\cdots,\vdotsand\ddotsfill the gaps horizontally, vertically and diagonally.Augmented matrix
LaTeX\left[\begin{array}{cc|c} 1 & 2 & 5 \\ 3 & 4 & 6 \end{array}\right]Use
arraywhen you need a vertical line:{cc|c}means two centered columns, a rule, one more column.Column vector
LaTeX\mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}Matrix product
LaTeX\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} x + 2y \\ 3x + 4y \end{bmatrix}Right-aligned entries
LaTeX\begin{bmatrix*}[r] 1 & -2 \\ -30 & 4 \end{bmatrix*}The starred environments come from
mathtools;[r]right-aligns each column so minus signs line up.
How to write a matrix step by step
Load amsmath in the preamble with \usepackage{amsmath} (Overleaf templates usually already do). The matrix environments only work in math mode, so put the matrix inside \[ … \], $ … $ or an equation environment.
Type the entries row by row. Put & between entries in the same row and \\ at the end of every row except the last. Every row should have the same number of &; a missing one leaves a gap instead of an error, so check the shape in the preview.
Choose the environment for the bracket style. Brackets made by pmatrix or bmatrix stretch to the height of the matrix automatically, which ( typed by hand does not.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
A = \begin{bmatrix}
1 & 2 \\
3 & 4
\end{bmatrix}
\]
\end{document}Matrix or array?
The matrix family centers every column and adds brackets for you. array takes a column specification such as {lcr} or {cc|c}, so it is the one to use for left or right alignment, vertical rules (augmented matrices) or mixed alignment. Wrap it in \left[ … \right] for brackets.
For right-aligned numbers without array, mathtools provides bmatrix* and pmatrix* with an optional [l], [c] or [r] argument.
Inline matrices
A full-size matrix inside a sentence pushes the lines apart. Use smallmatrix with brackets you add yourself, for example \left(\begin{smallmatrix} 1 & 0 \\ 0 & 1 \end{smallmatrix}\right), or move the matrix to a displayed equation.
Common mistakes
A single backslash is a spacing command, not a new row. Rows end with two backslashes.
Instead of\begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix}Write\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}The matrix environments are math-mode only. In running text LaTeX stops with "Missing $ inserted".
Instead of\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} (outside $ … $)Write\[ \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \]Typed parentheses stay one line tall.
pmatrix(or\left( … \right)) stretches them to fit.Instead of( \begin{matrix} 1 & 2 \\ 3 & 4 \end{matrix} )Write\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}amsmath matrices allow 10 columns by default. More columns raise an "Extra alignment tab" error until you raise the counter.
Instead of\begin{bmatrix} a & b & c & d & e & f & g & h & i & j & k \end{bmatrix}Write\setcounter{MaxMatrixCols}{20} % in the preamble
In Overleaf, MathJax and Word
Overleaf and pdfLaTeX
Works as shown once
\usepackage{amsmath}is in the preamble;bmatrix*also needs\usepackage{mathtools}. The 10-column limit applies here.MathJax and KaTeX
MathJax and KaTeX support
matrix,pmatrix,bmatrix,Bmatrix,vmatrix,Vmatrix,smallmatrixandarraywithout loading anything, and have no 10-column limit. Markdown editors that use them (Obsidian, Jupyter, GitHub) accept the same code between$$ … $$.Microsoft Word
Word does not read amsmath environments reliably through paste. Build the matrix in the editor on this page and use Copy for Word: it pastes a native Word equation whose brackets and cells you can keep editing.
Matrix in LaTeX: questions
- How do I type a 3×3 matrix in LaTeX?
- Write three rows of three entries:
\begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix}. Two&per row,\\after the first and second rows. - What is the difference between pmatrix and bmatrix?
- Only the brackets:
pmatrixdraws round parentheses,bmatrixsquare brackets. Both come from amsmath and both stretch to the matrix height. - How do I write a determinant?
- Use
vmatrix, which draws single vertical bars:\begin{vmatrix} a & b \\ c & d \end{vmatrix}.Vmatrixgives double bars for a norm. - How do I add a vertical line in a matrix?
- Switch to
arrayand put|in the column specification:\left[\begin{array}{cc|c} 1 & 2 & 5 \\ 3 & 4 & 6 \end{array}\right]. This is the usual way to write an augmented matrix. - Can I turn a picture of a matrix into LaTeX?
- Yes. Paste a screenshot or photo into the image to LaTeX converter on the home page; it returns the matrix as
bmatrixorpmatrixcode with a preview you can correct.
Related LaTeX reference
- Vector in LaTeX\vec{v} and \overrightarrow{AB}, bold vectors with \mathbf and \boldsymbol, unit vectors, column and row vectors, and norms.
- Transpose Symbol in LaTeXA^T, A^\top, A^\intercal and A^\mathsf{T} compared, plus inverse transpose and conjugate transpose.
- How to Write a Multiline Equation in LaTeXalign and align* for aligned lines, aligned inside $$ … $$, split, gather and multline for long formulas and systems of equations.
- LaTeX equation editorSymbol panel, live preview, and PNG, SVG, MathML or Word export.
- All LaTeX reference pagesSymbols, math structures and text formatting in one list.
Have the formula as a picture?
Paste a screenshot or photo of the equation and get its LaTeX, ready to copy or open in the editor.
Convert an image to LaTeX