Octree LogoOctree

Matrices in LaTeX

Matrices are fundamental structures in linear algebra, physics, computer science, and engineering. LaTeX's amsmath package provides a comprehensive set of environments for typesetting matrices of every kind — from simple undelimited grids to determinants, augmented matrices, and block matrices. This guide covers every matrix environment you will need, with practical examples you can copy directly into your documents.

Basic Matrix with amsmath

The most basic matrix environment is matrix, provided by theamsmath package. Entries in each row are separated by &, and rows are terminated by \\. The environment must be placed inside a math environment such as \[...\] or equation.

\usepackage{amsmath}

\[
\begin{matrix}
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9
\end{matrix}
\]

This produces a plain grid of numbers with no surrounding brackets or delimiters. Each column is automatically centred. You do not need to specify the number of columns — LaTeX infers it from the content.

% A 2x2 matrix
\[
\begin{matrix}
  a & b \\
  c & d
\end{matrix}
\]

% A 1x3 row vector
\[
\begin{matrix}
  x_1 & x_2 & x_3
\end{matrix}
\]

Matrix Types

The amsmath package provides six matrix environments, each producing different surrounding delimiters. All share the same internal syntax — only the brackets differ:

EnvironmentBracket StyleTypical Use
matrixNonePlain grid, manual delimiters
pmatrixParentheses ( )General-purpose matrices, vectors
bmatrixSquare brackets [ ]Linear algebra, systems of equations
BmatrixCurly braces { }Set notation, special contexts
vmatrixVertical lines | |Determinants
VmatrixDouble vertical lines || ||Norms, double-bar delimiters
\usepackage{amsmath}

% All six matrix types with the same content
\[
\begin{matrix} a & b \\ c & d \end{matrix} \quad
\begin{pmatrix} a & b \\ c & d \end{pmatrix} \quad
\begin{bmatrix} a & b \\ c & d \end{bmatrix}
\]

\[
\begin{Bmatrix} a & b \\ c & d \end{Bmatrix} \quad
\begin{vmatrix} a & b \\ c & d \end{vmatrix} \quad
\begin{Vmatrix} a & b \\ c & d \end{Vmatrix}
\]

Parenthesized Matrices (pmatrix)

The pmatrix environment is perhaps the most widely used matrix type. It wraps the matrix in round parentheses and is the standard choice for vectors, transformation matrices, and general-purpose linear algebra notation.

% A 3x3 matrix in parentheses
\[
A = \begin{pmatrix}
  1 & 0 & 0 \\
  0 & 1 & 0 \\
  0 & 0 & 1
\end{pmatrix}
\]

% A column vector
\[
\mathbf{v} = \begin{pmatrix}
  x \\
  y \\
  z
\end{pmatrix}
\]

% A row vector
\[
\mathbf{w}^T = \begin{pmatrix}
  w_1 & w_2 & w_3
\end{pmatrix}
\]

% Matrix with fractions
\[
R = \begin{pmatrix}
  \cos\theta & -\sin\theta \\
  \sin\theta & \cos\theta
\end{pmatrix}
\]

Bracketed Matrices (bmatrix)

The bmatrix environment surrounds the matrix with square brackets. This style is standard in many linear algebra textbooks and is commonly used for coefficient matrices and augmented matrices.

% A 3x3 matrix in square brackets
\[
A = \begin{bmatrix}
  2 & -1 & 0 \\
  -1 & 2 & -1 \\
  0 & -1 & 2
\end{bmatrix}
\]

% The zero vector
\[
\mathbf{0} = \begin{bmatrix}
  0 \\
  0 \\
  0
\end{bmatrix}
\]

% A matrix equation
\[
\begin{bmatrix}
  a_{11} & a_{12} \\
  a_{21} & a_{22}
\end{bmatrix}
\begin{bmatrix}
  x_1 \\
  x_2
\end{bmatrix}
=
\begin{bmatrix}
  b_1 \\
  b_2
\end{bmatrix}
\]

Determinants (vmatrix)

The vmatrix environment uses vertical bars as delimiters, which is the standard notation for determinants. The Vmatrix environment uses double vertical bars, typically for matrix norms.

% 2x2 determinant
\[
\det(A) = \begin{vmatrix}
  a & b \\
  c & d
\end{vmatrix}
= ad - bc
\]

% 3x3 determinant
\[
\begin{vmatrix}
  a_{11} & a_{12} & a_{13} \\
  a_{21} & a_{22} & a_{23} \\
  a_{31} & a_{32} & a_{33}
\end{vmatrix}
\]

% Matrix norm with Vmatrix
\[
\begin{Vmatrix}
  a & b \\
  c & d
\end{Vmatrix}
\]

% Determinant used in Cramer's rule
\[
x = \frac{
  \begin{vmatrix} b_1 & a_{12} \\ b_2 & a_{22} \end{vmatrix}
}{
  \begin{vmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{vmatrix}
}
\]

Small Matrices

The smallmatrix environment produces a compact matrix suitable for inline use within running text. It must be wrapped in delimiter commands manually since it does not add brackets on its own.

% Inline small matrix with parentheses
The rotation matrix
$\left(\begin{smallmatrix} \cos\theta & -\sin\theta \\
\sin\theta & \cos\theta \end{smallmatrix}\right)$
rotates a vector by $\theta$ radians.

% Inline small matrix with square brackets
The matrix
$\left[\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right]$
has determinant $ad - bc$.

% Multiple inline matrices
If $A = \left(\begin{smallmatrix} 1 & 2 \\ 3 & 4 \end{smallmatrix}\right)$
and $B = \left(\begin{smallmatrix} 5 & 6 \\ 7 & 8 \end{smallmatrix}\right)$,
then $A + B = \left(\begin{smallmatrix} 6 & 8 \\ 10 & 12 \end{smallmatrix}\right)$.

Augmented Matrices

Augmented matrices — used to represent systems of linear equations — include a vertical line separating the coefficient matrix from the constants column. LaTeX does not have a built-in augmented matrix environment, but you can create one using the array environment with a vertical bar in the column specification.

% Augmented matrix using array
\[
\left[\begin{array}{ccc|c}
  1 & 0 & 0 & 3 \\
  0 & 1 & 0 & 5 \\
  0 & 0 & 1 & -2
\end{array}\right]
\]

% A system of equations in augmented form
\[
\left[\begin{array}{cc|c}
  2 & 1 & 8 \\
  1 & 3 & 13
\end{array}\right]
\xrightarrow{R_1 \leftrightarrow R_2}
\left[\begin{array}{cc|c}
  1 & 3 & 13 \\
  2 & 1 & 8
\end{array}\right]
\]

% Row echelon form
\[
\left[\begin{array}{ccc|c}
  1 & 2 & -1 & 3 \\
  0 & 1 & 3 & 7 \\
  0 & 0 & 1 & 2
\end{array}\right]
\]

You can also define a reusable augmented matrix command in your preamble to avoid repeating the array setup:

% Define a reusable augmented bmatrix environment
\newenvironment{amatrix}[1]{%
  \left[\begin{array}{@{}*{#1}{c}|c@{}}
}{%
  \end{array}\right]
}

% Usage: \begin{amatrix}{number of coefficient columns}
\[
\begin{amatrix}{3}
  1 & 2 & 3 & 4 \\
  5 & 6 & 7 & 8 \\
  9 & 10 & 11 & 12
\end{amatrix}
\]

Block Matrices

Block matrices are large matrices composed of submatrix blocks. In LaTeX, you can typeset these using nested matrix environments or by using spacing and horizontal/vertical lines to indicate the block structure.

% Block matrix with visual separation
\[
M = \left[\begin{array}{cc|cc}
  A_{11} & A_{12} & B_{11} & B_{12} \\
  A_{21} & A_{22} & B_{21} & B_{22} \\
  \hline
  C_{11} & C_{12} & D_{11} & D_{12} \\
  C_{21} & C_{22} & D_{21} & D_{22}
\end{array}\right]
\]

% Block diagonal matrix
\[
\begin{bmatrix}
  A & 0 \\
  0 & B
\end{bmatrix}
=
\begin{bmatrix}
  a_{11} & a_{12} & 0 & 0 \\
  a_{21} & a_{22} & 0 & 0 \\
  0 & 0 & b_{11} & b_{12} \\
  0 & 0 & b_{21} & b_{22}
\end{bmatrix}
\]

% Block matrix notation
\[
M = \begin{pmatrix}
  A & B \\
  C & D
\end{pmatrix}
\quad \text{where } A, B, C, D \text{ are submatrices}
\]

Special Matrices

Certain matrices appear frequently in mathematics and have standard notations. LaTeX provides the dot commands \ddots (diagonal dots),\vdots (vertical dots), and \cdots (centred horizontal dots) for indicating patterns in large matrices.

Identity Matrix

% 3x3 identity matrix
\[
I_3 = \begin{pmatrix}
  1 & 0 & 0 \\
  0 & 1 & 0 \\
  0 & 0 & 1
\end{pmatrix}
\]

% General n x n identity matrix
\[
I_n = \begin{pmatrix}
  1 & 0 & \cdots & 0 \\
  0 & 1 & \cdots & 0 \\
  \vdots & \vdots & \ddots & \vdots \\
  0 & 0 & \cdots & 1
\end{pmatrix}
\]

Zero Matrix

% General zero matrix
\[
O_{m \times n} = \begin{pmatrix}
  0 & 0 & \cdots & 0 \\
  0 & 0 & \cdots & 0 \\
  \vdots & \vdots & \ddots & \vdots \\
  0 & 0 & \cdots & 0
\end{pmatrix}
\]

Diagonal Matrix

% Diagonal matrix
\[
D = \begin{pmatrix}
  d_1 & 0 & \cdots & 0 \\
  0 & d_2 & \cdots & 0 \\
  \vdots & \vdots & \ddots & \vdots \\
  0 & 0 & \cdots & d_n
\end{pmatrix}
\]

% Using \operatorname{diag}
\[
D = \operatorname{diag}(d_1, d_2, \ldots, d_n)
\]

Symmetric and Triangular Matrices

% Upper triangular matrix
\[
U = \begin{pmatrix}
  u_{11} & u_{12} & u_{13} & \cdots & u_{1n} \\
  0 & u_{22} & u_{23} & \cdots & u_{2n} \\
  0 & 0 & u_{33} & \cdots & u_{3n} \\
  \vdots & \vdots & \vdots & \ddots & \vdots \\
  0 & 0 & 0 & \cdots & u_{nn}
\end{pmatrix}
\]

% Lower triangular matrix
\[
L = \begin{pmatrix}
  l_{11} & 0 & 0 & \cdots & 0 \\
  l_{21} & l_{22} & 0 & \cdots & 0 \\
  l_{31} & l_{32} & l_{33} & \cdots & 0 \\
  \vdots & \vdots & \vdots & \ddots & \vdots \\
  l_{n1} & l_{n2} & l_{n3} & \cdots & l_{nn}
\end{pmatrix}
\]

Matrix Operations

LaTeX can elegantly display matrix operations including multiplication, transposition, and inversion. Here are common examples of how to typeset these operations.

Matrix Multiplication

% Matrix multiplication
\[
\begin{pmatrix}
  a & b \\
  c & d
\end{pmatrix}
\begin{pmatrix}
  e & f \\
  g & h
\end{pmatrix}
=
\begin{pmatrix}
  ae + bg & af + bh \\
  ce + dg & cf + dh
\end{pmatrix}
\]

% Matrix-vector multiplication
\[
\begin{bmatrix}
  1 & 2 \\
  3 & 4
\end{bmatrix}
\begin{bmatrix}
  x \\
  y
\end{bmatrix}
=
\begin{bmatrix}
  x + 2y \\
  3x + 4y
\end{bmatrix}
\]

Transpose

% Transpose notation
\[
A^T = \begin{pmatrix}
  1 & 2 \\
  3 & 4 \\
  5 & 6
\end{pmatrix}^T
=
\begin{pmatrix}
  1 & 3 & 5 \\
  2 & 4 & 6
\end{pmatrix}
\]

% Transpose properties
\[
(AB)^T = B^T A^T
\]

Inverse

% 2x2 inverse formula
\[
A^{-1} = \frac{1}{\det(A)}
\begin{pmatrix}
  d & -b \\
  -c & a
\end{pmatrix}
= \frac{1}{ad - bc}
\begin{pmatrix}
  d & -b \\
  -c & a
\end{pmatrix}
\]

% Inverse properties
\[
A A^{-1} = A^{-1} A = I
\]

\[
(AB)^{-1} = B^{-1} A^{-1}
\]

Systems of Linear Equations

Matrices are the natural way to represent systems of linear equations in the compact form Ax = b. LaTeX can display both the expanded system and its matrix form side by side.

% System of equations in matrix form
\[
\underbrace{
  \begin{bmatrix}
    a_{11} & a_{12} & a_{13} \\
    a_{21} & a_{22} & a_{23} \\
    a_{31} & a_{32} & a_{33}
  \end{bmatrix}
}_{A}
\underbrace{
  \begin{bmatrix}
    x_1 \\
    x_2 \\
    x_3
  \end{bmatrix}
}_{\mathbf{x}}
=
\underbrace{
  \begin{bmatrix}
    b_1 \\
    b_2 \\
    b_3
  \end{bmatrix}
}_{\mathbf{b}}
\]
% Concrete example
\[
\begin{bmatrix}
  2 & 1 & -1 \\
  -3 & -1 & 2 \\
  -2 & 1 & 2
\end{bmatrix}
\begin{bmatrix}
  x \\
  y \\
  z
\end{bmatrix}
=
\begin{bmatrix}
  8 \\
  -11 \\
  -3
\end{bmatrix}
\]
% Expanded system alongside matrix form
\[
\begin{cases}
  2x + y - z = 8 \\
  -3x - y + 2z = -11 \\
  -2x + y + 2z = -3
\end{cases}
\quad \Longleftrightarrow \quad
\begin{bmatrix}
  2 & 1 & -1 \\
  -3 & -1 & 2 \\
  -2 & 1 & 2
\end{bmatrix}
\begin{bmatrix}
  x \\ y \\ z
\end{bmatrix}
=
\begin{bmatrix}
  8 \\ -11 \\ -3
\end{bmatrix}
\]

Borders and Labeling

Sometimes you need to add row and column labels to a matrix for clarity. You can accomplish this using the array environment with additional rows and columns for labels, or by using packages like blkarray.

% Matrix with row and column labels using array
\[
\begin{array}{c|ccc}
  & x_1 & x_2 & x_3 \\
  \hline
  y_1 & 1 & 2 & 3 \\
  y_2 & 4 & 5 & 6 \\
  y_3 & 7 & 8 & 9
\end{array}
\]
% Labeled matrix with kbordermatrix (requires package)
% \usepackage{kbordermatrix}
\[
\kbordermatrix{
  & c_1 & c_2 & c_3 \\
  r_1 & 1 & 2 & 3 \\
  r_2 & 4 & 5 & 6 \\
  r_3 & 7 & 8 & 9
}
\]
% Using blkarray for labeled block matrices
\usepackage{blkarray}

\[
\begin{blockarray}{cccc}
  & x_1 & x_2 & x_3 \\
  \begin{block}{c(ccc)}
    y_1 & a_{11} & a_{12} & a_{13} \\
    y_2 & a_{21} & a_{22} & a_{23} \\
    y_3 & a_{31} & a_{32} & a_{33} \\
  \end{block}
\end{blockarray}
\]

Quick Reference Table

Here is a summary of all the key matrix commands and environments covered in this guide:

Command / EnvironmentOutputNotes
\begin{matrix}Plain matrix (no delimiters)Requires amsmath
\begin{pmatrix}Parenthesized matrix ( )Most common for vectors
\begin{bmatrix}Bracketed matrix [ ]Common in linear algebra texts
\begin{Bmatrix}Braced matrix { }Set-theoretic notation
\begin{vmatrix}Determinant | |Standard determinant notation
\begin{Vmatrix}Norm || ||Double vertical bars
\begin{smallmatrix}Compact inline matrixWrap with \left(\right)
\begin{array}{cc|c}Augmented matrixUse | for vertical separator
&Column separatorSeparates entries in a row
\\Row separatorEnds the current row
\cdotsHorizontal dotsPattern across columns
\vdotsVertical dotsPattern down rows
\ddotsDiagonal dotsDiagonal pattern
A^TTransposeSuperscript T
A^{-1}InverseSuperscript -1
\det(A)Determinant functionUpright "det" operator
\operatorname{diag}Diagonal operatorFor diagonal matrix shorthand

Next Steps

Now that you can create matrices in LaTeX, explore more mathematical typesetting topics:

Free LaTeX Tools

Ready to start writing?

Try Octree – the AI-powered LaTeX editor that makes writing faster and easier.

Try Octree Free →