# How can I type text above a matrix?

I'm trying to write a matrix like the one in the image, but I don't know how to do it (I tried using the align environment, but I had no luck). Any help will be really appreciated. :)

• Welcome to TeX SX! You could do that with the blkarray package. – Bernard Jan 27 at 22:57
• Also nicematrix is promising candidate. – Zarko Jan 27 at 22:58
• Welcome to the TeX.SE community. You can try with kbordermatrix, spalign, nicematrix package for example. But I'm scared of the photo is not that you come from Dante Alighieri's hell? Italy celebrates 700 years since his birth.:-)))))) I'm joking. – Sebastiano Jan 27 at 22:59
• @Sebastiano: A Cat in hell ??? ;o) – Bernard Jan 27 at 23:14
• Thank you guys sooo much, I was struggling a lot with this one :( I'll try with those packages, thank youuu <3 – Doja Cat Jan 27 at 23:28

Here's a solution that uses only some very basic LaTeX packages, along with center, tabular, and bmatrix environments. I've tried as much as possible to mimic the layout in the screenshot you posted.

\documentclass{article}
\usepackage{array,xcolor,amsmath,multirow}
\begin{document}

\begin{center}
\begin{tabular}{@{} r @{} l c >{\hspace{7mm}}l @{}}
& \multicolumn{2}{c}{\textcolor{cyan}{From:}} & \\
& \textcolor{cyan}{City}
& \textcolor{cyan}{Subu\rlap{rbs}} % ok, the use of '\rlap' is kludgy...
& \textcolor{cyan}{To:} \\[0.75ex]
\multirow{2}{*}{$M{=}$}
& \multicolumn{2}{@{}l}{\multirow{2}{*}{%
$\begin{bmatrix} 0.95 & 0.03 \\ 0.05 & 0.97 \end{bmatrix}$}}
& \textcolor{cyan}{City} \\
& & & \textcolor{cyan}{Suburbs}
\end{tabular}
\end{center}

\end{document}

• It looks great, thank you very much! – Doja Cat Jan 27 at 23:30

Here is a way to do that with {NiceTabular} of nicematrix.

You put all the elements (text and numbers) in a great array and you put the brackets where you want with the command \SubMatrix in the \CodeAfter.

With the key baseline, you put the baseline where you want (for the alignment with M=).

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\newcommand{\cyan}{\color{cyan}}

$M = \begin{NiceTabular}{cc>{\cyan}l}[baseline=line-4] \Block{1-2}{\cyan From:} \\ \cyan City & \cyan Suburbs & To: \\$.95$&$.03$& City \\$.05$&$.97$& Suburbs \\ \CodeAfter \SubMatrix[{3-1}{4-2}][slim] \end{NiceTabular}$

\end{document}


You need several compilations (because nicematrix uses PGF/Tikz under the hood).

• Excellent your package :-) – Sebastiano Mar 13 at 13:19