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. :)
- 43
2 Answers
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}
- 506,678
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).
- 40,250



blkarraypackage. – Bernard Jan 27 '21 at 22:57nicematrixis promising candidate. – Zarko Jan 27 '21 at 22:58