Matrices are fundamental in linear algebra, quantum mechanics, statistics, computer graphics, and countless mathematical applications. LaTeX provides multiple matrix environments with different bracket styles, making it easy to typeset matrices professionally with proper alignment and formatting.
This comprehensive guide covers all LaTeX matrix environments—matrix, pmatrix, bmatrix, vmatrix, and Vmatrix—along with dots notation, augmented matrices, determinants, and advanced techniques for large and complex matrices.
Table of Contents
Matrix Environments Overview
LaTeX provides five main matrix environments from the amsmath package, each with different delimiter styles:
Matrix Environment Comparison
\usepackage{amsmath}
% No delimiters
\begin{matrix}
a & b \\
c & d
\end{matrix}
% Parentheses ( )
\begin{pmatrix}
a & b \\
c & d
\end{pmatrix}
% Square brackets [ ]
\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
% Single bars | |
\begin{vmatrix}
a & b \\
c & d
\end{vmatrix}
% Double bars || ||
\begin{Vmatrix}
a & b \\
c & d
\end{Vmatrix}
When to Use Each Type
- matrix: Plain matrix without delimiters, or when adding custom delimiters
- pmatrix: Most common for general matrices with parentheses
- bmatrix: Square brackets, common in linear algebra and computer science
- vmatrix: Determinant notation with single bars
- Vmatrix: Norm notation with double bars
Required Package
\usepackage{amsmath} % Required for all matrix environments
Creating Basic Matrices
Matrix syntax is straightforward: columns separated by &, rows separated by \\.
Basic Matrix Syntax
% 2×2 matrix
\[
\begin{pmatrix}
a & b \\
c & d
\end{pmatrix}
\]
% 3×3 matrix
\[
\begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{pmatrix}
\]
Column Vector
% Column vector (n×1 matrix)
\[
\begin{pmatrix}
x_1 \\
x_2 \\
x_3 \\
\vdots \\
x_n
\end{pmatrix}
\]
Row Vector
% Row vector (1×n matrix)
\[
\begin{pmatrix}
x_1 & x_2 & x_3 & \cdots & x_n
\end{pmatrix}
\]
Matrix with Numbers
% Concrete numerical matrix
\[
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{bmatrix}
\]
Matrix with Variables
% Symbolic matrix
\[
\begin{pmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{pmatrix}
\]
Matrix Types: pmatrix, bmatrix, vmatrix
Each matrix environment serves specific mathematical conventions and purposes.
Parenthesis Matrix (pmatrix)
% Most common for general matrices
\[
A = \begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
\]
% Matrix multiplication
\[
\begin{pmatrix}
a & b \\
c & d
\end{pmatrix}
\begin{pmatrix}
x \\
y
\end{pmatrix}
=
\begin{pmatrix}
ax + by \\
cx + dy
\end{pmatrix}
\]
Bracket Matrix (bmatrix)
% Common in linear algebra and computer science
\[
B = \begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{bmatrix}
\]
% Transformation matrix
\[
T = \begin{bmatrix}
\cos\theta & -\sin\theta \\
\sin\theta & \cos\theta
\end{bmatrix}
\]
Determinant (vmatrix)
% Single bars indicate determinant
\[
\det(A) = \begin{vmatrix}
a & b \\
c & d
\end{vmatrix}
= ad - bc
\]
% 3×3 determinant
\[
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}
\]
Norm (Vmatrix)
% Double bars indicate norm
\[
\begin{Vmatrix}
x \\
y \\
z
\end{Vmatrix}
= \sqrt{x^2 + y^2 + z^2}
\]
Plain Matrix (No Delimiters)
% Use with custom delimiters
\[
\left\langle
\begin{matrix}
a & b \\
c & d
\end{matrix}
\right\rangle
\]
Dots Notation in Matrices
Dots indicate patterns or omitted elements in large matrices. LaTeX provides horizontal, vertical, and diagonal dot commands.
Dot Commands
% Horizontal dots (···)
\cdots
% Vertical dots (⋮)
\vdots
% Diagonal dots (⋱)
\ddots
% Diagonal dots (other direction ⋰)
\iddots % Requires mathdots package
General n×n Matrix
% Matrix with ellipsis notation
\[
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}
\]
Diagonal Matrix
% Diagonal elements with zeros
\[
D = \begin{pmatrix}
d_1 & 0 & \cdots & 0 \\
0 & d_2 & \cdots & 0 \\
\vdots & \vdots & \ddots & \vdots \\
0 & 0 & \cdots & d_n
\end{pmatrix}
\]
Identity Matrix
% Identity matrix notation
\[
I_n = \begin{pmatrix}
1 & 0 & \cdots & 0 \\
0 & 1 & \cdots & 0 \\
\vdots & \vdots & \ddots & \vdots \\
0 & 0 & \cdots & 1
\end{pmatrix}
\]
Block Matrix
% Matrix partitioned into blocks
\[
M = \begin{pmatrix}
A & B & \cdots \\
C & D & \cdots \\
\vdots & \vdots & \ddots
\end{pmatrix}
\]
Tridiagonal Matrix
% Three diagonals
\[
T = \begin{pmatrix}
b_1 & c_1 & 0 & \cdots & 0 \\
a_1 & b_2 & c_2 & \cdots & 0 \\
0 & a_2 & b_3 & \cdots & 0 \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
0 & 0 & 0 & \cdots & b_n
\end{pmatrix}
\]
Special Matrices
Common matrix types with specific structures and properties.
Identity Matrix
% 3×3 Identity
\[
I = \begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{bmatrix}
\]
Zero Matrix
% All zeros
\[
O = \begin{pmatrix}
0 & 0 & 0 \\
0 & 0 & 0 \\
0 & 0 & 0
\end{pmatrix}
\]
Diagonal Matrix
% Only diagonal elements non-zero
\[
D = \begin{pmatrix}
2 & 0 & 0 \\
0 & 3 & 0 \\
0 & 0 & 5
\end{pmatrix}
\]
Upper Triangular Matrix
% Zeros below diagonal
\[
U = \begin{bmatrix}
1 & 2 & 3 \\
0 & 4 & 5 \\
0 & 0 & 6
\end{bmatrix}
\]
Lower Triangular Matrix
% Zeros above diagonal
\[
L = \begin{bmatrix}
1 & 0 & 0 \\
2 & 3 & 0 \\
4 & 5 & 6
\end{bmatrix}
\]
Symmetric Matrix
% A = A^T
\[
S = \begin{pmatrix}
1 & 2 & 3 \\
2 & 4 & 5 \\
3 & 5 & 6
\end{pmatrix}
\]
Skew-Symmetric Matrix
% A^T = -A
\[
K = \begin{pmatrix}
0 & a & b \\
-a & 0 & c \\
-b & -c & 0
\end{pmatrix}
\]
Rotation Matrix
% 2D rotation by angle θ
\[
R(\theta) = \begin{bmatrix}
\cos\theta & -\sin\theta \\
\sin\theta & \cos\theta
\end{bmatrix}
\]
Augmented Matrices
Augmented matrices combine a coefficient matrix with a constant vector, commonly used in solving linear systems.
Using array Environment
% Augmented matrix with vertical bar
\[
\left[
\begin{array}{ccc|c}
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8 \\
9 & 10 & 11 & 12
\end{array}
\right]
\]
System of Linear Equations
% [A | b] augmented matrix
\[
\left[
\begin{array}{rrr|r}
2 & -1 & 3 & 5 \\
1 & 3 & -2 & -4 \\
-1 & 2 & 1 & 3
\end{array}
\right]
\]
Multiple Separators
% Multiple vertical bars
\[
\left[
\begin{array}{cc|c|c}
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8
\end{array}
\right]
\]
Row Reduction Steps
% Show Gaussian elimination steps
\[
\left[
\begin{array}{rrr|r}
2 & 4 & 6 & 8 \\
1 & 3 & 2 & 5 \\
3 & 1 & 5 & 7
\end{array}
\right]
\xrightarrow{R_1 \to R_1/2}
\left[
\begin{array}{rrr|r}
1 & 2 & 3 & 4 \\
1 & 3 & 2 & 5 \\
3 & 1 & 5 & 7
\end{array}
\right]
\]
Reduced Row Echelon Form
% Final RREF form
\[
\left[
\begin{array}{rrr|r}
1 & 0 & 0 & x \\
0 & 1 & 0 & y \\
0 & 0 & 1 & z
\end{array}
\right]
\]
Determinants
Determinants are scalar values computed from square matrices, denoted with single vertical bars.
2×2 Determinant
% Basic 2×2 determinant
\[
\begin{vmatrix}
a & b \\
c & d
\end{vmatrix}
= ad - bc
\]
3×3 Determinant
% 3×3 determinant
\[
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}
= a\begin{vmatrix}
e & f \\
h & i
\end{vmatrix}
- b\begin{vmatrix}
d & f \\
g & i
\end{vmatrix}
+ c\begin{vmatrix}
d & e \\
g & h
\end{vmatrix}
\]
Determinant Notation
% Various determinant notations
\[
\det(A) = |A| = \begin{vmatrix}
a & b \\
c & d
\end{vmatrix}
\]
Properties of Determinants
% Determinant of product
\[
\det(AB) = \det(A) \cdot \det(B)
\]
% Determinant of transpose
\[
\det(A^T) = \det(A)
\]
% Determinant of inverse
\[
\det(A^{-1}) = \frac{1}{\det(A)}
\]
Cofactor Expansion
% Expansion along first row
\[
\det(A) = \sum_{j=1}^{n} (-1)^{1+j} a_{1j} M_{1j}
\]
Large Matrices
Techniques for handling matrices that exceed normal size constraints.
Small Matrix Environment
% Inline small matrices
The transformation $A = \begin{psmallmatrix} 1 & 2 \\ 3 & 4 \end{psmallmatrix}$ is compact.
% Other small variants
\begin{bsmallmatrix} ... \end{bsmallmatrix}
\begin{vsmallmatrix} ... \end{vsmallmatrix}
Scaling Large Matrices
% Scale down large matrices
\[
\scalebox{0.8}{$
\begin{pmatrix}
1 & 2 & 3 & 4 & 5 \\
6 & 7 & 8 & 9 & 10 \\
11 & 12 & 13 & 14 & 15 \\
16 & 17 & 18 & 19 & 20
\end{pmatrix}
$}
\]
Adjusting Column Spacing
% Increase space between columns
\[
\begin{pmatrix}
1 & 2 & 3 \\[5pt]
4 & 5 & 6 \\[5pt]
7 & 8 & 9
\end{pmatrix}
\]
% Or use arraystretch
\renewcommand{\arraystretch}{1.5}
\begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{pmatrix}
Breaking Large Matrices
% Split across pages or lines
\[
A = \begin{pmatrix}
\text{Top-left block} & \cdots \\
\vdots & \ddots
\end{pmatrix}
\]
Maximum Matrix Size
% Increase max columns (default 10)
\setcounter{MaxMatrixCols}{20}
% Now can use up to 20 columns
Advanced Techniques
Colored Matrix Elements
\usepackage{xcolor}
% Highlight specific elements
\[
\begin{pmatrix}
\textcolor{red}{1} & 2 & 3 \\
4 & \textcolor{red}{5} & 6 \\
7 & 8 & \textcolor{red}{9}
\end{pmatrix}
\]
Matrix with Labels
% Row and column labels
\[
\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}
\]
Bordered Matrix
% Matrix with border
\[
\begin{array}{|ccc|}
\hline
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\hline
\end{array}
\]
Matrix with Annotations
% Arrow annotations
\[
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
\xrightarrow[\text{operation}]{\text{row}}
\begin{pmatrix}
1 & 2 \\
0 & -2
\end{pmatrix}
\]
Kronecker Product
% Tensor product of matrices
\[
A \otimes B = \begin{pmatrix}
a_{11}B & a_{12}B \\
a_{21}B & a_{22}B
\end{pmatrix}
\]
Block Matrix Notation
% Partitioned matrix
\[
M = \left[
\begin{array}{c|c}
A & B \\
\hline
C & D
\end{array}
\right]
\]
Transpose Notation
% Matrix transpose
\[
A^T = \begin{pmatrix}
a & c \\
b & d
\end{pmatrix}, \quad
A = \begin{pmatrix}
a & b \\
c & d
\end{pmatrix}
\]
Inverse Matrix
% 2×2 inverse formula
\[
A^{-1} = \frac{1}{\det(A)} \begin{pmatrix}
d & -b \\
-c & a
\end{pmatrix}
\]
Troubleshooting
Too Many Columns Error
Problem: ! Extra alignment tab has been changed to \cr
Solution: Increase maximum columns:
\setcounter{MaxMatrixCols}{20} % Default is 10
Misaligned Matrix Elements
Problem: Elements don't align vertically.
Solution: Check for missing & or \\:
% Wrong (missing &)
\begin{pmatrix}
1 2 \\ % Missing & between 1 and 2
3 & 4
\end{pmatrix}
% Correct
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
Matrix Too Wide
Problem: Matrix extends beyond page margins.
Solutions:
% Solution 1: Scale down
\scalebox{0.8}{$\begin{pmatrix} ... \end{pmatrix}$}
% Solution 2: Use smaller font
{\small $\begin{pmatrix} ... \end{pmatrix}$}
% Solution 3: Reduce spacing
\setlength{\arraycolsep}{3pt}
\begin{pmatrix} ... \end{pmatrix}
Undefined Environment Error
Problem: ! LaTeX Error: Environment pmatrix undefined
Solution: Load amsmath package:
\usepackage{amsmath}
Vertical Spacing Too Tight
Problem: Rows are too close together.
Solution: Add extra spacing:
% Add space after specific rows
\begin{pmatrix}
1 & 2 \\[5pt]
3 & 4
\end{pmatrix}
% Or increase globally
\renewcommand{\arraystretch}{1.5}
Determinant Bars Wrong Size
Problem: Manual bars don't scale with matrix size.
Solution: Use vmatrix environment instead:
% Wrong (manual bars)
$| \begin{matrix} a & b \\ c & d \end{matrix} |$
% Correct (vmatrix)
$\begin{vmatrix} a & b \\ c & d \end{vmatrix}$
Frequently Asked Questions
How do I create a matrix in LaTeX?
Use a matrix environment from the amsmath package:
\usepackage{amsmath}
% Parentheses matrix
\[
\begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{pmatrix}
\]
Columns are separated by &, rows by \\.
What's the difference between pmatrix, bmatrix, and vmatrix?
The difference is in the delimiter style:
- pmatrix: Parentheses ( ) — most common for general matrices
- bmatrix: Square brackets [ ] — common in linear algebra
- vmatrix: Single bars | | — represents determinant
- Vmatrix: Double bars || || — represents norm
- matrix: No delimiters — use with custom brackets
\begin{pmatrix} a & b \\ c & d \end{pmatrix} % (a b; c d)
\begin{bmatrix} a & b \\ c & d \end{bmatrix} % [a b; c d]
\begin{vmatrix} a & b \\ c & d \end{vmatrix} % |a b; c d|
How do I create an augmented matrix?
Use the array environment with a vertical bar in the column specification:
\[
\left[
\begin{array}{ccc|c}
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8 \\
9 & 10 & 11 & 12
\end{array}
\right]
\]
The | in {ccc|c} creates a vertical separator after the third column.
How do I add dots to show matrix patterns?
Use \cdots (horizontal), \vdots (vertical), and \ddots (diagonal):
\[
\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}
\]
How do I write a determinant in LaTeX?
Use the vmatrix environment for single vertical bars:
\[
\det(A) = \begin{vmatrix}
a & b \\
c & d
\end{vmatrix}
= ad - bc
\]
Or use \det command with matrix:
\[
\det\begin{pmatrix} a & b \\ c & d \end{pmatrix}
\]
How do I make a column or row vector?
Column vector: Single column, multiple rows:
\[
\mathbf{v} = \begin{pmatrix}
x \\
y \\
z
\end{pmatrix}
\]
Row vector: Single row, multiple columns:
\[
\mathbf{v} = \begin{pmatrix} x & y & z \end{pmatrix}
\]
How do I make small matrices for inline text?
Use smallmatrix environments:
% Inline small matrix
The matrix $\begin{psmallmatrix} 1 & 2 \\ 3 & 4 \end{psmallmatrix}$ fits inline.
% Other variants
\begin{bsmallmatrix} ... \end{bsmallmatrix} % [brackets]
\begin{vsmallmatrix} ... \end{vsmallmatrix} % |bars|
These produce compact matrices that don't increase line spacing.
Why do I get "too many columns" error?
LaTeX's default maximum is 10 columns per matrix. Increase it:
% In preamble
\setcounter{MaxMatrixCols}{20} % Or higher
% Now can use up to 20 columns
\begin{pmatrix}
... (20 columns) ...
\end{pmatrix}
How do I increase spacing in matrices?
Adjust spacing with \arraystretch or explicit spacing:
% Method 1: Global stretch
\renewcommand{\arraystretch}{1.5}
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
% Method 2: Per-row spacing
\begin{pmatrix}
1 & 2 \\[5pt]
3 & 4
\end{pmatrix}
% Method 3: Column spacing
\setlength{\arraycolsep}{10pt}
Can I color matrix elements?
Yes, use the xcolor package:
\usepackage{xcolor}
% Color specific elements
\[
\begin{pmatrix}
\textcolor{red}{1} & 2 & 3 \\
4 & \textcolor{red}{5} & 6 \\
7 & 8 & \textcolor{red}{9}
\end{pmatrix}
\]
% Color entire row/column
\[
\begin{pmatrix}
\textcolor{blue}{1 & 2 & 3} \\
4 & 5 & 6 \\
7 & 8 & 9
\end{pmatrix}
\]
Conclusion
LaTeX's matrix environments—matrix, pmatrix, bmatrix, vmatrix, and Vmatrix—provide comprehensive tools for typesetting matrices of any size and complexity. From simple 2×2 matrices to large sparse matrices with ellipsis notation, LaTeX handles the formatting automatically while giving you complete control over delimiters and alignment.
Key takeaways: Use pmatrix for general matrices with parentheses, bmatrix for square brackets common in linear algebra, vmatrix specifically for determinants, and the array environment for augmented matrices. Master dots notation (\cdots, \vdots, \ddots) for representing patterns in large matrices, and remember to load the amsmath package for all matrix environments.
With these tools and techniques, you can typeset any matrix beautifully and professionally, whether for linear algebra homework, research papers, or technical documentation.