Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

My problem is that I have tables that are three pages long. Thus, I would like to have three different captions when using the longtable package.

The similar question How to use a longtable with only one entry in the list of tables does not solve my problem. As far as I know this is how we could use longtable regarded this matter:

\caption{Example of longtable}
\endfirsthead
\caption[]{Continued}
\endhead

However, if the table occupies three pages, so that's how the captions are:

Table 2.1 - Example of longtable (page 1)

Table 2.1 - Continued (page 2)

Table 2.1 - Continued (page 3)

In other words, I can not get different captions for the second and third pages. That's what I want and that's the problem.

share|improve this question
    
Welcome to TeX.sx! We closed your question as it seems to be a duplicate, also to prevent duplicate answers. Please have a look at the answers to the other question linked above. If your question is different, please edit it to show that, we could reopen it for you if desired. –  Stefan Kottwitz Dec 30 '11 at 15:32
    
Sant, I reopened the question now. I also took the liberty to fix some formatting things and brought in a suitable form. You might want to check if it is ok like this. @StefanKottwitz –  Martin Scharrer Dec 30 '11 at 16:38
add comment

1 Answer

longtable has two settings for the headers of the table: \endfirsthead and \endhead. The contents of \endfirsthead will be saved in the save box \LT@firsthead and the contents of \endhead in \LT@head. If the box \LT@firsthead is empty, longtable uses the contents of \LT@head. The same procedure will be used for the footer. In the same way I defined a \endsecondhead

\def\endsecondhead{\LT@end@hd@ft\LT@secondhead}

To use this command I have to define a new save box which will be set only on the second page.

\newbox\LT@secondhead

To make sure that only on the second page this box will be used I have to manipulate the output of longtable. Therefore I use a simple \ifvoid clause.

\ifvoid\LT@secondhead
   \copy\LT@head\nobreak
\else
   \box\LT@secondhead\nobreak
\fi

\ifvoid tests whether the save box \LT@secondhead is empty or not. If you set a second head, the box will be printed by \box. This command prints the box and clears the box. So on the next page the test above is true. In this way you make sure that when no second head is defined the \endhead will be used.

To allow for more than three different headings you must define new save boxes and extend the test.

\documentclass{article}
\usepackage{forloop,longtable}
\newcounter{count}
\setcounter{count}{1}
\makeatletter
\newbox\LT@secondhead
\def\endsecondhead{\LT@end@hd@ft\LT@secondhead}
\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
          \ifvoid\LT@secondhead
           \setbox\z@\vbox{\box\LT@head}%
          \else
           \setbox\z@\vbox{\box\LT@secondhead}%
          \fi
        \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
      \ifvoid\LT@secondhead
         \copy\LT@head\nobreak
      \else
         \box\LT@secondhead\nobreak
      \fi
  \fi}
\makeatother


\begin{document}
\listoftables
\section{section}
\begin{longtable}{c}
\caption{foo}\\\stepcounter{table}\endfirsthead
\caption{foo bar}\\\stepcounter{table}\endsecondhead
\caption{bar}\\\endhead
\whiledo{\value{count} < 100}{\stepcounter{count}
   \arabic{count} \\}
\end{longtable}
\end{document}
share|improve this answer
    
This will work only for three page tables, though. –  egreg Dec 30 '11 at 22:43
    
@egreg: The request were 3 ;-). For more you have to define more boxes which will influence the calculation. –  Marco Daniel Dec 30 '11 at 22:45
    
Maybe you can specify it in your (very clever) answer. –  egreg Dec 30 '11 at 22:47
    
@egreg: Thanks. –  Marco Daniel Dec 30 '11 at 23:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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