How to Add Row and Column Labels to Matrices in LaTeX
Standard LaTeX matrix environments don't support row or column labels, but labeled matrices are common in linear algebra, statistics (confusion matrices), and Markov chains. You'll need to use the array environment with manual spacing, or a specialized package like nicematrix. Bibby AI includes matrix templates with labels pre-configured in its template gallery, making it much quicker than building them from scratch in Overleaf.
Manual Labels with the array Environment
Use the array environment inside math mode with an extra column for row labels and an extra row for column labels:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\begin{array}{c|ccc}
& c_1 & c_2 & c_3 \\ \hline
r_1 & 1 & 0 & 0 \\
r_2 & 0 & 1 & 0 \\
r_3 & 0 & 0 & 1
\end{array}
\]
\end{document}Using kbordermatrix for Traditional Labeling
The kbordermatrix package places labels outside the matrix delimiters in the traditional style used in many textbooks:
\usepackage{kbordermatrix}
% Rename the label command if needed
\renewcommand{\kbldelim}{(}
\renewcommand{\kbrdelim}{)}
\[
\kbordermatrix{
& x_1 & x_2 & x_3 \\
y_1 & 0.8 & 0.1 & 0.1 \\
y_2 & 0.2 & 0.7 & 0.1 \\
y_3 & 0.1 & 0.2 & 0.7
}
\]Using nicematrix for Modern Labeled Matrices
The nicematrix package offers the most flexible and modern approach with fine control over borders, colors, and label placement:
\usepackage{nicematrix}
% Confusion matrix with labels
\[
\begin{pNiceArray}{ccc}[first-row, first-col]
& \text{Pred. Cat} & \text{Pred. Dog} & \text{Pred. Bird} \\
\text{Cat} & 50 & 3 & 2 \\
\text{Dog} & 4 & 45 & 1 \\
\text{Bird} & 1 & 2 & 48
\end{pNiceArray}
\]💡 Tips
- •For simple cases, the array environment with \hline works well and needs no extra packages.
- •nicematrix is the most powerful option — it also supports colored cells, block elements, and annotations.
- •When labeling a Markov transition matrix, put state names on both axes to show transition probabilities clearly.
- •Bibby AI's matrix builder tool lets you drag and drop labels onto matrices visually — no manual array wrangling needed.
Try This in Bibby AI
Write LaTeX faster with AI auto-complete and instant compilation.
Start Writing Free