[Tex/LaTex] Make longtable automatically fit page (line width)

landscapeline-breakinglongtablepage-breakingwidth

I have a longtable in landscape mode that is automatically split on multiple pages when there are too many rows.

I have solved the length problem. Now I have a problem with width, the rows are going outside the right side of the page.

I'd like to know if there is some automatic adjustment that can be applied to have the table fit inside the page's \linewidth. I'd like not having to set the column width by hand, but instead having the compilation breaking lines inside the cells, where appropriate. Other questions don't take the "automatic" part into account, or don't use longtable.

Not all columns need to be the same width (the ones containing few words should be smaller).

If you allow me to mention it, in Word, I would use set "optimal column size"? Sorry for mentioning that program 🙂

Update) it seems like tabulary would behave exactly as I need, I found an answer on how to make combine it with longtable to make it span many pages, but I have one last problem, the headers are not fitting properly. Any idea on how to fix this?

tabulary headers not fitting

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{longtable,tabulary}
\usepackage{pdflscape}

\makeatletter

%commands for multipage
\def\ltabulary{%
\def\endfirsthead{\\\hline}%
\def\endhead{\\\hline}%
\def\endfoot{\\\hline}%
\def\endlastfoot{\\\hline}%
\def\tabulary{%
  \def\TY@final{%
\def\endfirsthead{\LT@end@hd@ft\LT@firsthead}%
\def\endhead{\LT@end@hd@ft\LT@head}%
\def\endfoot{\LT@end@hd@ft\LT@foot}%
\def\endlastfoot{\LT@end@hd@ft\LT@lastfoot}%
\longtable}%
  \let\endTY@final\endlongtable
  \TY@tabular}%
\dimen@\columnwidth
\advance\dimen@-\LTleft
\advance\dimen@-\LTright
\tabulary\dimen@}

\def\endltabulary{\endtabulary}

\makeatother

\begin{document}

\section{Notes on papers}

\begin{landscape}
    \subsection{Thematics}

    \begin{ltabulary}{L|L|L|L|L|L|L|L}
    Work & System & Spatial & Topologies & Dependencies & Attack & Methodologies & Notes \\ \hline
    \endhead % all the rows above this will be repeated on every page

    [8] & 2Int & No & RR, ER, SF & 1to1 (bidir) & random (1-p) & perc, gen fun, sim & introduced cascading fails concept \\ \hline
    [39] & 2Int & no & ER, SF & Dependencies & random (1-p) & perc, gen fun, sim & critical coupling \\ \hline
    [1] & single, 2Int & Yes & various & 1to1, none & random, targeted & perc, other? & review \\ \hline
    [37] & 2Int & no & ER, SF & 1to1? with pref & random & sim, analysis & inter-similarity measure \\ \hline

    [38] & ??? & ER, SF & conn, dep & Attacks & perc & Notes \\ \hline
    [7] & 2Int & no & RR, ER, SF & 1to1 (bidir) & random (1-p) & perc & same degrees connected \\ \hline
    [35] & single & no & ER & competitive & none & perc & different view \\ \hline
    [31] & single & yes & LB, ER & none & none & perc & Notes \\ \hline
    [46] & 2Int & no & ER, SF & sup, dep, multiple (unidir) & random (multiple) & perc, num sim & different links, unidir \\ \hline
    [2] & single & no & ER & con, dep & random (1-p) & perc, sim & single net with dep links \\ \hline

    [?] & single, 2Int & no & ER, SF & 1to1 (bidir) & targeted (1-p) & perc, gen fun & maps targeted attack to random \\ \hline
    [?] & single & no & ER, SF, Re & none & targeted (q) & perc, sim & risk mitigation, new robustness measure \\ \hline
    [?] & NON & no & TofER, SLofER, LLofER & full dep, partial dep & ??? & perc & Notes \\ \hline
    \end{ltabulary}
\end{landscape}

\end{document}

Best Answer

(Without knowing much about your cell contents), You can use ltablex package and convert your long table in to breakable tabularx. Here one can use X column type that will be evenly wider.

\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pdflscape}
\usepackage{tabularx}
\usepackage{ltablex} % uncommenting this line will work
\keepXColumns

\title{MyTitle}
\author{Me}

\begin{document}

    \maketitle

    \section{First}
    %some words here

    \begin{landscape} %landscape mode
        \begin{tabularx}{1.5\textheight}{l|l|l|X|X}
            head1 & head2 & head3 & head4 & head5 \\
            \hline
            \endhead % all the rows above this will be repeated on every page

            %col5 goes out of the page (the real text is much longer)
            foo & foo & foo bar & foo bar foo bar foo bar & foo bar foo bar foo bar foo bar foo bar \\ \hline
            foo & foo bar foo & foo bar foo bar foo bar foo & foo bar & foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar \\ \hline
            foo & foo & foo bar foo bar foo bar foo & foo bar foo bar foo bar foo bar foo bar foo bar foo bar &  foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar \\ \hline

            %some more rows here

        \end{tabularx}
    \end{landscape}
\end{document}

enter image description here