3

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:

enter image description here

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

enter image description here

How to avoid this problem?

Viesturs
  • 7,895
  • 2
    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
  • Should this table be in landscape orientation? – Bernard Apr 22 '19 at 21:00
  • @Bernard, my question is about portrait orientation – Viesturs Apr 22 '19 at 21:00
  • 2
    @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
  • 3
    \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
  • You could make the table fit the fit into the textwidth using a combination of the \small font size, \setlength{\tabcolsep}{4pt} to reduce the horizontal whie space between columns and a p{1.5cm} column for the first column. – leandriis Apr 22 '19 at 21:03
  • 4
    @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

2 Answers2

6
\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}
David Carlisle
  • 757,742
  • Your solution is causing a problem with cline. See the update of my question. – Viesturs Apr 22 '19 at 21:49
  • 1
    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
4

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} 

enter image description here

Bernard
  • 271,350