[Tex/LaTex] Change \endhead in the middle of a longtable

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}
\endfirsthead
\toprule
Question 1 (cont.) 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\endhead
\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?

Best Answer

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}
\endfirsthead
\toprule
Question 1 (cont.) 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\endhead
\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}
\endfirsthead
\toprule
Question 2 (cont.) 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\endhead
\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>