Changing dimensions (height and width ) of latex table generated tabularray - latex

I need to make the LaTeX table generated here smaller in height either by changing the total height or each row height, whichever looks better. How can I do that?
Here is the code:
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={lXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
vspan=even,
cell{1}{2}={halign=c,bg=gray,fg=white},
cell{2}{1}={bg=gray,fg=white},
cell{3}{5-6}={bg=lightgray,font=\bfseries},
cell{5}{4-6}={bg=lightgray,font=\bfseries},
cell{6}{3}={bg=lightgray,font=\bfseries},
}
& \SetCell[c=5]{} Type of Distribution & & & &\\
\SetCell[r=5]{} \rotatebox{90}{\parbox{3.5cm}{\centering Type of Transformation}} & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & Y & Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & Y & Y & Y \\
& Logarithmic & Y & Y & N & N \\
\end{tblr}
\end{table}

The extra height comes from the \parbox. If you reduce the width of the \parbox a bit, the rows become less tall.
If you need the rows to be even narrower, you can change the rowsep (but you'll have to ditch the idea of rotated text then).
\documentclass[11pt]{article}
\usepackage{tabularray}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{graphicx}
\begin{document}
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={lXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
vspan=even,
cell{1}{2}={halign=c,bg=gray,fg=white},
cell{2}{1}={bg=gray,fg=white},
cell{3}{5-6}={bg=lightgray,font=\bfseries},
cell{5}{4-6}={bg=lightgray,font=\bfseries},
cell{6}{3}={bg=lightgray,font=\bfseries},
% rowsep = 0mm
}
& \SetCell[c=5]{} Type of Distribution & & & &\\
\SetCell[r=5]{} \rotatebox{90}{\parbox{2.6cm}{\centering Type of Transformation}} & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & Y & Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & Y & Y & Y \\
& Logarithmic & Y & Y & N & N \\
\end{tblr}
\end{table}
\end{document}

Related

Latex table multicolumn values are not centerd

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}

How to build a table in latex that one column can have three subcolumns

I am trying to build a table in Latex that one column which in my case in "Factor Loading" has three subcolumns, and also text in the "Item" column to be wrapped if it too big.
\multicolumn{}{}{} is the way to merge a number of cells but in your case, I would move "Factor loading" to a bottom part with annotations. The table seems to look more balanced without and the information is still there.
Here is the solution with a few improvements
\documentclass{article}
\usepackage{geometry}
\usepackage{tabularx} % Mainly for X-type column. Also loads very useful `array` package
\usepackage{booktabs} % For custom rules: \toprule, \bottomrule, \midrule, \cmidrule and \spacialrule
\usepackage{caption} % For custom formatting of captions
\usepackage{ragged2e} % Adds \RaggedRight and \Centering and improves text typesetting in narrow cells
\usepackage[nopar]{kantlipsum} % Only for summy text - can be removed in final draft
\captionsetup[table]{position=top, skip=1pt} % Adds a small gap between caption and a table
\newcommand\tn[1]{\rlap{\textsuperscript{#1}}} % A custom macro to add an annotation mark
\newcommand\thead[1]{\textbf{#1}} % Formats headings
\renewcommand{\tabularxcolumn}[1]{>{\RaggedRight}p{#1}} % For X formatting. Here, it only adds \RaggedRight
\newcolumntype{F}[1]{>{\Centering}p{#1}} % A custom column-type. Adds centering to default left alignment of p{}
\begin{document}
\begin{table}[tbh]
\renewcommand*{\arraystretch}{1.5} % Stretch a table vertically adding small spacing between rows
\centering
\caption{The table}
\label{tab:table}
\begin{tabularx}{1.0\linewidth}{#{} p{2.2cm} | X | *3{#{}F{1.2cm}} #{}}
\toprule
\multicolumn{1}{#{}p{2.2cm}}{\thead{Construct}} % \multicolumn can also cancel vertical bars around cells
& \multicolumn{1}{c}{\thead{Items}}
& \thead{KR}\tn{*}
& \thead{SG}\tn{*}
& \thead{US}\tn{*} \\
\specialrule{\lightrulewidth}{3pt}{0pt}
Collaborative & \kant[1][1] & 0.798 & 0.814 & 0.878 \\
& \kant[1][2] & 0.749 & 0.874 & 0.838 \\
& \kant[1][3] & 0.865 & 0.878 & 0.924 \\
& \kant[1][4] & 0.893 & 0.834 & 0.820 \\
& \kant[1][5] & 0.910 & 0.86 & 0.898 \\
& \kant[1][6] & 0.770 & 0.903 & 0.921 \\
\specialrule{\heavyrulewidth}{0pt}{0pt}
\multicolumn{5}{#{}l}{\tn{*}\hspace{0.25em} Factor loading}
\end{tabularx}
\end{table}
\end{document}
EDIT.Also, as suggested, tabularray is another alternative. The package gives you very easy interface to actually paint tables. The downside is time of compilation, which is a few times longer than that of regular tables.
Just for fun, here a variation with the tabularray package:
\documentclass{article}
\usepackage{tabularray}
\usepackage{lipsum}
\begin{document}
\begin{table}[tbh]
\caption{The table}
\label{tab:table}
\begin{tblr}{|l|X|c|c|c|}
\hline
Construct & Items & \SetCell[c=3]{} Factor loading &&\\
\hline
& & KR & SG & US \\
\hline
Collaborative & \lipsum[1][1] & 0.798 & 0.814 & 0.878 \\
& \lipsum[1][1] & 0.749 & 0.874 & 0.838 \\
& \lipsum[1][1] & 0.865 & 0.878 & 0.924 \\
& \lipsum[1][1] & 0.893 & 0.834 & 0.820 \\
& \lipsum[1][1] & 0.910 & 0.86 & 0.898 \\
& \lipsum[1][1] & 0.770 & 0.903 & 0.921 \\
\hline
\end{tblr}
\end{table}
\end{document}

Changing latex table text direction?

I want to change the table generated by the following code so that the first column looks like the figure below by 1- changing the text direction in the first column. I made some changes regarding this post but still have a problem.
Desired output:
Current output:
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={XXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
hspan=even,
}
& \SetCell[c=5]{halign=c,bg=gray,fg=white} Type of Distribution & & & &\\
\SetCell[r=5]{bg=gray,fg=white} \parbox[t]{2mm}{\multirow{5}{*}{\rotatebox[origin=c]{90}{Type of \\ Transformation}}} & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & \SetCell{bg=lightgray,font=\bfseries} Y & \SetCell{bg=lightgray,font=\bfseries} Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & \SetCell{bg=lightgray,font=\bfseries} Y & \SetCell{bg=lightgray,font=\bfseries} Y & \SetCell{bg=lightgray,font=\bfseries} Y \\
& Logarithmic & \SetCell{bg=lightgray,font=\bfseries} Y & Y & \SetCell{font=\bfseries} N & \SetCell{font=\bfseries} N \\
\end{tblr}
\end{table}
\begin{table}[H]
\centering
\begin{tabular}{|c|l|r|r|r|r|}
\hline
& \multicolumn{1}{|c|}{Text} & \multicolumn{1}{|c|}{Text} & \multicolumn{1}{|c|}{Text} & \multicolumn{1}{|c|}{Text} & \multicolumn{1}{|c|}{text}\\
\hline
\rotatebox{90}{\parbox{2mm}{\multirow{3}{*}{rota}}} & text &&&&\\
& text &&&&\\
& text &&&&\\
\hline
\end{tabular}
\end{table}
Use the \rotatebox before the \parbox (and don't use \multirow in a tblr, that's no longer defined there):
---
title: "misc"
author: "Me"
date: "`r Sys.Date()`"
output:
pdf_document:
keep_tex: true
extra_dependencies: caption
number_sections: yes
fig_caption: yes
classoption: table
header-includes:
- \usepackage{tabularray}
language:
label:
fig: !expr function(x) sprintf("**Figure %s.** ", x)
fontsize: 11pt
urlcolor: blue
---
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={lXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
vspan=even,
cell{1}{2}={halign=c,bg=gray,fg=white},
cell{2}{1}={bg=gray,fg=white},
cell{3}{5-6}={bg=lightgray,font=\bfseries},
cell{5}{4-6}={bg=lightgray,font=\bfseries},
cell{6}{3}={bg=lightgray,font=\bfseries},
}
& \SetCell[c=5]{} Type of Distribution & & & &\\
\SetCell[r=5]{} \rotatebox{90}{\parbox{3.5cm}{\centering Type of Transformation}} & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & Y & Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & Y & Y & Y \\
& Logarithmic & Y & Y & N & N \\
\end{tblr}
\end{table}

LaTeX - tabular start at new line

I want to start the tabular at start of line, but when I put it directly after e.g. \paragraph{Foo}, than It will be displayed at right side of 'Foo'.
\documentclass[twocolumn,a4,titlepage]{article}
\begin{document}
\section{foo}
\paragraph{A}
\begin{tabular}{ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\paragraph{B}
\begin{tabular}{ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\end{document}
I know, I could put it between \begin{center} and \end{center}, but than it would float around (i.e. will sometimes be displayed after the following text) and will be centered, which looks strange for small tables.
I strongly believe that this question is already answered elsewhere, but I couldn't find it.
Thanks!!
You can force the table to start in a new line like this:
\documentclass[twocolumn,a4,titlepage]{article}
\begin{document}
\section{foo}
\paragraph{A}
\begin{tabular}{ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\paragraph{B}\mbox{}\par
\noindent\begin{tabular}{#{}ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\end{document}

LaTex table corner cell line not showing

I'm using Overleaf to write a report. I can't seem to get the format just right for the top right corner cell.
I would also like to center "Correlation coefficient"
\begin{table}[h]
\caption{Correlation coefficient between force plate data and kinematic model data}
\label{IMUvsVB}
\begin{center}
\begin{tabular}{|c||c||c|}
\hline
\textbf{Ground reaction} & \multicolumn{2}{l}{\textbf{Correlation coefficient}} \\
\cline{2-3}
& \textbf{IMU based kinematics} & \textbf{Vision based kinematics}\\
\hline
Anteroposterior & 0.91 & 0.96\\
Mediolateral & 0.80 & 0.79\\
Vertical & 0.97 & 0.99\\
Frontal & 0.64 & 0.66\\
Sagittal & 0.91 & 0.94\\
Transverse & 0.82 & 0.84\\
\hline
\end{tabular}
\end{center}
\end{table}
Substitute
\textbf{Ground reaction} & \multicolumn{2}{l}{\textbf{Correlation coefficient}} \\
with
\textbf{Ground reaction} & \multicolumn{2}{c|}{\textbf{Correlation coefficient}} \\
The \multicolumn command needs 3 inputs:
The 1st one is the number of columns to be spanned.
The 2nd is the formatting of the cell (l for left, c for center and r for right). Here you can add also | for a single vertical line or even || for a double vertical line.
The 3rd one is the content of the cell.
Have a look at the documentation here if you need more insights on the usage of \multicolumn.

Resources