Vertical and horizontal cell centering in LaTex table - latex

I am making a LaTex table and have trouble centering text (and images) inside cells themselves. The table as a whole is centered on the page, but what I want is that text and images in every cell are centered both vertically and horizontally.
This is my code
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\usepackage{tabularray}
\usepackage{float}
\usepackage{graphicx}
\begin{document}
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|c|}
\hline
\rowcolor[HTML]{C0C0C0}
\textbf{Particle} & \textbf{Orientation} & \textbf{Symbol} & \textbf{Letter} \\[4ex] \hline
& in & {\includegraphics[scale=0.8]{im/inel.jpg}} & $u$ \\[2ex] \cline{2-4}
\multirow{-2}{*}{fermion} & out & {\includegraphics[scale=0.8]{im/outel.jpg}} & $\overline{u}$ \\[2ex] \hline
& in & {\includegraphics[scale=0.8]{im/inpoz.jpg}} & $\overline{v}$ \\[2ex] \cline{2-4}
\multirow{-2}{*}{antifermion} & out & {\includegraphics[scale=0.8]{im/outpoz.jpg}} & $v$ \\[2ex] \hline
& in & {\includegraphics[scale=0.8]{im/inph.jpg}} & $\epsilon_\mu$ \\[2ex] \cline{2-4}
\multirow{-2}{*}{foton} & out & {\includegraphics[scale=0.8]{im/outph.jpg}} & $\epsilon_{\mu}^*$ \\[2ex] \hline
\end{tabular}
\end{table}
\end{document}
This is what my code gives me (also notice how some lines are obscured by the images - that is not how I wanted it):
And, this is what I want:
Any help is welcome! Thank you

You are already loading the tabularray package, just use it instead of a tabular:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\usepackage{tabularray}
\usepackage{float}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\begin{document}
\begin{table}[H]
\centering
\begin{tblr}{
cells={valign=m,halign=c},
row{1}={bg=lightgray,font=\bfseries,rowsep=8pt},
colspec={QQQQ},
hlines,
vlines
}
Particle & Orientation & Symbol & Letter\\
\SetCell[r=2]{} fermion& in & \includegraphics[scale=0.5,valign=c]{example-image-duck} & $u$ \\
& out & \includegraphics[scale=0.5,valign=c]{example-image-duck}& $\overline{u}$ \\
\SetCell[r=2]{} antifermion & in & \includegraphics[scale=0.5,valign=c]{example-image-duck} & $\overline{v}$ \\
& out & \includegraphics[scale=0.5,valign=c]{example-image-duck} & $v$ \\
\SetCell[r=2]{} photon & in & \includegraphics[scale=0.5,valign=c]{example-image-duck} & $\epsilon_\mu$ \\
& out & \includegraphics[scale=0.5,valign=c]{example-image-duck} & $\epsilon_{\mu}^*$ \\
\end{tblr}
\end{table}
\end{document}

Related

How to create a multirow multicolumn table with subheadings below the first row

I am attempting to create a table in latex as below, although I am a bit confused about where and when the \multirow and \multicolumn commands are to be used inside a tabular environment. Col_1, Col_2, and Col_3 are supposed to be subheadings but they need to appear in the second row.
What I started off with is, (the columns are duplicated, so I wanted to split it into multiple rows as shown in the image) :
\begin{table}[H]
\begin{tabular}{lSSSSSSSS}
\toprule
\multirow{2}{*}{\textbf{A}} &
\multicolumn{3}{c}{\textbf{Model_1}} &
\multicolumn{5}{c}{\textbf{Model_2}} \\
& {Col_1} & {Col_2} & {Col_3} & {Col_1} & {Col_2} & {Col_3} & {Col_4} & {Col_5} \\
\midrule
x\% & a\% b\% & c\% & x\% & y\% & z\% & 0. & 0. \\
\bottomrule
\end{tabular}
\caption{results}
\end{table}
You can place your subheaders in a row of their own, no need to worry about mutirows and such
\documentclass{article}
\usepackage{siunitx}
%\usepackage{booktabs}
\usepackage{float}
\begin{document}
\begin{table}[H]
\begin{tabular}{l|S|S|S}
\textbf{A} & \multicolumn{3}{c}{x\%} \\
\hline
& {Col\_1} & {Col\_2} & {Col\_3} \\
Model\_1 & a\% & b\% & c\% \\
\hline
Model\_2 & x\% & y\% & z\% \\
\end{tabular}
\caption{results}
\end{table}
\end{document}
Instead of the layout you show in your image, I rather suggest to avoid vertical lines for a more professional looking result:
\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabular}{#{}lSSSSSSSS#{}}
\toprule
\textbf{A} &
\multicolumn{3}{c}{\textbf{Model\_1}} &
\multicolumn{5}{c}{\textbf{Model\_2}} \\
& {Col\_1} & {Col\_2} & {Col\_3} & {Col\_1} & {Col\_2} & {Col\_3} & {Col\_4} & {Col\_5} \\
\cmidrule(r){2-4}\cmidrule(l){5-9}
x\% & a\% & b\% & c\% & x\% & y\% & z\% & 0. & 0. \\
\bottomrule
\end{tabular}
\caption{results}
\end{table}
\end{document}

How to fix the width of longtable LATEX

I want to set the width of my longtable so it fits within the page and not crops out. I have 206 columns and 17 rows.
I use tablesgenerator.com to get my longtable, but the option "Scale table to text width" doesn't work with longtable, could use some help. (Latex beginner here).
\documentclass{article}
\usepackage{longtable}
\usepackage{booktabs}
\begin{document}
\begin{longtable}{#{}lllllllllllllllll#{}}
\toprule
\textbf{SN} & \textbf{zhelio} & \textbf{zhelio\_un} & \textbf{zCMB} &
\textbf{zCMB\_un} & \textbf{Peak\_MJD} & \textbf{Peak\_MJD\_un} & \textbf{x1}
& \textbf{x1\_un} & \textbf{c} & \textbf{c\_un} & \textbf{mB} &
\textbf{mB\_un} & \textbf{t} & \textbf{t\_un} & \textbf{Host\_logmass} &
\textbf{Host\_logmass\_un} \\* \midrule
\endhead
%table stuff here...
\caption{}
\label{tab:my-table}\\
\end{longtable}
\end{document}
This is what it looks like
Assuming you mean 206 rows and not columns, then you could try the following strategies:
put the table on landscape pages
use a smaller font
reduce the margins of your document to have more space
reduce the space between columns
\documentclass{article}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage[vmargin=2cm]{geometry}
\usepackage{lscape}
\begin{document}
\begin{landscape}
\small
\setlength{\tabcolsep}{2pt}
\begin{longtable}{#{}lllllllllllllllll#{}}
\toprule
\textbf{SN} & \textbf{zhelio} & \textbf{zhelio\_un} & \textbf{zCMB} &
\textbf{zCMB\_un} & \textbf{Peak\_MJD} & \textbf{Peak\_MJD\_un} & \textbf{x1}
& \textbf{x1\_un} & \textbf{c} & \textbf{c\_un} & \textbf{mB} &
\textbf{mB\_un} & \textbf{t} & \textbf{t\_un} & \textbf{Host\_logmass} &
\textbf{Host\_logmass\_un} \\* \midrule
\endhead
%table stuff here...
\caption{}
\label{tab:my-table}\\
\end{longtable}
\end{landscape}
\end{document}

Misplaced \noalign if using \ifstrequal

I get a "misplaced \noaling" error within an tabularx environment if I am using \ifstrequal.
This is working:
\documentclass{article}
\usepackage{tabularx}
\usepackage{etoolbox}
\begin{document}
\begin{tabularx}{\textwidth}{rrrrr}
I & am & a & useless & table \\
\ifstrequal{a}{a}{
I & am & a & useless & table \\
} {
I & am & a & useless & table \\
}
\end{tabularx}
\end{document}
This not:
\documentclass{article}
\usepackage{tabularx}
\usepackage{etoolbox}
\begin{document}
\begin{tabularx}{\textwidth}{rrrrr}
I & am & a & useless & table \\
\ifstrequal{a}{a}{
\hline
I & am & a & useless & table \\
\hline
} {
I & am & a & useless & table \\
}
\end{tabularx}
\end{document}
You could resort to good old \ifx:
\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\newcommand{\stringa}{a}
\newcommand{\stringb}{b}
\begin{document}
\begin{tabularx}{\textwidth}{rrrrr}
I & am & a & useless & table \\
\ifx\stringa\stringa
\midrule
I & am & a & useless & table \\
\midrule
\else
I & am & a & useless & table \\
\fi
\end{tabularx}
\end{document}
(I'm using rules from the booktabs package instead of \hline because they give better spacing)

How to define a cellsize in LaTeX for wraptable?

I have created the following table in LaTex with wrap table from Package wrapfigure.
\documentclass[a4paper,11pt,oneside,numbers=noendperiod,setspace,parskip=half]{scrbook}
\usepackage[ngerman, english]{babel}
\usepackage{blindtext}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{wrapfig}
\usepackage[table,xcdraw]{xcolor}
\begin{document}
\blindtext
\begin{wraptable}{l}{8cm}
\begin{tabular}{lcccc}
\hline
\rowcolor[HTML]{656565}
\multicolumn{1}{|c|}{\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF}
\textbf{Surface geometry}}} & \multicolumn{4}{c|}{\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF} \textbf{Terrain Classes}}} \\ \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}fine texture,\\ high convexity\end{tabular}}} & \multicolumn{1}{c|}{\cellcolor[HTML]{643403}\textbf{1}} & \multicolumn{1}{c|}{\cellcolor[HTML]{F8A102}\textbf{5}} & \multicolumn{1}{c|}{\cellcolor[HTML]{009901}\textbf{9}} & \multicolumn{1}{c|}{\cellcolor[HTML]{34FF34}\textbf{13}} \\ \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}fine texture,\\ low convexity\end{tabular}}} & \multicolumn{1}{c|}{\cellcolor[HTML]{CE6301}\textbf{3}} & \multicolumn{1}{c|}{\cellcolor[HTML]{FFCC67}\textbf{7}} & \multicolumn{1}{c|}{\cellcolor[HTML]{34CDF9}\textbf{11}} & \multicolumn{1}{c|}{\cellcolor[HTML]{3531FF}\textbf{15}} \\ \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}coarse texture,\\ low convexity\end{tabular}}} & \multicolumn{1}{c|}{\cellcolor[HTML]{F007F4}\textbf{2}} & \multicolumn{1}{c|}{\cellcolor[HTML]{A34894}\textbf{6}} & \multicolumn{1}{c|}{\cellcolor[HTML]{C5C783}\textbf{10}} & \multicolumn{1}{c|}{\cellcolor[HTML]{FCFF2F}\textbf{14}} \\ \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}coarse texture,\\ low convexity\end{tabular}}} & \multicolumn{1}{c|}{\cellcolor[HTML]{F690E5}\textbf{4}} & \multicolumn{1}{c|}{\cellcolor[HTML]{F6C8EC}\textbf{8}} & \multicolumn{1}{c|}{\cellcolor[HTML]{DFE360}\textbf{12}} & \multicolumn{1}{c|}{\cellcolor[HTML]{FFFC9E}\textbf{16}} \\ \hline
& \multicolumn{1}{l}{\textit{steep}} & \multicolumn{2}{l}{} & \multicolumn{1}{l}{\textit{gentle}}
\end{tabular}
\end{wraptable}
\blindtext
\end{document}
This looks currently like that:
Table in document:
How is it possible, to change, or set a specific cell size of the columns for all the "Terrain Classes" 1 to 16? Like alls cells have the same width 1.5cm?
Here are my proposed changes to your code:
Change the tabular command to fixed-width columns
Change cells from \multicolumn{1}{c}{<stuff>} to simply <stuff>
Change \\ at end of tabular row to \tabularnewline
I have also taken the liberty to choose 1cm instead of 1.5cm (too wide) and increase the wrapping size to accommodate the wider table.
\documentclass[a4paper,11pt,oneside,numbers=noendperiod,setspace,parskip=half]{scrbook}
\usepackage[ngerman, english]{babel}
\usepackage{blindtext}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{wrapfig}
\usepackage[table,xcdraw]{xcolor}
\begin{document}
\blindtext
\begin{wraptable}{l}{10cm}
\begin{tabular}{|l*{4}{|>{\centering}p{1cm}}|}
\hline
\rowcolor[HTML]{656565}
\multicolumn{1}{|c|}{\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF}
\textbf{Surface geometry}}} & \multicolumn{4}{c|}{\cellcolor[HTML]{656565}{\color[HTML]{FFFFFF} \textbf{Terrain Classes}}} \\ \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}fine texture,\\ high convexity\end{tabular}}} &
\cellcolor[HTML]{643403}\textbf{1} &
\cellcolor[HTML]{F8A102}\textbf{5} &
\cellcolor[HTML]{009901}\textbf{9} &
\cellcolor[HTML]{34FF34}\textbf{13} \tabularnewline \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}fine texture,\\ low convexity\end{tabular}}} &
\cellcolor[HTML]{CE6301}\textbf{3} &
\cellcolor[HTML]{FFCC67}\textbf{7} &
\cellcolor[HTML]{34CDF9}\textbf{11} &
\cellcolor[HTML]{3531FF}\textbf{15} \tabularnewline \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}coarse texture,\\ low convexity\end{tabular}}} &
\cellcolor[HTML]{F007F4}\textbf{2} &
\cellcolor[HTML]{A34894}\textbf{6} &
\cellcolor[HTML]{C5C783}\textbf{10} &
\cellcolor[HTML]{FCFF2F}\textbf{14} \tabularnewline \hline
\multicolumn{1}{|l|}{\textit{\begin{tabular}[c]{#{}l#{}}coarse texture,\\ low convexity\end{tabular}}} &
\cellcolor[HTML]{F690E5}\textbf{4} &
\cellcolor[HTML]{F6C8EC}\textbf{8} &
\cellcolor[HTML]{DFE360}\textbf{12} &
\cellcolor[HTML]{FFFC9E}\textbf{16} \tabularnewline \hline
\multicolumn{1}{l}{} & \multicolumn{1}{l}{\textit{steep}} & \multicolumn{2}{l}{} & \multicolumn{1}{l}{\textit{gentle}}
\end{tabular}
\end{wraptable}
\blindtext
\end{document}
Here's the result:

Latex threeparttable table notes width

I am having problems with my threeparttable notes as they are somehow not covering the entire width of my table. Can anybody help me? Thank you already in advance!
Here is the code I use:
\documentclass[11pt, oneside]{article}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage{graphicx}
\usepackage{lscape}
\usepackage{booktabs}
\usepackage{amssymb}
\usepackage[flushleft]{threeparttable}
\usepackage{longtable}
\usepackage{array}
\usepackage{pdflscape}
\usepackage{tabularx}
\usepackage{adjustbox}
\pagenumbering{gobble}
\usepackage{booktabs}
\begin{document}
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{table}
\scalebox{0.6}{
\begin{threeparttable}
\begin{tabularx}
\textwidth{lccccccccc}
& \textbf{Neonatal} & \textbf{Infant} & \textbf{Low} \\
& \textbf{Mortality} & \textbf{Mortality} & \textbf{Birthweight} & \textbf{Stunting} & \textbf{Underweight} &\\
\multicolumn{10}{c}{\noindent\rule{18cm}{0.4pt}}\\
\textit{Mother is between 17 and 19 yrs} & -0.00517*** & -0.00857*** & -0.00896** & -0.0487*** & -0.0315*** \\
& [0.001] & [0.002] & [0.004] & [0.005] & [0.004] \\
\multicolumn{10}{c}{\noindent\rule{18cm}{0.2pt}}\\
\textbf{N} & 752543 & 574609 & 399845 & 501520 & 512482 \\
\multicolumn{10}{c}{\noindent\rule{18cm}{0.4pt}}\\
\end{tabularx}
\begin{tablenotes}
\item Results are from the main results regressions (4). ***, ** and * denote significance at the 1\%, 5\%, and 10\% levels, respectively. Std. Errors in square brackets and are clustered at PSU level.
\end{tablenotes}
\end{threeparttable}
}
\end{table}
}
\end{document}
I know it's been years since your question, I am learning creating tables right now that's how I've found this question.
I think you confused the threeparttable package by declaring 10 colunms, while you have only 6 in reality. Why don't you try using 6 columns and then some way linebreak the headers and long cells instead?
\documentclass[11pt, oneside]{article}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage{graphicx}
\usepackage{lscape}
\usepackage{booktabs}
\usepackage{amssymb}
\usepackage[flushleft]{threeparttable}
\usepackage{longtable}
\usepackage{array}
\usepackage{pdflscape}
\usepackage{tabularx}
\usepackage{adjustbox}
\pagenumbering{gobble}
\usepackage{booktabs}
\usepackage{makecell}
\renewcommand\theadalign{bc}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{\Gape[4pt]}
\renewcommand\cellgape{\Gape[4pt]}
\begin{document}
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{table}
\scalebox{0.8}{
\begin{threeparttable}
\begin{tabularx}
{16.5cm}{lccccc}
{} & \thead{Neonatal \\ Mortality} & \thead{Infant \\ Mortality} & \thead{Low \\ Birthweight} & \thead{Stunting} & \thead{Underweight} \\
\midrule
{\shortstack[l]{\textit{Mother is between} \\ \textit{17 and 19 yrs}}} & {\shortstack[c]{ $-0.00517$*** \\ $[0.001] $ }} & {\shortstack[c]{$ -0.00857$*** \\ $[0.002] $}} & {\shortstack[c]{$ -0.00896$** \\ $[0.004]$}} & {\shortstack[c]{$ -0.0487$*** \\ $[0.005]$}} & {\shortstack[c]{$-0.0315$*** \\ $[0.004]$}} \\
\midrule
\textbf{N} & 752543 & 574609 & 399845 & 501520 & 512482 \\
\midrule
\end{tabularx}
\begin{tablenotes}
\item Results are from the main results regressions (4). ***, ** and * denote significance at the 1\%, 5\%, and 10\% levels, respectively. Std. Errors in square brackets and are clustered at PSU level.
\end{tablenotes}
\end{threeparttable}
}
\end{table}
}

Resources