0

Instead of specifying the width of each column in a longtable (see below), is there a way to get it to automatically adjust its size so that it matches the text width, but does not go into the margins?

\floatsetup[longtable]{LTcapwidth=\textwidth}
\scriptsize 
\begin{longtable}[c] 
{p{0.045\textwidth}p{0.045\textwidth}p{0.045\textwidth}p{0.045\textwidth}p{0.045\textwidth}p{0.05\textwidth}p{0.045\textwidth}p{0.045\textwidth}p{0.045\textwidth}p{0.045\textwidth} p{0.045\textwidth} p{0.045\textwidth} p{0.045\textwidth}} 
\caption{Insert caption here}
\hline \multicolumn{13}{c} %Removed code for 13 column multiple page table%
 \end{longtable}
  • please extend your code snippet to complete document beginning with \documentclass... and ending with \end{document}. welcome to tex.se! – Zarko Feb 22 at 17:06
  • 1
    You might be interested in the ltablex package. Or you could use \begin{longtable}[c]{*{13}{p{\dimexpr0.0768\textwidth-2\tabcolsep}}} if all your 13 columns are equally wide. – leandriis Feb 22 at 17:22
  • @ leandriis, to my surprise, your suggestion (the same cross my mind) doesn't give expected result. it might be due to number 13? :-). – Zarko Feb 22 at 17:33
  • There is also the xltabular package. – Bernard Feb 22 at 17:47
  • 2
    @leandriis why not make it perfect (well, as perfect as TeX can) and use *{13}{p{\dimexpr\textwidth/13-2\tabcolsep\relax}}? – Skillmon Feb 22 at 18:37
2

This uses the same approach as Zarko and Skillmon, but uses a length register to avoid repeating the calculation 13 times.

\documentclass{article}
\usepackage{longtable}
\usepackage{showframe}

\newlength{\mycolwidth}

\begin{document}
\scriptsize 
\setlength{\mycolwidth}{\dimexpr \textwidth/13 - 2\tabcolsep}%
\begin{longtable}[c]{*{13}{p{\mycolwidth}}}
\caption{Insert caption here}
\endfirsthead
\hline 
\multicolumn{13}{c}{Removed code for 13 column multiple page table}\\
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13
\end{longtable}
\end{document}

demo


There is an automatic gap of \tabcolsep on the edges, which can be removed thusly.

\documentclass{article}
\usepackage{longtable}
\usepackage{showframe}

\newlength{\mycolwidth}

\begin{document}
\scriptsize 
\setlength{\mycolwidth}{\dimexpr \textwidth - 24\tabcolsep}%
\divide \mycolwidth by 13

\begin{longtable}[c]{@{}*{13}{p{\mycolwidth}}@{}}
\caption{Insert caption here}
\endfirsthead
\hline 
\multicolumn{13}{c}{Removed code for 13 column multiple page table}\\
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13
\end{longtable}
\end{document}
  • now i wonder, what was wrong in my the very same code that doesn't give the same result as yours. overwriting my test code with yours solve my problem. apparently in it was some invisible character. +1 for nice working answer! – Zarko Feb 22 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.