# Labeling and Double Labeling Matrices in LaTex (Overleaf) - useful for Markov chain transition matrices

This is actually not a question, but a solution. I looked everywhere and all of the solutions given are super complicated so I thought I would share my own for people who need help!!

• Treat the matrix/labels as one big array
• I used "tabular" command instead of array because the horizontal spacing doesn't work when you use the "array" command, however this doesn't matter too much because the syntax is relatively the same for both

I will post my code in the answer!!

In order to label a simple 3x3 matrix, you can either do it by columns/rows or give it a full title over the whole matrix.

This is the code for both the full titles and by each column/row:

\begin{document}

\begingroup
\setlength{\tabcolsep}{2pt} % sets spacing horizontally
\renewcommand{\arraystretch}{1} % sets spacing vertically
\begin{tabular}{ c c c }

%===============================================================
%Row 1
%===============================================================
%Column 1
Top Title &

%Column 2
&

%Column 3
\\

%===============================================================
%Row 2
%===============================================================
%Column 1
%this creates the horizontal array
\begingroup %make sure to use this to control spacing separately from the bigger array
\setlength{\tabcolsep}{6pt}
\begin{tabular}{cccc}
1  & 2  & 3
\end{tabular}
\endgroup&

%Column 2
&

%Column 3
\\

%===============================================================
%Row 3
%===============================================================

% Column 1
$\begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix}$ &

%Column 2
% this creates the vertical array
\begin{tabular}{ccc}
x \\ y \\ z
\end{tabular} &

%Column 3
Side Title

\end{tabular}
\endgroup

\end{document}


From this code you can adjust what titles you would like to keep/erase. Remember to adjust the matrix size accordingly. Matrices/tables/arrays have a syntax of:

Row1Column1 & Row1Column2     & ... & Row1Column(N) \\

Row2Column1 & Row2Column2     & ... & Row2Column(N) \\
.
.
.

Row(N)Column1 & Row(N)Column2 & ... & Row(N)Column(N)



So this is essentially a 3x3 matrix with the first column being:

• Big title (Top Title)
• column titles (1 2 3)
• matrix

Second Column:

• Empty space
• Empty space
• row titles (x y z)

Third Column:

• Empty space
• Empty space
• Big Title (Side title)

Welcome to TEX.SE! It's a nice approach. I use nicematrix package to do this.

\documentclass{article}
\usepackage{amsmath}
\usepackage{nicematrix}
\begin{document}
\begin{align*}
\begin{array}{cc}
\text{Top Title} & \\
\begin{bNiceMatrix}[first-row,last-col]
1 & 2 & 3 &     \\
a & b & c & x   \\
d & e & f & y   \\
g & h & i & z
\end{bNiceMatrix} &  \text{Side Title}
\end{array}
\end{align*}
\end{document}


Here I handled the matrix with exterior with bNiceMatrix environment.

Here is another way to do that with nicematrix. In the latest version (5.10 of 2021-02-05), there is a built-in command \SubMatrix which I used to add the brackets in the array previously constructed.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
$A = \enskip \begin{NiceMatrix}[baseline=4] \Block{1-3}{\text{Top Title}} \\ 1 & 2 & 3 & \\ a & b & c & x & \Block{3-1}{\text{Side Title}} \\ d & e & f & y \\ g & h & i & z \CodeAfter \SubMatrix[{3-1}{5-3}] \end{NiceMatrix}$
\end{document}


You need several compilations (because nicematrix uses PGF/Tikz nodes).