# How to change footer for longtable when table is truly long?

I'm creating a custom environment based on longtable using the following code:

\documentclass[openany,12pt]{book}

\usepackage{longtable}

\newcommand{\LORD}{\textsc{Lord}}
\let\Lord\LORD

\newlength{\psalmindent}
\setlength{\psalmindent}{0.7cm}

\newenvironment{sungpsalm}[2]
{%
\def\antiphon{#1}%
\def\tone{#2}
\newcommand{\pause}{\\*\hspace{\psalmindent}}%
\newcommand{\side}[1]{\\}%
\newcommand{\stanza}{\\\noalign{\penalty-50\vspace{\parskip}}}%
\begin{longtable}[l]{l}%
\antiphon\\*
%for some reason the ifood conditional is working backwards
\ifodd\thepage\textit{\small Continued next page}\fi\endfoot
}
{%
\end{longtable}%
}

\begin{document}
%test\pagebreak

\begin{sungpsalm}{Antiphon test}{tone test}
\side{2} O \Lord, my God, I take refuge in you.\pause
Save and rescue me from all my pursuers,
\side{3} lest they tear me apart like a lion,\pause
and drag me off with no one to rescue me.
\stanza
\side{4} If I have done this, O \Lord, my God,
\side{5} if I have paid back evil for good,\pause
I who saved my unjust oppressor:
\side{6} then let my foe pursue my soul and seize me;\pause
let him trample my life to the ground,\pause
and lay my honor in the dust.
\stanza
\side{7} O \Lord, rise up in your anger;\pause
be exalted against the fury of my foes.\pause
Awake, my God, to enact \pause
the justice you ordered.
\side{8} Let the company of peoples gather round you,\pause
as you take your seat above them on high.
\stanza
\side{9} The \Lord\ is judge of the peoples.\pause
Give judgment for me, O \Lord,\pause
for I am just and blameless of heart.
\stanza
\side{10} Put an end to the evil of the wicked!\pause
Make the just man stand firm;\pause
it is you who test mind and heart,\pause
O God of justice!
\stanza
\side{11} God is a shield before me,\pause
who saves the upright of heart.
\side{12} God is a judge, just and powerful and patient,\pause
not exercising anger every day.
\stanza
\side{13} Against someone who does not repent,\pause
God will sharpen his sword;\pause
he bends his bow and makes ready.
\side{14} For such a one he prepares deadly weapons;\pause
he barbs his arrows with fire.
\stanza
\side{15} Here is one who conceives iniquity;\pause
pregnant with malice, he gives birth to lies.
\side{16} He digs a pit and bores it deep;\pause
and in the trap he has made he falls.
\side{17} His malice recoils on his head;\pause
on his own skull his violence falls.
\stanza
\side{18} I thank the \Lord\ for his justice,\pause
singing to the name of the \Lord, the Most High.\pause

\end{sungpsalm}

\end{document}


Now, since in the actual use case the two arguments to my environment will be \includegraphics commands pointing to pictures of music, I'm trying to conserve space by only printing each piece of music once per two page spread (and printing a simple text reference otherwise). The above code does this nicely for the second argument (which ends up in \tone) but not for the first (which ends up in \antiphon). The conditional \ifnewspread always seems to be false. I suspect this is because \endlastfoot is putting the final footer into a box and the \ifnewspread clause is getting evaluated when the box is filled rather than when it is printed. However, even if I move that clause out into a separate function (\terminate) and then say \expandafter\terminate\endlastfoot or \noexpand\terminate\endlastfoot things still don't work (the former shows the same behavior as the MWE, the latter fails to print the last footer altogether).

Is there some special incantation that I can do so that the last footer will evaluate that conditional when it is printed and not before?

• It would probably be easier to change the page style and put it in the page footer. – cfr Dec 16 '16 at 4:31
• the table head and foot are a box so have fixed text, and are set at the start of the table, but in general you can never test the page number within the body of the document as page breaking acts at a different time. You need to use marks as in section header code or \label and \pageref/ – David Carlisle Dec 16 '16 at 7:47
• @DavidCarlisle When the \head is executed, a page break has occurred just before it. So IMHO there is no uncertainty about the page number. Similarly, the \foot will be inserted before the page break. Isn't that how it works? – Pieter van Oostrum Dec 16 '16 at 9:19
• @PietvanOostrum no:-) the table head and foot boxes are set at the start, and the head isn't added in the output routine but actually in the page (probably a mistake but related to the fact that a longtable can start mid-page) – David Carlisle Dec 16 '16 at 9:39
• @DavidCarlisle Sad :( – Pieter van Oostrum Dec 16 '16 at 9:45

The \newspreadtrue is evaluated locally (in a table cell), so it doesn't reach the outside world. So use \global\newspreadtrue instead. Then it works.
And moreover, once it is set to true, it never becomes false again. Is that what you want. Maybe the \newspreadfalse initialisation should be moved to the beginning of the sungpsalm environment?