# Label rows of a matrix by characters

How to add label to index the rows and columns of the matrix ? because I want to number the line outside the matrix

     $\begin{array}{lc} \ Mat_{\varPhi to M} & \left(\begin{array}{@{}ccccc@{}} 1 & 1 & 1 & 1 & 1 \\ 0 & 1 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 & 1 \\ 0 & 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 0 & 1 \end{array}\right) \\[15pt] \end{array}$


Here is an option using kbordermatrix:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{kbordermatrix}% https://kcborder.caltech.edu/TeX/kbordermatrix.sty
\begin{document}
$\begin{array}{l@{{}={}}c} \text{Mat}_{\varphi\text{ to }M} & \left(\begin{array}{@{}ccccc@{}} 1 & 1 & 1 & 1 & 1 \\ 0 & 1 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 & 1 \\ 0 & 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 0 & 1 \end{array}\right) \end{array}$

\renewcommand{\kbldelim}{(}% Left delimiter
\renewcommand{\kbrdelim}{)}% Right delimiter
$\text{Mat}_{\varphi\text{ to }M} = \kbordermatrix{ & c_1 & c_2 & c_3 & c_4 & c_5 \\ r_1 & 1 & 1 & 1 & 1 & 1 \\ r_2 & 0 & 1 & 0 & 0 & 1 \\ r_3 & 0 & 0 & 1 & 0 & 1 \\ r_4 & 0 & 0 & 0 & 1 & 1 \\ r_5 & 0 & 0 & 0 & 0 & 1 }$

\end{document}


For more options and styles, including the usage of \bordermatrix, see Where is the \matrix command?

• This doesn't seem to work (anymore). See tex.stackexchange.com/questions/249484/…
– JJJ
May 17, 2019 at 23:57
• Worked for me today. Not to find out how I can place two of them side by side for comparative purposes... Feb 12, 2023 at 19:23

You can use the blkarray package:

\documentclass[12pt]{report}
\usepackage{blkarray}
\usepackage{amsmath}

\begin{document}

$\begin{blockarray}{cccccc} a & b & c & d & e \\ \begin{block}{(ccccc)c} 1 & 1 & 1 & 1 & 1 & f \\ 0 & 1 & 0 & 0 & 1 & g \\ 0 & 0 & 1 & 0 & 1 & h \\ 0 & 0 & 0 & 1 & 1 & i \\ 0 & 0 & 0 & 0 & 1 & j \\ \end{block} \end{blockarray}$

\end{document}


• This approach is way more flexible and intuitive, +1, plus available through CTAN. Oct 8, 2013 at 17:50
• How can I make horizontal lines between rows?
– dh16
Oct 15, 2018 at 0:30
• @dh16 You might want to post your own question for that, but I'd suggest maybe using a table instead. Feb 26, 2021 at 9:46
• The best approach Apr 18, 2023 at 10:07

For the exterior rows and columns, it's possible to use nicematrix:

\documentclass{article}
\usepackage{nicematrix}
\NiceMatrixOptions{
code-for-first-row = \color{blue} ,
code-for-last-row = \color{blue} ,
code-for-first-col = \color{blue} ,
code-for-last-col = \color{blue}
}

\begin{document}
$\begin{pNiceMatrix}[first-row,last-row,first-col,last-col] & C_1 & C_2 & C_3 & C_4 \\ L_1 & a & b & c & d & L_1 \\ L_2 & e & f & g & h & L_2 \\ L_3 & i & j & k & l & L_3 \\ & C_1 & C_2 & C_3 & C_4 \\ \end{pNiceMatrix}$
\end{document}


• +1 The aesthetics of the delimiters with respect to the matrix elements are much nicer here than in the solution with blkarray, for example. Aug 27, 2020 at 9:43

I'll add a variant with blockarray, with footnote size text as column and row headings (answer to another, duplicate question):

\documentclass{article}
\usepackage{mathtools}
\usepackage{blkarray, bigstrut} %

\begin{document}

\begin{equation*}
\mathbf{S}=
\begin{blockarray}{*{5}{c} l}
\begin{block}{*{5}{>{$\footnotesize}c<{$}} l}
Rose & Teal & Plum & Sand & Peach & \\
\end{block}
\begin{block}{[*{5}{c}]>{$\footnotesize}l<{$}}
0 & 9 & 8 & 0 & 7 \bigstrut[t]& small \\
3 & 5 & 6 & 5 & 1 & medium \\
1 & 0 & 0 & 3 & 2 & large \\
\end{block}
\end{blockarray}
\end{equation*}

\end{document}


• Thanks for this contribution. It doesn't seem to work for font size though: I'm setting the column and header size to \tiny (or \footnotesize, as in the example) and it is displaying the headers with the same size as the coefficients. On the other hand, if I set the color of the headers as \color[gray]{0.6}, it does display correctly (i.e. headers in gray, coefficients in black). Do you know how to fix this? Sep 29, 2018 at 13:05

I introduce this answer, because it uses a new feature of the tabstackengine which allows one to remember various data and dimensions from the most recently created stack. Thus, what is done here, since the labels are wider than the matrix content, are as follows:

1. I set \fixTABwidth{T} to indicate that TABstacks should be built so that all columns are of a width fixed to the maximum column-width of the build. I also indicate that TABstacks that are built in text mode should be set in \scriptsize, colored blue.

2. I build the column labels and save them in \collabels AND then I save, into macro \colwidth, the maximum dimension of the columns, which had been stored in \maxTABwidth.

3. I save the stack of row labels as \rowlabels.

4. Up to this point, the label stacks were created in text mode (they could have been just as easily done in math mode). But now, I execute the actual matrix in \ensurestackMath mode.

5. To the right of the equal sign, I stack the column label stack upon the matrix itself. However, to get the right column width on the matrix elements, I merely needed to place one of the matrix entries in a box of width \colwidth, which was the saved column-width of the column label stack. Finally, I regurgitate my row label stack.

Here is the MWE.

\documentclass{article}
\usepackage{tabstackengine}[2016-11-30]
\usepackage{xcolor}
\begin{document}
$%\stackText% MUST BE PREVAILING MODE TO GET LABELS IN TEXT \TABstackTextstyle{\scriptsize\color{blue}} \fixTABwidth{T} \savestack\collabels{\tabbedCenterstack{dog & cat& mouse & aardvark & ant}} \edef\colwidth{\maxTABwd} \savestack\rowlabels{\tabbedCenterstack[l]{this \\ land \\ is \\ my \\ land!}} \ensurestackMath{ \textrm{Mat}_{\varphi \mathrm{~to~} M} = \stackon{% \parenMatrixstack{ \makebox[\colwidth]{1} & 1 & 1 & 1 & 1\\ 0 & 1 & 0 & 0 & 1\\ 0 & 0 & 1 & 0 & 1\\ 0 & 0 & 0 & 1 & 1\\ 0 & 0 & 0 & 0 & 1 }% }{\collabels} \rowlabels }$
\end{document}