0

I'm trying to create table in Latex like this;

\begin{table}[h|]
  \begin{center}

    \begin{tabular}{cccc}
  
      & & \multicolumn{2}{c}{\large Actual Values} \\
      & & Positive (1) & Negative (0) \\\cline{3-4}
      {\rotatebox[origin=c]{90}{\large Values}} & Positive (1) & 
      \multicolumn{1}{|l|} {TP} & \multicolumn{1}{l|} {FP} \\\cline{3-4}  
      {\rotatebox[origin=c]{90}{\large Predicted    }} &  Negative (0) &  \multicolumn{1}{|l|}  {FN}  &  \multicolumn{1}{l|} {TN}  \\\cline{3-4}
    
    \end{tabular}

  \end{center}
  \caption[Confusion Matrix]{\small Confusion Matrix}
  \label{table: matrix}
\end{table}

But it ends up like this;

Matrix Image

I've two queries here.

How can I center align cell text?

I tried different methods but due to multicolumn, it is not adjusting cell text in center.

How can I adjust height of both rows same?

I seriously have no idea how can I do this?

I'm new to latex, so any kind of help will be appreciated.

10
  • Doe "center align" refer to horizontally or vertically centere text or both?
    – leandriis
    Feb 7, 2021 at 16:52
  • where to put this "center align"? Feb 7, 2021 at 16:58
  • Sorry for the typo in my previous comment. I just wanted to know what your requirement "How can I center align cell text?" refers to. Do you want to horizontally or vertically center the contents in your cells or do you want both?
    – leandriis
    Feb 7, 2021 at 17:00
  • 1
    simply edit the code block above so it starts \documentclass{article} and ends \end{document} and demonstrates the problem without generating errors. Feb 7, 2021 at 17:12
  • 2
    you specified centred columns then used \multicolumn{1}{|l|} the only purpose of which is to over-ride the c with l for left alignment. Is the answer just "delete` the multicolumn or was there some other intention for those commands? Feb 7, 2021 at 17:20

1 Answer 1

4

The nicematrix package provides the command \Block which takes care of vertical and horizontal centering (neither multicolumn nor multirow are needed).

It also allows global directives to expand the cells. The code obtained is very simple and easy to maintain.

You need to compile it twice the first time. Later only once, if you do not change the setup of the table.

x

UPDATE nicematrix v6.24

\documentclass[12pt,a4paper]{article}
\usepackage{nicematrix}

\begin{document}    
    
    \begin{table}
        \centering              
        \setlength{\tabcolsep}{0pt}
        \begin{NiceTabular}{c@{\enskip}c@{\enskip}wc{2.5cm}wc{2.5cm}}
                &               & \Block{1-2}{\large Actual Values}         \\[1ex]   
                &               & \Block{}{Positive (1)}    & Negative (0)  \\[1ex]
                \RowStyle[nb-rows=2]{\rule[-1.1cm]{0pt}{2.5cm}}% make the cells a square
                \Block{2-1}<\rotate\large >{Predicted Values}
                & Positive (1)  & \Block[hvlines]{2-2}{}TP  & FP    \\
                & Negative (0)  & FN                        & Tn    \\         
        \end{NiceTabular}
        \medskip
        \caption[Confusion Matrix]{\small Confusion Matrix}
        \label{table: matrix}
    \end{table}

\end{document}

You must log in to answer this question.

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