4

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. :)

enter image description here

6
  • 2
    Welcome to TeX SX! You could do that with the blkarray package.
    – Bernard
    Jan 27, 2021 at 22:57
  • 1
    Also nicematrix is promising candidate.
    – Zarko
    Jan 27, 2021 at 22:58
  • 1
    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, 2021 at 22:59
  • 1
    @Sebastiano: A Cat in hell ??? ;o)
    – Bernard
    Jan 27, 2021 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, 2021 at 23:28

2 Answers 2

4

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.

enter image description here

\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}
1
  • It looks great, thank you very much!
    – Doja Cat
    Jan 27, 2021 at 23:30
1

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).

Output of the above code

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .