[Tex/LaTex] Reacting to a page break in LaTeX

page-breaking

I'm trying to build an environment where if a page breaks in the environment some additional text is automatically printed at the end of the page, while this text is not printed when no page break occurs. So, for example:

\begin{special_env}{Please also check following page}
  A lot of text goes here...
  And even more text goes here...
\end{special_env}

Should print the text in the environment as normal, but whenever a page break occurs should add "Please also check following page" to the bottom of the page.

I know things like longtable can do this, but my text isn't really a table and I'm not sufficiently advanced in my TeX skills to figure out how they do it. I've seen Add customized text at an optional page-break, which almost does what I need except it requires me to already know approximately where the page break will be. I've also seen Add customized text at an optional page-break, which again seems to focus too much on tables (unless I'm misunderstanding).

My question is, how can I code this so that it will automatically figure out where the page breaks end up being and inserts the text where needed?

While I have some experience as a LaTeX user and have occasionally ventured into doing some slightly more advanced scripting, my knowledge is really rather limited, so please bear with me while I try to digest your answers.

[Edit]

Thanks both for your help. I have played with the different variants proposed, but haven't quite been able to achieve what I need. It's close, but not quite there yet. Let me give you a little bit more detail on what I'm trying to achieve. I am extending a class that defines a question environment for the purposes of writing exam papers. When a question continues on a new page, we're required to add "Question X continues on next page" on that page. The class currently uses an interesting method to determine whether to page break after a question.

A document then consists essentially of a \maketitle followed by a sequence of question environments, normally with no other text between them.

At the moment, my code looks somewhat like this:

\documentclass{article}

\usepackage{lipsum}

\makeatletter

\newif\ifinenv
\inenvfalse

\newcommand \@@footer{\ifinenv {QUESTION \thequestion\ CONTINUES ON NEXT PAGE} \fi}

\def \ps@exam{%
  \let \@mkboth \@gobbletwo%
  \def \@oddhead{}%
  \def \@oddfoot{\@@footer}%
  \def \@evenhead{}%
  \def \@evenfoot{\@@footer}%
}
\pagestyle{exam}

\newcommand \testpagebreak[1]{%
  \vfil%
  \penalty #1%
  \vfilneg%
}

\newcounter{question}

\newenvironment{question}{%
  \refstepcounter{question}%
  \inenvtrue
  \begin{list}{}{}%
      \item[\bf \thequestion.]% 
}{%
  \end{list}%
  \testpagebreak{-350}%
}

\begin{document}

  \begin{question}
    \lipsum[1-7]
  \end{question}

  \begin{question}
    \lipsum[8-13]
  \end{question}

  \begin{question}
    Just a single line

    And another
  \end{question}
\end{document}

This works quite well, except where the start of a question is pushed to the start of a new page. In this case, the PTO text will be printed on the page before the question starts, so for example:

Text of Question 1
Question 2 continues on the next page
----page break----
Text of Question 2

Clearly the problem is that the start of the environment of Question 2 is still processed before TeX decides to open a new page. If I replace the \testpagebreak code with a hard \newpage or leave it out completely all is fine. Is there a way to fiddle with this code to make it work with \testpagebreak (or a variant thereof)?

[/Edit]

[Edit 2]

I've experimented some more based on @Werner's idea. My code can be found at PasteBin. Note that in the second question there are two alternative versions of the contents, a short one and a longer one. Using the short one (so that Question 2 doesn't actually span multiple pages) all is well. Using the long one (so that Question 2 does span multiple pages) the "Question 2 continues" text is shown already on the last page of Question 1; that is one page too early.

From this, I deduce that the label mechanism works correctly. However, the code in the footer doesn't always correctly pick up whether a question actually starts on the page currently being shipped out. I have no idea how to fix this, but maybe someone out there can point me in a useful direction?

[/Edit 2]

Many thanks in advance,

Steffen

Best Answer

What about the following idea?

It's much different from yours (or perhaps the class author's), but perhaps more robust as it uses the \label-\ref system to determine whether a page break has occurred mid-question:

enter image description here

\documentclass{article}

\usepackage[paper=a5paper,margin=1in]{geometry}% Just for this example
\usepackage{multido}% Just for this example
\usepackage[nopar]{lipsum}% Just for this example
\usepackage{refcount}
\usepackage{fancyhdr}

\newcounter{question}

\newenvironment{question}{%
    \begin{list}{}{}%
      \refstepcounter{question}%
      \item[\bfseries\thequestion.]%
      \leavevmode% Start paragraph
      \label{question-start-\thequestion}%
  }{%
    \label{question-end-\thequestion}%
    \end{list}
  }

\pagestyle{fancy}
\fancyhf{}% Clear header/footer
\fancyfoot[L]{% Left footer
  \ifnum\getpagerefnumber{question-start-\thequestion}<\getpagerefnumber{question-end-\thequestion}
    Question~\thequestion{} continues on the next page%
  \fi}
\fancyfoot[R]{See next page}% Right footer
\renewcommand{\headrulewidth}{0pt}% Remove header rule

\AtEndDocument{\fancyfoot[R]{}}% Remove right footer at end of document

\sloppypar% Just for this example
\begin{document}

\multido{\i=1+1}{50}{%
  \begin{question}
    \lipsum[\i]
  \end{question}
  \bigskip
}

\end{document}

At each \item inside question, a \label is set, and then another \label at the end of the question. A check is made to see whether the page number at the start is different from (less than) the ending page number. Accordingly, the footer sets the appropriate clause.

Related Question