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}
Related
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}
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}
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)
I would like to ask how to format table above in latex - table is pretty decent but I would like to have "model" bit right not at the very end, also I would like to have no space at the very right of the table.
\documentclass{article}
\usepackage[utf8]{inputenc}
\title{table in stack}
\author{petr102030 Hrobar}
\date{November 2019}
\begin{document}
\maketitle
\section{Introduction}
\begin{table}[!htbp] \centering
\label{exp_rmse}
\begin{tabular}{#{\extracolsep{5pt}}lccccccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
Model & \multicolumn{1}{c}{RMSE} & \multicolumn{1}{c}{MAE} & \multicolumn{1}{c}{MAPE} & \\
\hline \\[-1.8ex]
Holt-Winters (add.) & 1256.361 & 924.962 & 3.518 &\\
Holt-Winters (mul.) & 1238.799 & 909.395 & 3.457 &\\
Parab. Trend (model 4) & 2228.310 & 1792.471 & 7.959 &\\
Lin.Trend (model 5) & 3074.307 & 2545.426 & 11.339 &\\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
\end{document}
The "Model" is at the leftmost boarder of your table because the default padding is shallowed by #{\extracolsep{5pt}}. If you remove this, you'll get the padding back. (However the usual advice is to remove this padding, so please consider carefully if you really want to do this)
only specify as many columns as you have in your table and not 8 like in your example
with the siunitx package you can align the numbers nicely by their decimal markers
as already mentioned by Picaud Vincent the booktabs package is very useful to create nice looking tables. Amongst other things, it improves the vertical spacing around rules
using \label{} only makes sense if there also is a caption it can reference
I suggest to remove all the unnecessary \multicolumn{1}{...}, they don't do anything useful and can mess up the formatting
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{siunitx}
\title{table in stack}
\author{petr102030 Hrobar}
\date{November 2019}
\begin{document}
\maketitle
\section{Introduction}
\begin{table}[!htbp]
\centering
\begin{tabular}{#{}lS[table-format=4.3]S[table-format=4.3]S[table-format=2.3]}
\toprule
Model & {RMSE} & {MAE} & {MAPE} \\
\midrule
Holt-Winters (add.) & 1256.361 & 924.962 & 3.518 \\
Holt-Winters (mul.) & 1238.799 & 909.395 & 3.457 \\
Parab. Trend (model 4) & 2228.310 & 1792.471 & 7.959 \\
Lin.Trend (model 5) & 3074.307 & 2545.426 & 11.339 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
I have a table with 3 columns where the first 2 columns are pictures and the 3rd is my comment on those pictures, but the comment "drowns", it is down at the bottom of the cell.
I have tried with m{2cm} instead of c:
\begin{tabularx}{\textwidth}{c c m{2cm}}
but no change.
\begin{table}
\caption{Caption if needed}
\label{tab:lastcheck}
\begin{tabularx}{\textwidth}{c c c}
\textbf{O-Na} & \textbf{Na-Y} & \textbf{Comments} \\
\includegraphics[width=0.3\linewidth]{pic1.png} &
\includegraphics[width=0.3\linewidth]{pic2.png} &
My comments. \\
\end{tabularx}
\end{table}
My text is at the bottom of the cell.
The graphbox provides the handy align=c option to vertically centre images:
\documentclass{article}
\usepackage{tabularx}
\usepackage{array}
\usepackage{graphicx}
\usepackage{graphbox}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\begin{document}
\begin{table}
\caption{Caption if needed}
\label{tab:lastcheck}
\begin{tabularx}{\textwidth}{XXX}
\thead{O-Na} & \thead{Na-Y} & \thead{Comments} \\
\includegraphics[width=\linewidth,align=c]{example-image-duck} &
\includegraphics[width=\linewidth,align=c]{example-image-duck} &
My comments. \\
\end{tabularx}
\end{table}
\end{document}