0

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)

5
  • If you only need lasthead, you may try tabularray package.
    – L.J.R.
    Aug 5, 2021 at 13:28
  • Welcome to TSE. Please post a Minimal Working Example. Aug 5, 2021 at 13:32
  • The question is really a duplicate of tex.stackexchange.com/a/263602/1090 if you got an error there is an error in your code but as you have shown no code it is hard to guess what you have done wrong. Aug 5, 2021 at 20:15
  • @JoséCarlosSantos Thank you. I did so now. Aug 9, 2021 at 9:58
  • @DavidCarlisle I am using the exact code as in this example but once I add a starting caption it causes an error. I included the code in my question now. Aug 9, 2021 at 9:59

1 Answer 1

1

You can only change the header between chunks, so \LTchunksize is important. In this example there is only one row per page, hence \def\LTchunksize{1}.

I went to some effort to not change the size of \LT@head, although I don't know if that was really necessary.

\documentclass{article}
\usepackage{longtable}
\usepackage{blindtext}

\makeatletter
\newcommand{\changehead}[1]% #1 = tabular...
{\bgroup
  \sbox0{\makebox[\wd\LT@head][c]{\raisebox{0pt}[\ht\LT@head][\dp\LT@head]{#1}}}%
  \global\setbox\LT@head=\copy0
\egroup}
\makeatother

\begin{document}

\begin{longtable}{p{2in}}
  \multicolumn{1}{c}{Inital Definition}\\
\endhead
\def\LTchunksize{1}% must go after headers
\blindtext\\
\changehead{\begin{tabular}[b]{c}New Definition\end{tabular}}
\blindtext
\end{longtable}
\end{document}
2
  • I don't think you can d it that way in general you have a very untypical 1-column table, but for a multicolumn table you need th ecolumns of the head to line up (I have an ansert on site somewhere, I'll find....) Changing chunksize where you do won't have any effect as it's ju lstocal to the following cell with the blindtext entry. Aug 5, 2021 at 20:08
  • @DavidCarlisle - Ah yes, aligning the columns would be a bit more difficult, but he didn't say exactly what he wanted to replace the head with. Aug 6, 2021 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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