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

  • 2
    Welcome to TeX SX! You could do that with the blkarray package. – Bernard Jan 27 at 22:57
  • 1
    Also nicematrix is promising candidate. – Zarko Jan 27 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 at 22:59
  • 1
    @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.

enter image description here


\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]
& \multicolumn{2}{@{}l}{\multirow{2}{*}{%
           0.95 & 0.03 \\ 
           0.05 & 0.97 
& \textcolor{cyan}{City} \\
& & & \textcolor{cyan}{Suburbs} 

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




$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]


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

Output of the above code

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.