Latex table multicolumn values are not centerd - latex

I'm trying to create a table with the following layout:
This is what I was able to do so far, as you can see there is too much space in my multicolumns(circled in red)
My code is:
\begin{table*}
\caption{Different sampling strategies and the corresponding PRF scores,
explain the scores!}
\label{table:PRF_scores}
\centering
\begin{tabular}{c c | c c | l l l l } % 7 columns
\hline\hline
\multicolumn{2}{c}{\emph{sampling strategy}} & \multicolumn{2}{c}{} &
\multicolumn{4}{c}{\emph{performance metrics}} \\
under\_sample & over\_sample & N_{memb} & N_{non-memb} & F1(\%) &
ROC\_AUC(\%) PR\_AUC(\%) & MCC(\%)\\
\hline
0.5 & 1.0 & 752 & 752 & 98.54 \pm 0.62 & 98.62 \pm 0.64 & 98.62 \pm 0.59
& 97.08 \pm 1.25\\
0.1 & 1.0 & 3760 & 3760 & 99.09 \pm 0.17 & 99.08 \pm 0.18 & 99.09 \pm
0.17 & 98.17 \pm 0.35\\
0.06 & 0.6 & 3759 & 6266 & 98.68 \pm 0.23 & 99.19 \pm 0.15 & 98.70 \pm
0.23 & 97.89 \pm 0.37\\
\hline
\end{tabular}
\end{table*}
What can I do to fix this issue?
Any help is greatly appreciated!

I propose a "re-designed" table.
First, I would avoid vertical bars, which IMO are rather distraction. Then, I moved group of N to the left; it doesn't seem to be a part of the other two groups and shouldn't be in the middle, does it? I also reduced wording in a few columns and added line breaks in long headers. This is one way to avoid overly wide and unbalanced tables.
Here is a list of helpful packages I applied to help me to format your table:
array define column groups and additional column types
makecell format cells and allows for line breaks, which I haven't had to use here
threeparttable splits a table into three parts: caption, body, and annotation; also, keeps a table and its caption within boundaries
caption adds options to format a caption
siunitx formats and align numbers
booktabs adds improved horizontal rules
Here's the screenshot
and the code
\documentclass{article}
\usepackage{array}
\usepackage{threeparttable}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{caption}
\usepackage{siunitx}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\begin{table}[tbh]
\small
% Settings for the caption
\captionsetup{position=top, skip=3pt, format=hang}
% Reduced horizontal spaces between columns
\setlength\tabcolsep{3pt}
% Slightly increased vertical spacing
\renewcommand{\arraystretch}{1.2}
% Settings for makecell
\renewcommand{\theadfont}{\small\bfseries\itshape}
\renewcommand{\theadgape}{\Gape[0pt][0pt]}
\renewcommand{\cellgape}{\Gape[3pt][0pt]}
%
\centering
\begin{threeparttable}
\caption{Different sampling strategies and the corresponding PRF scores, explain the scores!}
\label{table:PRF_scores}
\begin{tabular}{
*2{S[table-format=4]}
#{\quad}
*2{S[table-format=1.2]}
*4{C{1.8cm}}
}
\toprule
\multicolumn{2}{c}{} & \multicolumn{2}{c}{\thead{Strategy}} & \multicolumn{4}{c}{\thead{Performance metrics}} \\
\cmidrule{3-4}\cmidrule(l){5-8}
\multicolumn{2}{c}{N$^{*}$} & \multicolumn{2}{c}{Sample} & F1 & ROC & PR & MCC \\[-3pt]
{mem} & {n-mem} & {under} & {over} & (\%) & AUC (\%) & AUC (\%) & (\%) \\
\midrule
752 & 752 & 0.5 & 1.0 & 98.54 \pm 0.62 & 98.62 \pm 0.64 & 98.62 \pm 0.59 & 97.08 \pm 1.25 \\
3760 & 3760 & 0.1 & 1.0 & 99.09 \pm 0.17 & 99.08 \pm 0.18 & 99.09 \pm 0.17 & 98.17 \pm 0.35\\
3759 & 6266 & 0.06 & 0.6 & 98.68 \pm 0.23 & 99.19 \pm 0.15 & 98.70 \pm 0.23 & 97.89 \pm 0.37\\
\bottomrule
\multicolumn{8}{#{}l#{}}{%
\makecell[l]{\footnotesize$^{*}$ The left column represents members of a group; the right
column the opposite}}
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}

Related

How to top align a table in a table LaTeX

so i started working with latex and i'm trying to fit a table with statistical content into another table for better alignment.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx,dashrule}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage[a4paper,left=2cm,right=2cm,top=1cm,bottom=1cm]{geometry}
% Document metadata
\title{Statistische Auswertung}
\author{Automatisch Generiert}
\date{2023-02-09 16:24:04}
\begin{document}
% Dokumententitel und Datum generieren
\maketitle
\begin{tabularx}{\textwidth}{|X|X|}
{\begin{tabularx}{\linewidth}{|X|X|}
\hline
age & --- \\
count & 343.0 \\
mean & 60.8 \\
std & 12.1 \\
min & 28.0 \\
25\% & 53.0 \\
50\% & 60.0 \\
75\% & 70.0 \\
max & 89.0 \\
\hline
\end{tabularx}} & \frame{\includegraphics[width=\linewidth]{age.png}} \\
\hline
{\begin{tabularx}{\linewidth}{|X|X|}
\hline
status-fu & --- \\
count & 356 \\
unique & 2 \\
top & False \\
freq & 297 \\
\hline
\end{tabularx}} & \frame{\includegraphics[width=\linewidth]{status_fu.png}} \\
\hline
\end{tabularx}
\end{document}
The Output lools like this:
I want the Sub-tables on the left to be center top aligned with the Images on the right.
Any help???
Cheers!

align values in latex

i have a problem in LaTeX. I want the accuracy value centered in its cell since it is not associated with the labels but I don't know how to do it.
Here the code I'm using:
\documentclass[12 pt, letterpaper, twoside, openright]{book}
\usepackage{amsmath}
\usepackage{multirow}
\usepackage{enumitem}
\usepackage{booktabs, makecell, longtable}
\usepackage{stackengine}
\usepackage{tabularx}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\begin{table}[H]
\caption {Logistic Regression and Random Forest Performances}
\centering
\begin{tabular}{llssssssss}
\toprule
\multirow{2}{*}{Patient} &\multirow{2}{*}{Labels} &
\multicolumn{4}{c}{Logistic Regression} &
\multicolumn{4}{c}{Random Forest} \\
&& {Accuracy (\%)}&{$Prec$} & {$Rec$} & {$F1$} & {Accuracy (\%)}& {$Prec$} & {$Prec$} & {$F1$} \\
\midrule
\multirow{2}{3em}{F0110} & 0 & 55 & 0.59 & 0.62 & 0.61 & 55& 0.70 & 0.76 & 0.72 \\
& 1 & & 0.58 & 0.55 & 0.57& & 0.72 & 0.66 & 0.69 \\
\hline
\bottomrule
\end{tabular}
\label{tab:tab1}
\end{table}
\end{document}
you can use the same \multirow command as with the "FO110" cell
\documentclass[12 pt, letterpaper, twoside, openright]{book}
\usepackage{amsmath}
\usepackage{multirow}
\usepackage{enumitem}
\usepackage{booktabs, makecell, longtable}
\usepackage{stackengine}
\usepackage{tabularx}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\begin{table}[ht]
\caption {Logistic Regression and Random Forest Performances}
\centering
\begin{tabular}{llcccccccc}
\toprule
\multirow{2}{*}{Patient} &\multirow{2}{*}{Labels} &
\multicolumn{4}{c}{Logistic Regression} &
\multicolumn{4}{c}{Random Forest} \\
&& {Accuracy (\%)}&{$Prec$} & {$Rec$} & {$F1$} & {Accuracy (\%)}& {$Prec$} & {$Prec$} & {$F1$} \\
\midrule
\multirow{2}{3em}{F0110} & 0 & \multirow{2}{1em}{55} & 0.59 & 0.62 & 0.61 & \multirow{2}{1em}{55}& 0.70 & 0.76 & 0.72 \\
& 1 & & 0.58 & 0.55 & 0.57& & 0.72 & 0.66 & 0.69 \\
\hline
\bottomrule
\end{tabular}
\label{tab:tab1}
\end{table}
\end{document}
As said in your previous post, the tabularray packages makes merging cells much easier:
\documentclass[12 pt, letterpaper, twoside, openright]{book}
\usepackage{float}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{table}[H]
\caption{Logistic Regression and Random Forest Performances}
\centering
\begin{tblr}{llcccccccc}
\toprule
\SetCell[r=2]{} Patient &
\SetCell[r=2]{} Labels &
\SetCell[c=4]{} Logistic Regression &&&&
\SetCell[c=4]{} Random Forest &&& \\
&& {Accuracy (\%)}&{$Prec$} & {$Rec$} & {$F1$} & {Accuracy (\%)}& {$Prec$} & {$Prec$} & {$F1$} \\
\midrule
\SetCell[r=2]{} F0110 & 0 & \SetCell[r=2]{}55 & 0.59 & 0.62 & 0.61 & \SetCell[r=2]{}55& 0.70 & 0.76 & 0.72 \\
& 1 & & 0.58 & 0.55 & 0.57& & 0.72 & 0.66 & 0.69 \\
\bottomrule
\end{tblr}
\label{tab:tab1}
\end{table}
\end{document}

Manage the multiple row in Overleaf

I have made a table with multiple columns and rows arguments like this
\begin{table}[H]
\centering
\begin{tabular}{lccccc}
\hline
\multirow{2}{*}{Aspects} & Probability & \multicolumn{4}{c}{Methods} \\
\cline{3-6}
& for DE Genes & Binomial Test & Tangram & stereoscope & BayesPrism\\
\hline
\multirow{3}{*}{AUC} & 0.3 & 0.867 & 0.029 & 0.074 & 0.992 \\
& 0.1 & 0.722 & 0.023 & 0.015 & 0.987\\
& 0.05 & 0.512 & 0.055 & 0.025 & 0.972\\
\hline
\multirow{3}{*}{Best Thresholds} & 0.3 & ${0.135$ & 0.029 & 0.072 & $1e-10$ \\
& 0.1 & ${1e-60$ & 0.026 & 0.065 & $1e-10$\\
& 0.05 & $1e-60$ & 0.033 & 0.069 & $1e-10$\\
\hline
\end{tabular}
\caption{The ROC Curve Summary for Data with 15 Clusters}
\label{15 cluster summary}
\end{table}
It will give the results like this
I want to make the entries "Best Thresholds" be in two rows (filling 3 rows arguments) so that the table is not too wide. Could anyone helps how to make it?
You can place the text in a \parbox:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabular}{lccccc}
\hline
\multirow{2}{*}{Aspects} & Probability & \multicolumn{4}{c}{Methods} \\
\cline{3-6}
& for DE Genes & Binomial Test & Tangram & stereoscope & BayesPrism\\
\hline
\multirow{3}{*}{AUC} & 0.3 & 0.867 & 0.029 & 0.074 & 0.992 \\
& 0.1 & 0.722 & 0.023 & 0.015 & 0.987\\
& 0.05 & 0.512 & 0.055 & 0.025 & 0.972\\
\hline
\multirow{3}{*}{\parbox{2cm}{\raggedright Best Thresholds}} & 0.3 & $0.135$ & 0.029 & 0.072 & $1e-10$ \\
& 0.1 & $1e-60$ & 0.026 & 0.065 & $1e-10$\\
& 0.05 & $1e-60$ & 0.033 & 0.069 & $1e-10$\\
\hline
\end{tabular}
\caption{The ROC Curve Summary for Data with 15 Clusters}
\label{15 cluster summary}
\end{table}
\end{document}

How to remove middle horizontal line in a table in Overleaf

I have a table in Overleaf. I want to remove the horizontal line (crossing the number 0.3). I know I can use \cline{} command to remove some horizontal lines, but I do not know how to use the combination of them.
\begin{tabular}{l|c|c|c|c|c}
\hline
\multirow{2}{*}{Aspects} & Probability & \multicolumn{4}{c}{Methods} \\
\cline{3-6}
& for DE Genes & Binomial Test & Tangram & stereoscope & BayesPrism\\
\cline{3-6}
AUC & \multirow{2}{*}{0.3} & 0.519 & -0.261 & -0.020 & 0.996 \\
\cline{1-2}
\cline{3-6}
Best thresholds & &$0.398$ & $0.019$ & 0.204 & ${\rm e}^{-50}$\\
\hline
\end{tabular}
\caption{The ROC Curve Summary for Data with 5 Clusters with Probability 0.05 for DE
Genes}
\label{5 cluster 0.05}
\end{table}
The table looks like this.
If you want the line only in the first column, use \cline{1-1} instead of \cline{1-2} which will draw it over the first two columns.
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{l|c|c|c|c|c}
\hline
\multirow{2}{*}{Aspects} & Probability & \multicolumn{4}{c}{Methods} \\
\cline{3-6}
& for DE Genes & Binomial Test & Tangram & stereoscope & BayesPrism\\
\cline{3-6}
AUC & \multirow{2}{*}{0.3} & 0.519 & -0.261 & -0.020 & 0.996 \\
\cline{1-1}
\cline{3-6}
Best thresholds & &$0.398$ & $0.019$ & 0.204 & ${\rm e}^{-50}$\\
\hline
\end{tabular}
\end{document}

Wrong column spacing due to multicolumn in latex

in the LaTeX table below, the third column gets too way much spacing due to the long \multicolumn cell in the last line. Can anyone think of a way of getting LaTeX to distribute the additional spacing needed evenly across columns?
\documentclass[11pt]{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabular}{lrrr}
\toprule
& ICE-GB & ICE-SING & ICE-EA \\
\midrule
NP & 619 & 595 & 496 \\
\textbf{Arithmetic mean}& \textbf{1.36} & \textbf{1.33} & \textbf{1.22} \\
SD&0.73 & 0.71 & 0.53 \\
Variation coefficient& 0.54 & 0.52 & 0.44 \\
\midrule
Kruskal Wallis rank sum test&\multicolumn{3}{l}{$H = 16.3941, df = 2, p= 0.0002755123$} \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Thanks!
Excess width \multicolumns end up assigning the excess to the last column. You can avoid this by either using a fixed-width \multicolumn - that is, use a p{<len>} column, or for even spreading of columns use a fixed-width column for the non-\multicolumn columns:
\documentclass[11pt]{article}
\usepackage{booktabs,array}
\begin{document}
\noindent
\begin{tabular}{l*{3}{>{\raggedleft\arraybackslash}p{5em}}}
\toprule
& ICE-GB & ICE-SING & ICE-EA \\
\midrule
NP & 619 & 595 & 496 \\
\textbf{Arithmetic mean} & \textbf{1.36} & \textbf{1.33} & \textbf{1.22} \\
SD & 0.73 & 0.71 & 0.53 \\
Variation coefficient & 0.54 & 0.52 & 0.44 \\
\midrule
Kruskal Wallis rank sum test & \multicolumn{3}{r}{$H = 16.3941, df = 2, p= 0.0002755123$} \\
\bottomrule
\end{tabular}
\end{document}
In the above example, the last three columns each have width 5em, and are \raggedleft (similar to an r-column specification).

Resources