0

I have a problem with the adjustment of my longtables to textwidth. I use tables with a different number of columns, and apparently LaTeX fails to adjust them to the same width. All Longtables with the same number of colums are displayed with the same width. How can I solve this problem? Thanks in advance!

\documentclass[a4paper,12pt,headsepline,american]{scrreprt}

\usepackage{array}
\usepackage[english]{babel}
\usepackage[labelfont=bf]{caption}
\captionsetup{format=plain,font=footnotesize}
\usepackage{float}
\usepackage{geometry}
\usepackage{layouts}
\usepackage{longtable}
\usepackage{ragged2e}

\begin{document}
...

\begin{longtable}{|>{\RaggedRight}p{0.4\textwidth}|>{\RaggedRight}p{0.2\textwidth}|>{\RaggedRight}p{0.4\textwidth}|}
\caption[...]{\textbf{...}}\\
\hline
... & ...   & ...\\
\hline
\hline
\endfirsthead
\hline
... & ...   & ...\\
\hline
\hline
\endhead
... & ...& ...\\
\hline
\end{longtable}

...

\begin{longtable}{|>{\RaggedRight}p{0.3\textwidth}|>{\RaggedRight}p{0.7\textwidth}|}
\caption[...]{\textbf{...}}\\
\hline
...     & ...\\
\hline
\hline
\endfirsthead
\hline
...     & ...\\
\hline
\hline
\endhead
...     & ...\\
\hline
\end{longtable}

...
\end{document}
1

You need to account for the widths of the rules (\arrayrulewidth) and column padding (\tabcolsep on each side of the column) so:

enter image description here

\documentclass[a4paper,12pt,headsepline,american]{scrreprt}

\usepackage{array}
\usepackage[english]{babel}
\usepackage[labelfont=bf]{caption}
\captionsetup{format=plain,font=footnotesize}
\usepackage{float}
\usepackage{geometry}
\usepackage{layouts}
\usepackage{longtable}
\usepackage{ragged2e}

\newlength\ltlen

\begin{document}
...

\setlength\ltlen{\dimexpr\textwidth-4\arrayrulewidth-6\tabcolsep}
\begin{longtable}{|>{\RaggedRight}p{0.4\ltlen}|>{\RaggedRight}p{0.2\ltlen}|>{\RaggedRight}p{0.4\ltlen}|}
\caption[...]{\textbf{...}}\\
\hline
... & ...   & ...\\
\hline
\hline
\endfirsthead
\hline
... & ...   & ...\\
\hline
\hline
\endhead
... & ...& ...\\
\hline
\end{longtable}

...

\setlength\ltlen{\dimexpr\textwidth-3\arrayrulewidth-4\tabcolsep}
\begin{longtable}{|>{\RaggedRight}p{0.3\ltlen}|>{\RaggedRight}p{0.7\ltlen}|}
\caption[...]{\textbf{...}}\\
\hline
...     & ...\\
\hline
\hline
\endfirsthead
\hline
...     & ...\\
\hline
\hline
\endhead
...     & ...\\
\hline
\end{longtable}

...
\end{document}
|improve this answer|||||
  • Thank you! But the tables are now a lot wider that the actual textwidth, which is indicated by the length of the header etc. Also, I have some more tables with different column numbers, so what is the general rule behind the numbers in the line \setlength\ltlen{\dimexpr\textwidth-4\arrayrulewidth-6\tabcolsep}? – ke.sijing May 21 '15 at 11:27
  • @ke.sijing the tables in the above are exactly textwidth wide so I don't understand your comment about them being wider than the text width. the numbers are the numbers of rules in the table and twice the number of columms. Each rule is \arrayrulewidth wide so if you have four rules you need to allow for that before allocating space to the columns, each column has \tabcolsep space either side so if you have 3 columns that is 6\tabcolspace that must be accounted for. – David Carlisle May 21 '15 at 11:35
  • Yeah, the tables in your example have the same width. But this is not the case for the tables in my document, for whatever reason... – ke.sijing May 21 '15 at 15:00
  • ok, found my syntax error, everything works now. :) Thank you! – ke.sijing May 21 '15 at 15:12

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.