[Tex/LaTex] Adjust Longtable to textwidth

longtable

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}

Best Answer

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}
Related Question