1

I am trying to draw the following figure:

enter image description here

I have found an example to create the table in confusion matrix using only LaTeX code and tried to adapt the solution to my table

\documentclass{article}
%\usepackage{ifthen}
%\usetikzlibrary{matrix,calc}
\usepackage{graphicx}

\newcommand\MyBox[1]{% \fbox{\parbox[c][1.7cm][c]{1.7cm}{\centering #1}}% } \newcommand\MyVBox[1]{% \parbox[c][1.7cm][c]{1cm}{\centering\bfseries #1}% } \newcommand\MyHBox[2][\dimexpr1.7cm+2\fboxsep\relax]{% \parbox[c][1cm][c]{#1}{\centering\bfseries #2}% } \newcommand\MyTBox[4]{% \MyVBox{#1}\MyBox{#2}\hspace{-\fboxrule}% \MyBox{#3}\hspace{-\fboxrule}% \MyBox{#4}\par\vspace{-\fboxrule} }

\begin{document}

{
\offinterlineskip
% \raisebox{-5cm}[0pt][0pt]{\rotatebox[origin=c]{90}{\parbox[c][0pt][c]{1cm}{\textbf{Source2}\\[20pt]}}}\par
% \hspace*{1cm}\MyHBox[\dimexpr5.1cm+6\fboxsep\relax]{Source1}\par
\hspace*{1cm}\MyHBox{LH}\MyHBox{RH}\MyHBox{Other contact}\par
\MyTBox{Type 0}{25}{0}{0}
\MyTBox{Type 1}{1}{43}{1}
\MyTBox{Undetected{0}{5}{-}
}

\end{document}

and it does not compile. It shows, Missing \endcsname inserted. error.

I would like to generate the figure above and, if possible, use tikz and standalone documentclass.

user1993416
  • 1,046
  • https://tex.stackexchange.com/a/610750/197451 -- and -- https://tex.stackexchange.com/a/558899/197451 -- and -- https://tex.stackexchange.com/questions/69713/matrix-change-row-or-column-background – js bibra Jan 25 '22 at 11:36

2 Answers2

2
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix, positioning}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\matrix[matrix of nodes, nodes in empty cells, nodes={anchor=center, minimum width=3cm, minimum height=2cm, align=center, draw, text width=1.7cm}, column sep=-\pgflinewidth, row sep=-\pgflinewidth,
row 1/.style={nodes={draw=none}},
column 1/.style={nodes={draw=none}}] (A)
{
    & LH & RH & {Other contact} & {Other\\ No contact}\\
    Type 0 & 25 & 0 & 0 & 3\\
    Type 1 & 1 & 43 & 1 & 2\\
    Undetected & 0 & 5 & -- & --\\
};
\draw[green!80!black, line width=1mm] (A-2-2.north west) rectangle (A-3-3.south east);
\draw (A-2-2.north west)-- node[above right]{Real} node[below left]{AI model} (A-1-1.north west);
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
2

Here is a way to do that with {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{center} \setlength{\tabcolsep}{10pt} \renewcommand{\arraystretch}{1.5} \begin{NiceTabular}{cccc}[hvlines,first-col,first-row,columns-width=auto] \diagbox{Al model}{Real} & LH & RH & \Block{}{Other\ contact} & \Block{}{Other\ No contact} \ Type 0 \quad & \Block[draw=green,line-width=1pt,transparent]{2-2}{} 25 & 0 & 0 & 3 \ Type 1 \quad & 1 & 43 & 1 & 2 \ Undetected \quad & 0 & 5 & -- & -- \end{NiceTabular}
\end{center}

\end{document}

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

Output of the above code

F. Pantigny
  • 40,250