Change \endhead in the middle of a longtable

I am creating many longtables in my document, where one table represents the results of a specific topic. Normally the header of the longtable consists of the name of the topic, N and some cmidrules. If the table has to break at some point, the new header changes the name of the topic to topic (cont.).

Now sometimes a topic has two subquestions. If the table breaks after the second question I would like to change the endhead to include the second question. Here is a short MWE:

\documentclass[a4paper]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[latin1]{inputenc}
\usepackage{booktabs}
\usepackage{longtable}

\begin{document}
\begin{longtable}{lc}
\toprule
Question 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\toprule
Question 1 (cont.) 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\bottomrule\endfoot
Item 1 & 4 \\
Item 2 & 10 \\
Item 3 & 2 \\
Item 4 & 8 \\
Item 5 & 20 \\
Item 6 & 3 \\
Item 7 & 6 \\
Item 8 & 1 \\
Item 9 & 11 \\
Item 10 & 6 \\
& \\

Question 2   & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
Item 1 & 4 \\
Item 2 & 10 \\
Item 3 & 2 \\
Item 4 & 8 \\ \newpage
Item 5 & 20 \\
Item 6 & 3 \\
Item 7 & 6 \\
Item 8 & 1 \\
Item 9 & 11 \\
Item 10 & 6 \\
\end{longtable}
\end{document}


Now if the table breaks it would write Question 1 (cont.), but I would like it to be Question 2 (cont.). Is there a way to achieve that goal with longtable or is it possible to build a newcommand like

\newendhead{
Question 2 (cont.)  & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
}


which I can insert at a certain point in the table?

You can use two longtables, but arrange that they share the same column widths:

\documentclass[a4paper]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[latin1]{inputenc}
\usepackage{booktabs}
\usepackage{longtable}

\begin{document}

\begingroup
\makeatletter
\ifx\LT@ii\@undefined\else
\def\LT@entry#1#2{\noexpand\LT@entry{-#1}{#2}}
\xdef\LT@i{\LT@ii}
\fi
\endgroup
\begin{longtable}{lc}
\toprule
Question 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\toprule
Question 1 (cont.) 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\bottomrule\endfoot
Item 1 & 4 \\
Item 2 & 10 \\
Item 3 & 2 \\
Item 4 & 8 \\
Item 5 & 20 \\
Item 6 & 3 \\
Item 7 & 6 \\
Item 8 & 1 \\
Item 9 & 11 \\
Item 10 & 6
\end{longtable}

\begin{longtable}{lc}
\toprule
Question 2 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\toprule
Question 2 (cont.) 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\bottomrule\endfoot
\cmidrule(r){1-1}
\cmidrule(l){2-2}
Item 1 & 4 \\
Item 2 & 10 \\
Item 3 & 2 \\
Item 4 & 8 \\ \newpage
Item 5 & 20 \\
Item 6 & 3 \\
Item 7 & 6 \\
Item 8 & 1 \\
Item 9 & 11 \\
Item 10000widdddeeer col& 6 \\
\end{longtable}
\end{document}


If you need more than two, you need the weird code before each except the last, making in each case \LT@<thistable> be defined to be \LT@<the last> table>

Attention: This is not really meant as code usable in a real document as I didn't test it a lot. But I was curious if one can exchange the longtable headers in the middle of a table and this is the result.

The header of a longtable is stored in a box (\LT@head) and when a longtable chunk is output at the top of the page this box is added. The box is stored at the \endhead command. It is quite possible to have more than one \endhead in a table as long as they are all at the start of longtable. The last will win. It is also possible to exchange the LT@head in the middle of the table. The main problem is to do it in the right moment so that it isn't used too early, a small LTchunksize is useful here.

It is probably not a good idea to do this with boxes with different height.

\documentclass[a4paper]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{longtable}

\begin{document}
\makeatletter
\makeatother

\setcounter{LTchunksize}{2}
\begin{longtable}{lc}
\toprule
Question 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\toprule
Question 2 (cont.) 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\toprule
Question 1 (cont.) 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}

\bottomrule\endfoot
Item 1 & 4 \\
Item 2 & 10 \\
Item 3 & 2 \\
Item 4 & 8 \\
Item 5 & 20 \\
Item 6 & 3 \\
Item 7 & 6 \\
Item 8 & 1 \\
Item 9 & 11 \\
Item 10 & 6 \\
& \\\newpage
Item 1 & 4 \\
Item 2 & 10 \\
Item 3 & 2 \\
Item 4 & 8 \\
Item 5 & 20 \\
Item 6 & 3 \\
Item 7 & 6 \\
Item 8 & 1 \\
Item 9 & 11 \\
Item 10 & 6 \\
&\\

Question 2   & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
Item 2 & 10 \\
Item 3 & 2 \\
Item 4 & 8 \\
Item 5 & 20 \\
Item 6 & 3 \\\newpage
Item 7 & 6 \\
Item 8 & 1 \\
Item 9 & 11 \\
Item 10 & 6 \\
\end{longtable}

\end{document}


• yes wondered about that too:-) As always there's some variablity in when that gets executed relative to the output routine breaking the page, but as there is typically no vertical flexibility in a table, in practice the output routine always triggers in the only possible breakpoint so this is probably safe enough if chunksize is small – David Carlisle Aug 28 '15 at 11:48
• @DavidCarlisle: One could probably move the restore command a bit down to be on the safe side - after rethinking: if it is to much down it could come to late. The main question is what should happen if the pagebreak is directly before question 2? – Ulrike Fischer Aug 28 '15 at 11:51