I am trying to build a long table (and not two tables stuck together). The first two pages should have the same heading while the last one should change to another heading. I tried using the code in this question (Change \endhead in the middle of a longtable), however, my caption comes at the very beginning just after \begin(longtable) and this causes the following error to \endfirsthead and \endhead "Package longtable error: Longtable head or foot not at the start of the table".
Using the same example in the mentioned question, just after adding a caption after \begin(longtable) an error appears. I just need to add a caption as the first thing of the table heading and be repeated with the heading once a new page occurs. Here is the code with that causes the error:
\documentclass[a4paper]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{longtable}
\begin{document}
\newbox\LTheadsave
\makeatletter
\newcommand\saveLThead{\noalign{\global\setbox\LTheadsave\copy\LT@head}}
\newcommand\restoreLThead{\noalign{\global\setbox\LT@head\copy\LTheadsave}}
\makeatother
\setcounter{LTchunksize}{2}
\begin{longtable}{lc}
\caption{Example error}
\toprule
Question 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\endfirsthead
\multicolumn{2}{@{}l}{\textbf{Table} \ref{tab: example} continued}\\
\toprule
%\textbf{Table} \ref{tab: example} xyz\\
Question 2 (cont.) 1 & N \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\endhead\saveLThead
\multicolumn{2}{@{}l}{\textbf{Table} \ref{tab: example} continued}\\
\toprule
%\textbf{Table} \ref{tab: example} xyz\\
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 \\
& \\\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 1 & 4 \\\restoreLThead
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 \\
\label{tab: example}
\end{longtable}
\end{document}
Any help in this please!
Thank you. Zeinab (She/her)
lasthead
, you may trytabularray
package.