3

I am having a problem with fitting a table on the page - the obvious problems are the long(ish) column titles, as well as the "Confidence Prediction" column.

enter image description here

\documentclass[11pt,a4paper,titlepage]{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[!htbp] \centering 
\caption{Confusion Matrix - Logistic Regression.} 
\label{tab1} 
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}lSSlSSl} 
\toprule 
& \multicolumn{3}{c}{Model 1 (No Regularisation)} & \multicolumn{3}{c}{Model 2 (With Regularisation)} \\
\cmidrule(lr){2-4} 
\cmidrule(lr){5-7}
& 0 & 1 & Correct Predictions (\%) & 0 & 1 & Correct Predictions (\%) \\
\midrule \\
0 & 414  & 70 & 85.5 & 419 & 65 & 86.4\\ 
\addlinespace
1 & 67 & 420 & 86.2 & 58 & 429 & 88.1 \\
\midrule
\textbf{Average} &  &  & \textbf{85.9} &  & & \textbf{87.3} \\
\bottomrule \\
\end{tabular*} 
\end{table} 
5

Here is my suggestion that is based on splitting too long column titles into two lines and using the table-format option to the S columns.

\documentclass[11pt,a4paper,titlepage]{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{makecell}

\usepackage{etoolbox}
\robustify\bfseries

\begin{document}
\begin{table}[!htbp] \centering 
\caption{Confusion Matrix - Logistic Regression.} 
\label{tab1} 
\begin{tabular}{l
                S[table-format=3]
                S[table-format=3]
                S[table-format=2.1,detect-weight]
                S[table-format=3]
                S[table-format=3]
                S[table-format=2.1,detect-weight]} 
\toprule 
& \multicolumn{3}{c}{\makecell{Model 1 \\(No Regularisation)}} & \multicolumn{3}{c}{\makecell{Model 2 \\ (With Regularisation)}} \\
\cmidrule(lr){2-4} 
\cmidrule(lr){5-7}
& 0 & 1 & {\makecell{Correct\\ Predictions (\%)}} & 0 & 1 & {\makecell{Correct\\ Predictions (\%)}} \\
\midrule 
0 & 414  & 70 & 85.5 & 419 & 65 & 86.4\\ 
\addlinespace
1 & 67 & 420 & 86.2 & 58 & 429 & 88.1 \\
\midrule
\bfseries Average &  &  & \bfseries 85.9 &  & & \bfseries 87.3 \\
\bottomrule \\
\end{tabular} 
\end{table} 

\end{document}

enter image description here

  • In order to split the column headings into two lines, I used makecell from the eponymous package.
  • For a better formatting of the S colunns, I used the appropriate values for the table-format option. Additionally, I used S columns for the 4th and 7th column as well.
  • In order to correct the alignment of the bold numbers in the last row, I used etoolbox's \robustify command in combination with \bfseries and the detect-weight option of the S columns.
| improve this answer | |
  • What's the advantage to using the S column type instead of the r column type for columns 2, 3, 5, and 6? – Mico Apr 15 '18 at 20:02
  • @Mico, it is just a MWE... Could be needed like this and thus OP gave this MWE – koleygr Apr 15 '18 at 20:04
  • @Mico: Considering the case that the table in the MWE is already complete, there is indeed no disadvantage in using an r column instead of the S columns. Since the OP already used S columns in their example, I wanted to point out the usage of table-format that can help to get rid of unnecessary horizontal space. – leandriis Apr 15 '18 at 20:11
4

Here's a solution that employs a tabularx environment, to allow automatic line-wrapping in the headers of columns 4 and 7. A centered version of the X column type is used for these two columns.

enter image description here

\documentclass[11pt,a4paper,titlepage]{article}
\usepackage{booktabs,tabularx,ragged2e,caption}
\newcolumntype{C}{>{\Centering\arraybackslash}X} % centered version of 'X' col.
\begin{document}
\begin{table}[!htbp]
\captionsetup{skip=0.333\baselineskip}
\caption{Confusion Matrix -- Logistic Regression.}  \label{tab1} 
\begin{tabularx}{\linewidth}{@{} l *{2}{rrC} @{}} 
\toprule 
& \multicolumn{3}{c}{Model 1} 
& \multicolumn{3}{c}{Model 2} \\
& \multicolumn{3}{c}{(No Regularisation)}
& \multicolumn{3}{c}{(With Regularisation)}\\
\cmidrule(lr){2-4} \cmidrule(l){5-7}
& 0 & 1 & \% Correct Predictions
& 0 & 1 & \% Correct Predictions \\
\midrule 
0 & 414  & 70 & 85.5 & 419 & 65 & 86.4\\ 
\addlinespace
1 & 67 & 420 & 86.2 & 58 & 429 & 88.1 \\
\midrule
\textbf{Average} &&& \textbf{85.9} &&& \textbf{87.3} \\
\bottomrule
\end{tabularx} 
\end{table} 
\end{document}

Addendum: If you don't need the table to span the full width of the textblock, a much more compact (and, in my opinion, better-looking) solution, which uses just the "standard" column types l, c, and r, is available:

enter image description here

\documentclass[11pt,a4paper,titlepage]{article}
\usepackage{booktabs,caption}
\begin{document}
\begin{table}[!htbp]
\centering
\captionsetup{skip=0.333\baselineskip}
\caption{Confusion Matrix -- Logistic Regression.} 
\label{tab1} 
\begin{tabular}{@{} l *{2}{rrc} @{}} 
\toprule 
& \multicolumn{3}{c}{Model 1} 
& \multicolumn{3}{c}{Model 2} \\
& \multicolumn{3}{c}{(No Regularisation)}
& \multicolumn{3}{c}{(With Regularisation)} \\
\cmidrule(lr){2-4} \cmidrule(l){5-7}
& 0 & 1 & \% Correct & 0 & 1 & \% Correct \\
&&& Predictions &&& Predictions \\
\midrule 
0 & 414 &  70 & 85.5 & 419 &  65 & 86.4\\ 
\addlinespace
1 &  67 & 420 & 86.2 &  58 & 429 & 88.1 \\
\midrule
\textbf{Average} &&& \textbf{85.9} &&& \textbf{87.3} \\
\bottomrule
\end{tabular} 
\end{table} 
\end{document}
| improve this answer | |
2

I haven't siunitx on my system and used other columntypes... Feel free to use whatever you want (for example yours).

The idea is to break some cells every where needed.

(I used this answer: https://tex.stackexchange.com/a/19678/120578)

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{lipsum}
\newcommand{\specialcell}[2][c]{%
  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
\begin{document}
\lipsum[1-2]
\begin{table}[!htbp] \centering 
\caption{Confusion Matrix - Logistic Regression.} 
\label{tab1} 
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}lrrrrrr} 
\toprule 
& \multicolumn{3}{c}{\specialcell{Model 1\\ (No Regularisation)}} & \multicolumn{3}{c}{\specialcell{Model 2 \\(With Regularisation)}} \\
\cmidrule(lr){2-4} 
\cmidrule(lr){5-7}
& 0 & 1 & \specialcell{Correct\\ Predictions (\%)} & 0 & 1 & \specialcell{Correct\\ Predictions (\%)} \\
\midrule \\
0 & 414  & 70 & 85.5 & 419 & 65 & 86.4\\ 
\addlinespace
1 & 67 & 420 & 86.2 & 58 & 429 & 88.1 \\
\midrule
\textbf{Average} &  &  & \textbf{85.9} &  & & \textbf{87.3} \\
\bottomrule \\
\end{tabular*} 
\end{table} 
\end{document}

Output:

enter image description here

| improve this answer | |
  • If this answers your question, then this is possibly a dupplicate of the linked... Also wait for other answers (if not closed)... because I have seen many times improvements and advises of any kind in table questions... – koleygr Apr 15 '18 at 19:48
  • The 4th and 7th columns look a bit out of whack. Since the header cells ("Correct" and "Predictions (%)") are centered, you may want to use c rather than r as the column type for these two columns. – Mico Apr 15 '18 at 21:18
  • 1
    @Mico. I just gave the way... and asked to keep or change his own colimntypes... I didnt select better than his (OPs) but rather random... – koleygr Apr 16 '18 at 5:18

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.