# split longtable over even and odd pages (2)

I am a first-time user of LaTeX, trying to typeset my thesis. My problem is that some of my tables are large and do not fit on a single page; both in length (many rows) but some also in width (many columns).

The longtable package provides a good solution for the long tables, but I have not yet found a satisfying solution for tables that are (also) wide. I think that the best solution would be to have those tables span two facing pages, with the first few columns on the left page and subsequent columns on the right page.

Recently a question was asked on tex.stackexchange, that is analogous to my problem (albeit dealing with only two columns): How to make a long table split on even/odd pages?. And David Carlisle had posted an answer with code that works with longtable; splitting the table over two facing pages.

However, implementing the code revealed two issues:

1. Davids code requires a \pagebreak before \end{longtable}. This seems to interfere with the repeated footers specified through longtable (\endfoot, \endlastfoot). Specifically, the \endfoot is also repeated at the end of the table, instead of \endlastfoot.

2. Surrounding text does not flow naturally around the table float: Preceding text is printed only on the left/even page, and following text is placed on a new page (probably due to the \pagebreak mentioned above), leaving a lot of white space below the table.

My question is if anyone has an idea if these issues can be resolved, and how?

Below this question is an edited version of Davids code illustrating the problems:

\documentclass{book}

\usepackage{array,longtable,color}
\usepackage{booktabs}
\usepackage{lipsum}
\usepackage[justification=justified,singlelinecheck=false]{caption}

\makeatletter

\def\LT@output{%
\ifnum\outputpenalty <-\@Mi
\ifnum\outputpenalty > -\LT@end@pen
\LT@err{floats and marginpars not allowed in a longtable}\@ehc
\else
\setbox\z@\vbox{\unvbox\@cclv}%
\ifdim \ht\LT@lastfoot>\ht\LT@foot
\dimen@\pagegoal
\ifdim\dimen@<\ht\z@
\setbox\zzz\vbox{\moveleft10in\vbox{\unvcopy\@cclv\copy\LT@foot}\vss}%
\setbox\@cclv\vbox{\vbox{\unvbox\@cclv\copy\LT@foot}\vss}%
\@makecol
\@outputpage
\setbox\@cclv\box\zzz
\@makecol
\@outputpage
\fi
\fi
\global\@colroom\@colht
\global\vsize\@colht
%      \vbox
%      {\unvbox\z@\box\ifvoid\LT@lastfoot\LT@foot\else\LT@lastfoot\fi}%
\fi
\else
\setbox\zzz\vbox{\moveleft10in\vbox{\unvcopy\@cclv\copy\LT@foot}\vss}%
\setbox\@cclv\vbox{\vbox{\unvbox\@cclv\copy\LT@foot}\vss}%
\@makecol
\@outputpage
\setbox\@cclv\box\zzz
\@makecol
\@outputpage
\global\vsize\@colroom
\fi}

\newbox\zzz

\def\a{%
one&&red\\
two&&green\\
three&&yellow\\
four&&brown\\
}
\def\b{%
111&&this\\
2&&that\\
333&&the other\\
44&&something\\}

\begin{document}

\pagenumbering{arabic}
\lipsum

\begin{longtable}{cp{10in}c}
\caption{Test table} \\

% This is the first header
\toprule
LEFT&&RIGHT\\
\midrule

% This is for subsequent headers
\multicolumn{3}{l}{{\tablename\ \thetable{}, continued}} \\
\toprule
LEFT&&RIGHT\\
\midrule

% This is the first footer
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\endfoot

% This is the last footer
\bottomrule
\multicolumn{3}{l}{{This is the footer}} \\
\endlastfoot

%These are the data
first&&row\\
\a\uppercase{\a}\b
\a\uppercase{\a}\b\b
last&&row\\
\pagebreak % you need this  %BUT: it interferes with the \endfoot and \endlastfoot
\caption{end caption}
\end{longtable}

\lipsum

\end{document}


PS Thank you Jubobs for redirecting me to post my query as a new question.

• Have you considered switching to "landscape" mode to typeset the extra-wide tables?
– Mico
Nov 4 '14 at 16:56

The forced page break at the end is more or less required as otherwise it's hard to know what to do with text after the table (at the end of the table you need to finish two pages and you certainly don't want following text on both of them so not allowing it at all is simplest. It would possibly be possible to make \endlastfoot work again but not today.

The usual approach to letting longtable move past the surrounding text is to use \afterpage to insert it at the next page break, as in the code below.

\begin{filecontents}{\jobname.lt1}
\begin{longtable}{cp{10in}c}
\caption{Test table} \\

% This is the first header
\toprule
LEFT&&RIGHT\\
\midrule

% This is for subsequent headers
\multicolumn{3}{l}{{\tablename\ \thetable{}, continued}} \\
\toprule
LEFT&&RIGHT\\
\midrule

% This is the first footer
\midrule
\multicolumn{3}{r}{{foot}}
\endfoot

% This is the last footer% sorry not today
%\bottomrule
%\multicolumn{3}{l}{{This is the footer}} \\
%\endlastfoot

%These are the data
first&&row\\
\a\uppercase{\a}\b
\a\uppercase{\a}\b\b
last&&row\\
\pagebreak % you need this  %BUT: it interferes with the \endfoot and \endlastfoot
\caption{end caption}
\end{longtable}
\end{filecontents}

\documentclass{book}

\usepackage{array,longtable,color}
\usepackage{booktabs}
\usepackage{lipsum}
\usepackage{afterpage}
\usepackage[justification=justified,singlelinecheck=false]{caption}

\makeatletter

\def\LT@output{%
\ifnum\outputpenalty <-\@Mi
\ifnum\outputpenalty > -\LT@end@pen
\LT@err{floats and marginpars not allowed in a longtable}\@ehc
\else
\setbox\z@\vbox{\unvbox\@cclv}%
\ifdim \ht\LT@lastfoot>\ht\LT@foot
\dimen@\pagegoal
\ifdim\dimen@<\ht\z@
\setbox\zzz\vbox{\moveleft10in\vbox{\unvcopy\@cclv\copy\LT@foot}\vss}%
\setbox\@cclv\vbox{\vbox{\unvbox\@cclv\copy\LT@foot}\vss}%
\@makecol
\@outputpage
\setbox\@cclv\box\zzz
\@makecol
\@outputpage
\fi
\fi
\global\@colroom\@colht
\global\vsize\@colht
%      \vbox
%      {\unvbox\z@\box\ifvoid\LT@lastfoot\LT@foot\else\LT@lastfoot\fi}%
\fi
\else
\setbox\zzz\vbox{\moveleft10in\vbox{\unvcopy\@cclv\copy\LT@foot}\vss}%
\setbox\@cclv\vbox{\vbox{\unvbox\@cclv\copy\LT@foot}\vss}%
\@makecol
\@outputpage
\setbox\@cclv\box\zzz
\@makecol
\@outputpage
\global\vsize\@colroom
\fi}

\newbox\zzz

\def\a{%
one&&red\\
two&&green\\
three&&yellow\\
four&&brown\\
}
\def\b{%
111&&this\\
2&&that\\
333&&the other\\
44&&something\\}

\title{zzz}
\begin{document}

\maketitle
\clearpage

\pagenumbering{arabic}
\lipsum

\afterpage{\clearpage
\ifodd\value{page}\afterpage{\input{\jobname.lt1}\clearpage}%
\else\input{\jobname.lt1}\clearpage
\fi}

\lipsum
\lipsum

\end{document}