TransWikia.com

How can I type text above a matrix?

TeX - LaTeX Asked by Doja Cat on March 31, 2021

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

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}{Suburlap{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}

Correct answer by Mico on March 31, 2021

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

Answered by F. Pantigny on March 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP