align values in latex - 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}

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!

Centering the latex table in the page

I have the following table in LaTeX, and although I have used the \centering in the code, it is not centred when knitted. In addition it extends more than the text width as shown in the figure below
\begin{table}[H]
\centering
\caption[mnodels summary]{Summary results of the models \label{tab:modelssummary}}
\begin{tabular}{cccccc}
\toprule
\multicolumn{6}{c}{~ \textbf{~~Models Performance }} \\
\hline
\textbf{Model} & \textbf{Residual Mean} & \textbf{Constant Variance} & \textbf{Autocorrelation} & \textbf{Adj. R-Squared} & \textbf{AIC} \\
\hline\hline
\textbf{Model 1} & OK & OK & YES & 0.83 & 4804.8 \\
\textbf{Model 2} & OK & OK & YES & 0.84 & 4569.5 \\
\textbf{Model 3} & OK & OK & YES & 0.83 & 4763.7 \\
\textbf{Model 4} & OK & OK & YES & 0.87 & 3760.5 \\
\textbf{Model 5} & OK & OK & YES & 0.86 & 3964.5 \\
\textbf{Model 6} & No & No & YES & 0.32 & 10879.7~ \\
\textbf{Model 7} & No & No & YES & 0.80 & 5434.2 \\
\bottomrule
\end{tabular}
\end{table}
and it will look like this:
How can I resolve this?
If you accept multi lines in headings and abbreviations, you can squeeze the table and even fit within the default latex page. Then, \centering will work
and the code
\documentclass{article}
\usepackage{booktabs}
\usepackage{caption}
\captionsetup[table]{position=top,skip=6pt}
\usepackage{makecell}
\usepackage{ragged2e} % Centering
\usepackage{siunitx} % S[...]
% Makecell - settings
\renewcommand\theadfont{\normalsize\normalfont\bfseries}
\renewcommand\theadalign{tc}
% Custom column types
\newcolumntype{C}{>{\Centering\bfseries}p{1.4cm}}
\newcolumntype{P}[1]{>{\Centering}p{#1}}
\begin{document}
\begin{table}[tbh]
\centering
\setlength\tabcolsep{0pt}
\caption[mnodels summary]{Summary results of the models}\label{tab:modelssummary}
\begin{tabular}{
C #{\hspace{1em}}
*4{P{1.25cm}}
#{\hspace{0.75em}}
S[group-minimum-digits=4,table-format=4.1]
}
\toprule
& \multicolumn{5}{c}{\textbf{Models Performance}} \\
\cmidrule{2-6}
\thead{Model}
& \thead{Resid.\\Mean}
& \thead{Const.\\Var.}
& \thead{Auto\\corr.}
& \thead{Adj.\\R\textsuperscript{2}}
& {\thead{AIC}} \\ % {} required for S[] to work
\midrule\midrule
1 & OK & OK & YES & 0.83 & 4804.8 \\
2 & OK & OK & YES & 0.84 & 4569.5 \\
3 & OK & OK & YES & 0.83 & 4763.7 \\
4 & OK & OK & YES & 0.87 & 3760.5 \\
5 & OK & OK & YES & 0.86 & 3964.5 \\
6 & No & No & YES & 0.32 & 10879.7 \\
7 & No & No & YES & 0.80 & 5434.2 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

Add custom left spacing in a single LaTeX table's cell

I need to add some left spacing in a table's cell. Can anyone help me?
It follows a code sample; I need to add left spacing in the cells of the first colum where the label is not bold.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{graphics}
\begin{document}
{
\begin{table}[ht]
\centering
\setlength{\tabcolsep}{2pt}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{|p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering\arraybackslash}p{0.15\textwidth}|}
\hline
& \textbf{VRE} & \textbf{FPE} & \textbf{MPE} & \multicolumn{2}{c|}{\textbf{Full sample}} \\
& \textit{n} & \textit{n} & \textit{n} & \textit{n} & \textit{\%} \\
\hline
\textbf{Gender} &&&&& \\
Female & 12 & 10 & 18 & 40 & 52.6 \\
Male & 10 & 13 & 13 & 36 & 47.4 \\
\hline
\textbf{Educational level} &&&&& \\
High School & 8 & 5 & 6 & 19 & 25.0 \\
Bachelor's Degree & 13 & 9 & 19 & 41 & 53.9 \\
Master's Degree & 1 & 8 & 6 & 15 & 19.7 \\
PhD & 0 & 1 & 0 & 1 & 1.3 \\
\hline \hline
\textbf{Age} &&&&& \\
Mean & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\
Median & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\
\hline
\end{tabular}
\caption{Participants demographic information}
\label{tab:demographic}
\end{table}
}
Thanks in advance.
One possibility: you can place \quad (or similar spacing macros) at the start of these cells:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{graphics}
\begin{document}
{
\begin{table}[ht]
\centering
\setlength{\tabcolsep}{2pt}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{|p{0.26\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering\arraybackslash}p{0.15\textwidth}|}
\hline
& \textbf{VRE} & \textbf{FPE} & \textbf{MPE} & \multicolumn{2}{c|}{\textbf{Full sample}} \\
& \textit{n} & \textit{n} & \textit{n} & \textit{n} & \textit{\%} \\
\hline
\textbf{Gender} &&&&& \\
\quad Female & 12 & 10 & 18 & 40 & 52.6 \\
\quad Male & 10 & 13 & 13 & 36 & 47.4 \\
\hline
\textbf{Educational level} &&&&& \\
\quad High School & 8 & 5 & 6 & 19 & 25.0 \\
\quad Bachelor's Degree & 13 & 9 & 19 & 41 & 53.9 \\
\quad Master's Degree & 1 & 8 & 6 & 15 & 19.7 \\
\quad PhD & 0 & 1 & 0 & 1 & 1.3 \\
\hline \hline
\textbf{Age} &&&&& \\
\quad Mean & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\
\quad Median & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\
\hline
\end{tabular}
\caption{Participants demographic information}
\label{tab:demographic}
\end{table}
}
\end{document}
Unrelated to the problem, but your fixed width columns "waste" a lot of space. I would instead use a tabularx like this:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{graphics}
\begin{document}
{
\begin{table}[ht]
\centering
\setlength{\tabcolsep}{10pt}
\renewcommand{\arraystretch}{1.5}
\begin{tabularx}{\textwidth}{ | X ccccc | }
\hline
& \textbf{VRE} & \textbf{FPE} & \textbf{MPE} & \multicolumn{2}{c|}{\textbf{Full sample}} \\
& \textit{n} & \textit{n} & \textit{n} & \textit{n} & \textit{\%} \\
\hline
\textbf{Gender} &&&&& \\
\quad Female & 12 & 10 & 18 & 40 & 52.6 \\
\quad Male & 10 & 13 & 13 & 36 & 47.4 \\
\hline
\textbf{Educational level} &&&&& \\
\quad High School & 8 & 5 & 6 & 19 & 25.0 \\
\quad Bachelor's Degree & 13 & 9 & 19 & 41 & 53.9 \\
\quad Master's Degree & 1 & 8 & 6 & 15 & 19.7 \\
\quad PhD & 0 & 1 & 0 & 1 & 1.3 \\
\hline \hline
\textbf{Age} &&&&& \\
\quad Mean & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\
\quad Median & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\
\hline
\end{tabularx}
\caption{Participants demographic information}
\label{tab:demographic}
\end{table}
}
\end{document}

How can I make the verticle line in the table without any gaps?

The verticle line in the third column has 2 gaps. How do I do I make it look full?
Here's the code:
\begin{table}[H]
\centering
\caption{Enzymatic cocktails and the respective tested concentrations in \%(v/v) used in Combined Hydrolysis}
\label{enzymatic cocktails}
\begin{tabular}{#{}ccc#{}}
\toprule
\textbf{Cocktail} & \textbf{Enzymes} & \textbf{Concentration \%(v/v)} \\ \midrule
\multirow{2}{*}{A} & \multicolumn{1}{c|}{Cellulase} & \multirow{8}{*}{\begin{tabular}[c]{#{}c#{}}0.25\\ 0.50\\ 1.00\end{tabular}} \\
& \multicolumn{1}{c|}{$\boldsymbol{\beta}$-glucosidase} & \\ \cmidrule(r){1-2}
\multirow{3}{*}{B} & \multicolumn{1}{c|}{Cellulase} & \\
& \multicolumn{1}{c|}{$\boldsymbol{\beta}$-glucosidase} & \\
& \multicolumn{1}{c|}{xylanase} & \\ \cmidrule(r){1-2}
\multirow{3}{*}{C} & \multicolumn{1}{c|}{Cellulase} & \\
& \multicolumn{1}{c|}{$\boldsymbol{\beta}$-glucosidase} & \\
& \multicolumn{1}{c|}{$\boldsymbol{\beta}$-glucanase and xylanase} & \\ \bottomrule
\end{tabular}
\end{table}
Vertical lines like in c| and horizontal lines such as toprule, bottomrule, midrule, cmidrule from the package booktabs do not match.
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}%[H]
\centering
\caption{Enzymatic cocktails and the respective tested concentrations in \%(v/v) used in Combined Hydrolysis}
\label{enzymatic_cocktails}
\begin{tabular}{#{}ccc#{}}
\hline
\textbf{Cocktail} & \textbf{Enzymes} & \textbf{Concentration \%(v/v)}\\
\hline
\multirow{2}{*}{A} & \multicolumn{1}{c|}{Cellulase} & %
\multirow{8}{*}{\begin{tabular}[c]{#{}c#{}}0.25\\ 0.50\\ 1.00\end{tabular}}\\
& \multicolumn{1}{c|}{$\mathbb{\beta}$-glucosidase} & \\
\cline{1-2}
\multirow{3}{*}{B} & \multicolumn{1}{c|}{Cellulase} & \\
& \multicolumn{1}{c|}{$\boldsymbol{\beta}$-glucosidase} & \\
& \multicolumn{1}{c|}{xylanase} & \\
\cline{1-2}
\multirow{3}{*}{C} & \multicolumn{1}{c|}{Cellulase} & \\
& \multicolumn{1}{c|}{$\mathbb{\beta}$-glucosidase} & \\
& \multicolumn{1}{c|}{$\mathbb{\beta}$-glucanase and xylanase} & \\
\hline
\end{tabular}
\end{table}
\end{document}
So I went back to hlines in my code. Better would be to use the above mentioned package booktabs and avoid vertical lines instead.

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