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}
    a b c d

    No brackets.

  • \begin{pmatrix} a & b \\ c & d \end{pmatrix}
    ( a b c d )

    Parentheses ( ).

  • \begin{bmatrix} a & b \\ c & d \end{bmatrix}
    [ a b c d ]

    Square brackets [ ].

  • \begin{Bmatrix} a & b \\ c & d \end{Bmatrix}
    { a b c d }

    Curly braces { }.

  • \begin{vmatrix} a & b \\ c & d \end{vmatrix}
    | a b c d |

    Single bars: a determinant.

  • \begin{Vmatrix} a & b \\ c & d \end{Vmatrix}
    ‖ a b c d ‖

    Double bars: a matrix norm.

  • \left(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right)
    ( a b c d )

    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.

Templates

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

    ( a b c d )
    LaTeX
    \begin{pmatrix} a & b \\ c & d \end{pmatrix}
  • 3×3 identity matrix

    I 3 = [ 1 0 0 0 1 0 0 0 1 ]
    LaTeX
    I_3 = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}
  • Determinant

    det ( A ) = | a b c d | = a d − b c
    LaTeX
    \det(A) = \begin{vmatrix} a & b \\ c & d \end{vmatrix} = ad - bc
  • n×n matrix with dots

    A = ( a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a m 1 a m 2 ⋯ a m n )
    LaTeX
    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}

    \cdots, \vdots and \ddots fill the gaps horizontally, vertically and diagonally.

  • Augmented matrix

    [ 1 2 5 3 4 6 ]
    LaTeX
    \left[\begin{array}{cc|c} 1 & 2 & 5 \\ 3 & 4 & 6 \end{array}\right]

    Use array when you need a vertical line: {cc|c} means two centered columns, a rule, one more column.

  • Column vector

    x = [ x 1 x 2 x 3 ]
    LaTeX
    \mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}
  • Matrix product

    [ 1 2 3 4 ] [ x y ] = [ x + 2 y 3 x + 4 y ]
    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

    [ 1 − 2 − 30 4 ]
    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.

LaTeX
\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, smallmatrix and array without 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: pmatrix draws round parentheses, bmatrix square 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}. Vmatrix gives double bars for a norm.
How do I add a vertical line in a matrix?
Switch to array and 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 bmatrix or pmatrix code with a preview you can correct.

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