# Lasthead section in a longtabu environment

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.

(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
\ifdim\dimen@<\ht\z@
\setbox\@cclv\vbox{\unvbox\z@\copy\LT@foot\vss}%
\@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\@cclv\vbox{\unvbox\@cclv\copy\LT@foot\vss}%
\@makecol
\@outputpage
\global\vsize\@colroom
\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
\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\@cclv\vbox{\unvbox\z@\copy\LT@foot\vss}%
\@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
\global\morethanonepagetrue%
\setbox\@cclv\vbox{\unvbox\@cclv\copy\LT@foot\vss}%
\@makecol
\@outputpage
\global\vsize\@colroom