# fit longtable to textwidth

I am applying How to fit landscape multi-page table to textwidth to fit longtable to textwidth in this example:

\documentclass{article}
\usepackage{longtable}
\usepackage{lipsum}
\begin{document}
\lipsum[1][1-4]
%\resizebox{\textwidth}{!}{
\begingroup % localize the following settings
\setlength\LTcapwidth{\textwidth} % default: 4in (rather less than \textwidth...)
\setlength\LTleft{0pt}            % default: \parindent
\setlength\LTright{0pt}           % default: \fill
\begin{longtable}{@{\extracolsep{\fill}}|l|rcc|rcc|rcc|rcc|}
longtable aaaaa aaaa aaaa aaa aaaa & 11.11 & 2 & 0.000 & 22.22 & 2 & 0.000 & 333.33 & 2 & 0.137 & 44.44 & 2 & 0.000\\
\end{longtable}
\endgroup
%}
\end{document}


However, the outcome shows I haven't been successful:

What is wrong?

I check the answer of @DavidCarlisle. However, the proposed manipulations cause a problem with \cline:

\begingroup % localize the following settings
\setlength\LTcapwidth{\textwidth} % default: 4in (rather less than \textwidth...)
\setlength\LTleft{0pt}            % default: \parindent
\setlength\LTright{0pt}           % default: \fill
\tiny
\setlength{\tabcolsep}{0.4pt}
\begin{longtable}{@{\extracolsep{\fill}}|p{3cm}|rcc|rcc|rcc|rcc|}
Sample&\multicolumn{3}{c|}{AAAA}&\multicolumn{3}{c|}{BBBB}&\multicolumn{3}{c|}{CCCC}&\multicolumn{3}{c|}{DDDDD}\\\cline{2-13}
lllllllllongtable & 11.11 & 2 & 0.000 & 22.22 & 2 & 0.000 & 33.33 & 2 & 00.00 & 44.44 & 2 & 0.000\\
\end{longtable}
\endgroup


How to avoid this problem?

• as in tabular*, \extracolsep{\fill}} allows the table to expand to the specified width, your table is too wide, so you need to use a multi-line cell (change the first l to p{2cm} or whetever width you want) or use a smaller font such as \small – David Carlisle Apr 22 '19 at 20:57
• @DavidCarlisle is there any analogue of \resizebox? It works so nice. – Viesturs Apr 22 '19 at 20:59
• @Viesturs: I would definitely recommend to not use resizebox on material that contains text as it will lead to inconsistent font sized and distances. – leandriis Apr 22 '19 at 21:00
• \resizebox should never be used for tables, as it makes the font size inconsistent. B.t. w., do you use marginal notes? – Bernard Apr 22 '19 at 21:01
• @Viesturs every time I see someone apply \resizebox to a table, I wish I'd never implemented it. You wouldn't justify a paragraph by just scaling it to an arbitrary font size, why inflict that in tables? – David Carlisle Apr 22 '19 at 21:07

\documentclass{article}
\usepackage{longtable}
\usepackage{lipsum}
\begin{document}
\lipsum[1][1-4]
%\resizebox{\textwidth}{!}{ never scale tables!
\begingroup % localize the following settings
\setlength\tabcolsep{2pt}
\footnotesize

\setlength\LTcapwidth{\textwidth} % default: 4in (rather less than \textwidth...)

\setlength\LTleft{0pt}            % default: \fill
\setlength\LTright{0pt}           % default: \fill

\begin{longtable}{@{\extracolsep{\fill}}|p{2cm}|rcc|rcc|rcc|rcc|@{}}
longtable aaaaa aaaa aaaa aaa aaaa & 11.11 & 2 & 0.000 & 22.22 & 2 & 0.000 & 333.33 & 2 & 0.137 & 44.44 & 2 & 0.000\\
\end{longtable}
\endgroup
%}
\end{document}

• Your solution is causing a problem with cline. See the update of my question. – Viesturs Apr 22 '19 at 21:49
• if you use \extracolsep then the space is inserted between columns and has the effect on cline that you show, so that is by design (personally I would not use \extracolsep forcing the columns apart doesn't make the tables more readable – David Carlisle Apr 22 '19 at 22:31

A solution with the xltabular package and its eponymous environment, which brings the functionalities of longtable to tabularx. I loaded the geometry package, to have more decent margins:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{longtable}
\usepackage{ragged2e}
\usepackage{xltabular}

\usepackage{lipsum}

\begin{document}

\lipsum[1][1-4]
\begingroup % localize the following settings
\setlength\LTcapwidth{\textwidth} % default: 4in (rather less than \textwidth...)
\setlength\LTleft{0pt} % default: \parindent
\setlength\LTright{0pt} % default: \fill

\begin{xltabular}{\linewidth}{|>{\RaggedRight\arraybackslash}X|rcc|rcc|rcc|rcc|}
longtable aaaaa aaaa aaaa aaa aaaa & 11.11 & 2 & 0.000 & 22.22 & 2 & 0.000 & 333.33 & 2 & 0.137 & 44.44 & 2 & 0.000\\
\end{xltabular}

\endgroup

\end{document}


• How to centre vertically the cells with numerical values? – Viesturs Apr 22 '19 at 21:24
• Redefine the X column type as m{#1}, as was already answered to another of your questions, if I remember well. – Bernard Apr 22 '19 at 21:36