0

This is my first question on this site, and I'm also still a newbie with LaTeX and its plethora of packages.

I'm trying to recreate this table: enter image description here

I found this old post and tried to recreate it with the \rotatebox method, but the result ignores the forced line break in the first column, and also ignores the centering of the second column. Here's the code I used (I'm working in Overleaf if that's important):

\documentclass{article}

\usepackage{graphicx} \usepackage{tabularx} \usepackage{multirow}

\begin{document}

{ \newcolumntype{Y}{>{\centering\arraybackslash}X} \begin{table}[h!] \centering \caption{Confusion matrix and performance metrics calculated from the combinations of true and predicted conditions}\label{tab:tableConfMatrix2}

\begin{tabularx}{\textwidth}{|Y|Y|Y|Y|Y|}
\hline
\multicolumn{2}{|c|}{} & \multicolumn{2}{c|}{\textbf{Prediction/ classification}} & \\
\cline{3-4}
\multicolumn{2}{|c|}{} & positive & negative & \\
\hline
\multirow[c]{2}{=}{\rotatebox[origin=c]{90}{\textbf{True condition /\newline gold standard}}} & \rotatebox[origin=c]{90}{positive} & True positive, TP & False negative, FN & Sensitivity\newline= TP / (TP + FN)\\
\cline{2-5}
& \rotatebox[origin=c]{90}{negative} & False positive, FP & True negative, TN & Specificity\newline= TN / (TN + FP)\\
\hline
\multicolumn{2}{|c|}{} & Positive predictive value\newline= TP / (TP + FP) & Negative predictive value\newline= TN / (TN + FN) & \\
\hline
\end{tabularx}

\end{table} } \end{document}

enter image description here

I also tried the \begin{sideways} command from the rotating package with exactly the same result. Now I'm out of ideas.

Sandy G
  • 42,558
MattR0se
  • 101

1 Answers1

1

My answer based on tabularray, which i would suggest if you have to deal with that kind of tables. I am not entirely happy with this because I've had to do a little tuning

enter image description here

In general, tabularray has more intuitive interface and its functionality can be extended by a number of libraries; some of them provide similar functionality to standalone packages, mainly booktabs, siunitx etc.

I also set custom background at 4 corners. If you don't like it, just remove bg=cellbg from affected cells as well as the line \colorlet{cellbg}{black!10} and the macro \uspackage{xcolor} from the preamble.

Other points.
A few extra macros and custom lengths are added to slightly clarify the main code and avoid repetition.


EDIT. While the right-hand side lower corner is greyed-out, I believe there is one more metric

Accuracy = (TP + TN)/N

I could change the code to include it in the table or you may want to try it yourself. Let me know if you have a problem with that.


The example

\documentclass{article}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{tabularray}

\newcommand\rot[1]{\rotatebox{90}{#1}} \newcommand\Tr[1]{\begin{tabular}[t]{@{}c@{}}#1\end{tabular}} \colorlet{cellbg}{black!10} \newlength\heavyline \setlength{\heavyline}{0.12em} \newlength\lightline \setlength{\lightline}{0.08em}

\begin{document}

\begin{table}[h!] \begin{tblr}{ width=\textwidth, colspec = {| Q | Q | X[3,c,h] | X[3,c,h] | X[3,c,h] |}, hline{1,6} = {\heavyline}, hline{3,5} = {\lightline}, vline{1,6} = {\heavyline}, vline{3,5} = {\lightline}, cell{1}{1} = {r=2,c=2}{bg=cellbg}, cell{3}{1} = {r=2}{cmd=\textbf}, cell{1}{5} = {r=2}{bg=cellbg}, cell{5}{1} = {c=2}{bg=cellbg}, cell{5}{5} = {bg=cellbg}, cell{3-4}{3-4} = {cmd=\rule{0pt}{18pt},c,h}, cells = {font=\small}, } && \SetCell[c=2]{cmd=\textbf} Prediction / Classification && \ \hline && positive & negative & \ \rot{\Tr{True condition\gold standard}} & \rot{\Tr{positive} } & True positive, TP & False negative, FN & {Sensitivity\TP/(TP + FN)} \ \hline & \rot{\Tr{negative} } & False positive, FP & True negative, TN & {Specificity\TN/(TN + FP)} \ && {Positive\Predictive Value\TP/(TP + FP)} & {Negative\Predictive Value\TN/(TN + FN)} & \ \end{tblr} \end{table}

\end{document}

Celdor
  • 9,058