# Longtable - 'know' if first non-header row after page break?

The table I have been tasked with reproducing in LaTeX has a column of child parameters, and to the left of that a column of parents. Since it is organized by parent, the parent is not repeated unless the table breaks. Rows will be variable in height, so I can't just predict where the page will break. Here is an example of what the behavior should be:

---page break---

I've already figured out how to get the left column to 'remember' which parent it should show, by using the collcell package. I need help determining whether a row is the first non-header row on a new page. My instinct was to define the longtable \endfirsthead and \endhead commands differently, so headings after a page break would set a 'newPage' flag, but all headings are calculated at the beginning of setting a longtable, and aren't recalculated on the fly, as I discovered with the following code:

\documentclass{article}
\usepackage{longtable,array}
\begin{document}
\gdef\newPage{0}
\begin{longtable}{| c%
<{\ifnum\newPage=1%
(continued)\gdef\newPage{0}%
\fi
}%
| c | }
A & A1\\\hline
& A2\\\hline
& A3\\\hline\newpage
& A4\\\hline
& A5\\\hline
& A6\\\hline
\end{longtable}
\end{document}


Which results in:

---page break---

Because it calculates the second header at the beginning, page 1 gets the (continued) instead of page 2. I know this wouldn't be an issue if I used the supertabular package instead of longtable, but unfortunately elsewhere I have to use the arydshln package, which is incompatible with supertabular. I realize this is very similar to question 46205, and to this discussion but I am not yet familiar enough with LaTeX to understand how to go about this. Since my ultimate goal is to just know when a page break has happened, not necessarily to recalculate the header on-the-fly, maybe there is another way? If not, maybe someone could be so kind as to explain in more detail what commands I would use?

• One problem is that things get formatted before they break the page. The usual solution is to store information in the aux file for use the next time. This is how longtable know how wide to make the columns on the first page. Aug 5, 2017 at 2:34

You can insert numbered labels in every row and the compare their pageref. The main problem is to get the logic right at the start of the table and at the begin of a new parent:

\documentclass{article}
\usepackage{longtable}
\usepackage{array}
\usepackage[abspage,user]{zref}

\usepackage{etoolbox}
\newbool{startparent}
\newcounter{rowcount}

\makeatletter
\newcommand\checkandsetparent{%
\refstepcounter{rowcount}\zref@label{longtabel-row-\number\c@rowcount}%
\ifnum \zref@extractdefault{longtabel-row-\number\c@rowcount}{abspage}{-1}
= \zref@extractdefault{longtabel-start-\number\c@LT@tables}{abspage}{-1} %same page as start
\global\boolfalse{startparent}
\else
\ifnum \zref@extractdefault{longtabel-row-\number\c@rowcount}{abspage}{-1}
=\zref@extractdefault{longtabel-row-\the\numexpr\c@rowcount-1}{abspage}{-1} %first row
\global\boolfalse{startparent}
\else
\ifbool{startparent}{}{\current@parent\ (continued)}
\fi
\fi      }

\newcommand\parent[1]{\global\booltrue{startparent}\gdef\current@parent{#1}#1}
\newcommand\initlongtable{\zref@label{longtable-start-\the\numexpr \c@LT@tables+1}}
\makeatletter

\begin{document}

\begin{longtable}{| c<{\checkandsetparent}    %
| c | }
% Init
\initlongtable
\parent{X} & X1 \\
& x2\\
\parent{A} & A1\\\hline
& A2\\\hline
& A3\\\hline\newpage
& A4\\\hline
& A5\\\hline
& A6\\\hline
\parent{Y} & Y1\\\newpage
& y2\\\newpage
\parent{Z} & Z1
\end{longtable}
\end{document}


• Thank you very very much for this; I really appreciate you taking the time to explain, and to work this out for me! Aug 7, 2017 at 20:24