1

Longtable environment has sections for first head and other head. I want to add third section for last head which must be on the last page of a table with slightly different text. Is it possible?

PS. I'm not sure if this question needs minimal working example.

2

(Some explanation is written in the answer of Configure long table caption

It is possible. Take a look at the following code from longtable.sty

\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
        \advance\dimen@-\ht\LT@lastfoot
        \ifdim\dimen@<\ht\z@
          \setbox\@cclv\vbox{\unvbox\z@\copy\LT@foot\vss}%
          \@makecol
          \@outputpage
          \setbox\z@\vbox{\box\LT@head}%
        \fi
      \fi
      \global\@colroom\@colht
      \global\vsize\@colht
      \vbox
        {\unvbox\z@\box\ifvoid\LT@lastfoot\LT@foot\else\LT@lastfoot\fi}%
    \fi
  \else
    \setbox\@cclv\vbox{\unvbox\@cclv\copy\LT@foot\vss}%
    \@makecol
    \@outputpage
      \global\vsize\@colroom
    \copy\LT@head\nobreak%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \fi}

We could see that \ifnum\outputpenalty <-\@Mi is used to check whether this page is the last page. So we would like to \copy\LT@lasthead at this point. However, at this moment \LT@head is already inserted at the top of \@cclv. (Otherwise checking penalty does not make sense.) Therefore we need \setbox\LT@head=\vsplit\@cclv to0pt to remove \LT@head from \@cclv. (And return it back to \LT@head). But this fails since \LT@head in \@cclv follows a \nobreak. So we have to modify the line \copy\LT@head\nobreak to get the following code:

\documentclass{article}
\usepackage{longtable,setspace}
\begin{document}
\listoftables
\setstretch{5}
\makeatletter
\newif\ifmorethanonepage\morethanonepagefalse
\newbox\LT@lasthead\def\endlasthead{\LT@end@hd@ft\LT@lasthead}
\def\LT@output{%
  \ifnum\outputpenalty <-\@Mi
    \ifmorethanonepage\copy\LT@lasthead\fi%%%%%
    \setbox\LT@head=\vsplit\@cclv to0pt%%%%%%
    \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
        \advance\dimen@-\ht\LT@lastfoot
        \ifdim\dimen@<\ht\z@
          \setbox\@cclv\vbox{\unvbox\z@\copy\LT@foot\vss}%
          \@makecol
          \@outputpage
          \setbox\z@\vbox{\box\LT@lasthead}%
        \fi
      \fi
      \global\@colroom\@colht
      \global\vsize\@colht
      \vbox
        {\unvbox\z@\box\ifvoid\LT@lastfoot\LT@foot\else\LT@lastfoot\fi}%
    \fi
  \else
    \global\morethanonepagetrue%
    \setbox\@cclv\vbox{\unvbox\@cclv\copy\LT@foot\vss}%
    \@makecol
    \@outputpage
      \global\vsize\@colroom
    \copy\LT@head%%%%%\nobreak
  \fi}
\makeatother
\begin{longtable}{ccc}
    \caption{FIRST} \\\endfirsthead
    \caption{OTHER} \\\endhead
    \caption{LAST} \\\endlasthead
    1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\ 7 \\ 8 \\ 9 \\ 10 \\ 11 \\ 12 \\ 13 \\ 14 \\ 15 \\ 16 \\ 17 \\ 18 \\ 19 \\ 20 \\ 21 \\ 22 \\ 23 \\ 24 \\ 25 \\ 26 \\ 27 \\ 28 \\ 29 \\ 30 \\ \end{longtable}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.