[Tex/LaTex] Breaking a theorem between pages

amsmathpage-breakingparagraphstheorems

I currently have this custom myexample theorem environment that is working absolutely great.

\newtheoremstyle{myexamplestyle}{}{}{}{}{\bfseries}{.}{ }{}
\theoremstyle{myexamplestyle}\newtheorem{myexample}{Example}

However, can you please help me enhance this environment such that if it splits between two pages, then an "Example 1 (Cont.)" shows up on the next page?

The "Example 1 (Cont.)" should appear on its own non-indented line/paragraph on the next page, followed by the continuation of the example on the next line. (That continuation may or may not be a new "paragraph", depending on whether a new paragraph is starting on the next page, or whether a paragraph from the previous page is being continued on the next page. In other words, the solution should support both cases.)

This should of course not affect anything else, including my other custom theorem environments and styles.


The purpose of this is that without something like this, the new paragraph on the next page doesn't look like it's a part of the example.

\documentclass{article}

\usepackage{amsthm}
\newtheoremstyle{myexamplestyle}{}{}{}{}{\bfseries}{.}{ }{}
\theoremstyle{myexamplestyle}\newtheorem*{myexample}{Example}

\usepackage{lipsum}

\begin{document}

\lipsum[1-8]
Some more text for custom adjustment. Some more text for custom adjustment. Some more text for custom adjustment. Some more text for custom adjustment. Some more text for custom adjustment. Some more text for custom adjustment. Some more text for custom adjustment. Some more text for custom adjustment. 

\begin{myexample}
  \lipsum[4-5]
  A new paragraph. Is it clear that this is a part of the example?
  \lipsum[6]
\end{myexample}

Isn't the space above this paragraph somewhat confusing? The reader now has
to go back to the last page, and compare spacing, in order to finally realize
that the above paragraph was (probably) a part of the example.

\lipsum[6]

\end{document}

Here is the result (even though you can't really read the text):

result

Best Answer

A solution based on package afterpage. At the begin and end of the environment labels are set to get the page number. Then the continuation text is added at the start of the new pages as often as needed. I have centered the continuation text. For different alignments, \centerline can be replaced by \leftline, \leftline{\kern\parindent...} for left aligned with paragraph indent, or \rightline. Also additional space can be added below the continuation text, see comment in the code.

\documentclass{article}

\usepackage{amsthm}
\newtheoremstyle{myexamplestyle}{}{}{}{}{\bfseries}{.}{ }{}
\theoremstyle{myexamplestyle}
\newtheorem{myexample}{Example}

\usepackage{afterpage}
\usepackage{zref-abspage}

\makeatletter

\newcommand*{\addcontinuationsupport}[1]{%
  \expandafter\let\csname mycont@org@#1\expandafter\endcsname
      \csname#1\endcsname
  \expandafter\renewcommand\expandafter*\csname#1\endcsname{%
    \let\org@thm\@thm
    \let\@thm\mycont@thm
    \csname mycont@org@#1\endcsname
  }%
  \expandafter\let\csname mycont@org@end#1\expandafter\endcsname
      \csname end#1\endcsname
  \expandafter\def\csname end#1\endcsname{%
    \ifhmode\unskip\fi
    \zref@labelbyprops{\themycont z}{abspage}%
    \csname mycont@org@end#1\endcsname
  }%
}

% The labels at the begin and end of the environment
% get label names with the help of a counter
\newcounter{mycont}
\renewcommand*{\themycont}{mycont\the\value{mycont}}

% Cathing data from the begin part of the environment and
% smuggle \mycont@head at the beginning of the theorem
% header.
\newcommand*{\mycont@thm}[3]{%
  \def\mycont@counter{#2}%
  \def\mycont@name{#3}%
  \let\mycont@@head\mycont@head
  \org@thm{%
    #1%
    \thm@headfont\expandafter{%
      \the\thm@headfont
      \mycont@@head
      \global\let\mycont@@head\relax
    }%
  }{#2}{#3}%
}

% At the begin of the theorem add a label, get the number of
% pages and call a nested \afterpage as often as needed.
\newcommand*{\mycont@head}{%
  \stepcounter{mycont}%
  \zref@labelbyprops{\themycont}{abspage}%
  \zref@refused{\themycont}%
  \begingroup 
    \toks@={}%
    \count@=\numexpr\zref@extractdefault{\themycont z}{abspage}{0}%
      -\zref@extractdefault{\themycont}{abspage}{0}\relax
    \@whilenum\count@>\z@\do{%
      \advance\count@\m@ne
      \toks@\expandafter{%
        \expandafter\afterpage\expandafter{%
          \the\toks@
          \centerline{% centered or \leftline for flush left
          % \leftline{%
          % \leftline{\kern\parindent
            \color@begingroup
              \let\mycontHead\relax
              \the\thm@headfont
              \mycont@name
              \ifx\mycont@counter\@empty % support unnumbered theorems
              \else
                ~\csname the\mycont@counter\endcsname
              \fi
              ~(Cont.)%
            \color@endgroup
          }%
        }%  
      }%    
    }%      
    \the\toks@
  \endgroup   
}
\makeatother

\addcontinuationsupport{myexample}

\usepackage{lipsum}

\begin{document}

\lipsum[1-8]
Some more text for custom adjustment. Some more text for custom adjustment.
Some more text for custom adjustment. Some more text for custom adjustment.
Some more text for custom adjustment. Some more text for custom adjustment.
Some more text for custom adjustment. Some more text for custom adjustment.

\begin{myexample}
  \lipsum[4-5]   
  A new paragraph. Is it clear that this is a part of the example?
  \lipsum[7-12]
\end{myexample}

\textbf{After the example.}
Isn't the space above this paragraph somewhat confusing? The reader now has
to go back to the last page, and compare spacing, in order to finally
realize
that the above paragraph was (probably) a part of the example.

\lipsum[6]

\end{document}

Top of pages 3 and 4

Edit: Requested \addcontinuationsupport{<theorem>} added.
Edit: Support of unnumbered theorems added.

Related Question