1

I am making a table with some text underneath the columns. I'd like to make it a figure, but when I do so, the text underneath the columns collides with the Figure label. See the attached photo

enter image description here

Code below:

\documentclass[12pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{pifont}
\usepackage[left=2.00cm, right=2.00cm, top=2.00cm, bottom=2.00cm]{geometry}
\begin{document}

        \begin{figure}[t]
            \centering
            % need the pifont package
            \begin{tabular}{l|l|l|l|l}
                %\begin{tabular}{l|l|l|l|l}                                                                  
                \multicolumn{2}{c}{}&\multicolumn{2}{c}{True Label}&\\
                \cline{3-4}
                \multicolumn{2}{c|}{}&\multicolumn{1}{c|}{Positive}&\multicolumn{1}{c|}{Negative}&\multicolumn{1}{c}{}\\
                %\cline{2-4}                                                                                 
                \hhline{~|---}
                \multirow{2}{*}{Predicted Label}& Positive & TP & FP ($\alpha$) &\ding{214} PPV\\
                %\cline{2-4}                                                                                 
                \hhline{~|---}
                & Negative & FN ($\beta$) & TN & \ding{214} NPV\\
                %\cline{2-4}                                                                                 
                \hhline{~|---}
                \multicolumn{1}{c}{} & \multicolumn{1}{c}{} &
                \multicolumn{1}{c}{\multirow{2}{*}{\parbox{2em}{\vskip1ex\ding{215}\\ Se}}} & \multicolumn{1}{c}{\multirow{2}{*}{\parbox{2em}{\vskip1ex\ding{215}\\ Sp}}} &\multicolumn{1}{c}{}\\
            \end{tabular}
            \caption{Confusion Matrix}{A typical confusion matrix for a binary classifcation problem.}
            \label{confusionmatrix}

        \end{figure}


\end{document}
1

Although this is not an answer to your question, your example suggests that some macros I use may help you. They produce two way contingency tables like

enter image description here

The code is not as pretty or generic as it might be, but it is all I needed. (I'm sure I found most of the ideas in an answer on this site, which would surely have upvoted at the time - but I can't find the link now to put here.)

In the preamble:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Table packages
%
\usepackage{multirow}
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\usepackage{booktabs}% http://ctan.org/pkg/booktabs

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% formatting contingency tables
% 
\newcommand{\thecondition}{to be renewed}
\newcommand{\theyes}{to be renewed}
\newcommand{\theno}{to be renewed}
\newlength{\tempdima}

\newcommand{\ctablehead}[3]{%
   \settowidth{\tempdima}{#1}% compute width needed
   \addtolength{\tempdima}{-\arraycolsep}% minus default column sep
   \renewcommand{\thecondition}{#1}
   \renewcommand{\theyes}{#2}
   \renewcommand{\theno}{#3}
}
\newcommand{\ctablebody}{%
\cline{3-5}
& &   \multicolumn{2}{c|}{\thecondition} & \\ 
& &   {\makebox[0.5\tempdima]{\theyes}} 
    & {\makebox[0.5\tempdima]{\theno}} 
    & {total} \\
\hline
}

In the document:

\ctablehead{chronic fatigue syndrome}{yes}{no}
{\renewcommand{\arraystretch}{1.2}% for the vertical padding
\begin{tabular}{cc
|
S[table-format=3.0]
S[table-format=5.0]
|
S[table-format=6.0]
|
}
\ctablebody
\multicolumn{1}{|c}{\multirow{2}{*}{tested positive}} &
\multicolumn{1}{c|}{yes} &  203  &   6802  & 7005 \\
\multicolumn{1}{|c}{}        &                
\multicolumn{1}{c|}{no} &  32  &  92963 & 92885 \\
\hline
\multicolumn{1}{|c}{}
 & total & 235 & 99765 & 100000 \\
\hline
\end{tabular}
}
| improve this answer | |
  • This is beautiful! A much better version than my own! – Demetri Pananos Jul 28 '16 at 1:46
4

The main error is your multirow which are spanning past the end of the table.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{pifont}
\usepackage[left=2.00cm, right=2.00cm, top=2.00cm, bottom=2.00cm]{geometry}
\begin{document}

        \begin{figure}[t]
            \centering
            % need the pifont package
            \begin{tabular}{l|l|l|l|l}
                %\begin{tabular}{l|l|l|l|l}                                                                  
                \multicolumn{2}{c}{}&\multicolumn{2}{c}{True Label}&\\
                \cline{3-4}
                \multicolumn{2}{c|}{}&\multicolumn{1}{c|}{Positive}&\multicolumn{1}{c|}{Negative}&\multicolumn{1}{c}{}\\
                %\cline{2-4}                                                                                 
                \hhline{~|---}
                \multirow{2}{*}{Predicted Label}& Positive & TP & FP ($\alpha$) &\ding{214} PPV\\
                %\cline{2-4}                                                                                 
                \hhline{~|---}
                & Negative & FN ($\beta$) & TN & \ding{214} NPV\\
                %\cline{2-4}
                \hhline{~|---}                                                                     
                \multicolumn{2}{c}{\rule{0pt}{15pt}}&\multicolumn{1}{c}{\ding{215}}& \multicolumn{1}{c}{\ding{215}} &\\
                \multicolumn{2}{c}{}&\multicolumn{1}{c}{se}& \multicolumn{1}{c}{sp} &\\
            \end{tabular}
            \caption{Confusion Matrix}{A typical confusion matrix for a binary classifcation problem.}
            \label{confusionmatrix}

        \end{figure}


\end{document}
| improve this answer | |
1

The problem lies in your \multirow statements. They're meant to combine multiple rows, not to split an existing single row in multiple lines. If you use \parbox instead, you'll be fine.

\documentclass[12pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{pifont}
\usepackage[left=2.00cm, right=2.00cm, top=2.00cm, bottom=2.00cm]{geometry}
\begin{document}

        \begin{figure}[t]
           \centering
           % need the pifont package
           \begin{tabular}{l|l|l|l|l}
           %\begin{tabular}{l|l|l|l|l}                                                                  
           \multicolumn{2}{c}{}&\multicolumn{2}{c}{True Label}&\\
           \cline{3-4}
           \multicolumn{2}{c|}{}&\multicolumn{1}{c|}{Positive}&\multicolumn{1}{c|}{Negative}&\multicolumn{1}{c}{}\\
           %\cline{2-4}                                                                                 
           \hhline{~|---}
           \multirow{2}{*}{Predicted Label}& Positive & TP & FP ($\alpha$) &\ding{214} PPV\\
           %\cline{2-4}                                                                                 
           \hhline{~|---}
           & Negative & FN ($\beta$) & TN & \ding{214} NPV\\
           %\cline{2-4}                                                                                 
           \hhline{~|---}
           \multicolumn{2}{c}{}  &
           \multicolumn{1}{c}{\parbox{1em}{\ding{215}\\ Se}} & \multicolumn{1}{c}{\parbox{1em}{\ding{215}\\ Sp}}\\
           \end{tabular}
           \caption{Confusion Matrix}{A typical confusion matrix for a binary classifcation problem.}
           \label{confusionmatrix}

        \end{figure}


\end{document}
| improve this answer | |

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.