# Make longtable automatically fit page (line width)

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?

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

\makeatletter

%commands for multipage
\def\ltabulary{%
\def\endfoot{\\\hline}%
\def\endlastfoot{\\\hline}%
\def\tabulary{%
\def\TY@final{%
\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
\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}

• As far as longtable margins, this answer, tex.stackexchange.com/questions/171825/…, shows how to reset the margins just for the longtable, and then reset the geometry back to what it was. Note that it does not auto-scale the margin widths to fit in a specified size. Dec 1 '14 at 14:36
• see ltxtable package (or ltablex) searching the site will show many examples. The margins for longtable are \LTleft and \LTright which you can set negative if need be Dec 1 '14 at 14:36
• @DavidCarlisle I found that one of your previous answers to be very very close to what I need, but the headers are cut. There's that little glitch with the headers width. Could you give me a hand fixing it? Thanks. Dec 2 '14 at 18:13

(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}
\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}


• If the cells have the same width, I'm afraid this is not enough Dec 1 '14 at 14:46
• @Agostino You can use other columns too, say, like {c|c|c|c|X}. You can also control the widths like >{\hsize=0.5\hsize}, this will set the width to half of X column width.
– user11232
Dec 1 '14 at 14:54
• The whole table ends up in a different page, and the cells are wither all the same width (X|X|X|X|X), out of the page (c|c|c|c|c) or some are too small (c|c|c|c|X). Not ok. I'd like them to automatically assume an optimal size. I'll add a better example above. Dec 1 '14 at 15:23
• @Agostino Like that?
– user11232
Dec 1 '14 at 15:50
• Much better. I guess the only thing I need to set manually is the X in the columns that are too wide. That means it's not completely automatic, but a big step forward. But why is it necessary to set 1.5\textheight? Dec 1 '14 at 16:27