# Breaking a theorem between pages

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):

-
Would you do the same for the section titles? I guess no. I don't think such a "title" is really needed. –  egreg Sep 17 '12 at 6:27
@egreg For sections it would be obvious whether or not the text on the next page is a part of the section or not, even if a new paragraph is starting. (Since any text is, at least in my case, always a part of some section.) It's not as clear for this environment. I will update my question to show better what I mean. –  MayGodBlessKnuth Sep 17 '12 at 9:35

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

\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
\newcommand*{\mycont@thm}[3]{%
\def\mycont@counter{#2}%
\def\mycont@name{#3}%
\org@thm{%
#1%
}%
}{#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.
\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{%
\toks@\expandafter{%
\expandafter\afterpage\expandafter{%
\the\toks@
\centerline{% centered or \leftline for flush left
% \leftline{%
% \leftline{\kern\parindent
\color@begingroup
\mycont@name
\ifx\mycont@counter\@empty % support unnumbered theorems
\else
~\csname the\mycont@counter\endcsname
\fi
~(Cont.)%
\color@endgroup
}%
}%
}%
}%
\the\toks@
\endgroup
}
\makeatother

\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}


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

-
Thanks, this seems to work! Is there any way to make this a bit more general, so that I can type in a command like \addcontinuationsupport{myexample}? This would basically "enable" your code on myexample. Then I could choose to type this command (in the preamble) on this, or any other custom theorem environments as needed. A bit more future proof. –  MayGodBlessKnuth Sep 19 '12 at 22:07
@MayGodBlessKnuth Done. The code was already prepared for this purpose. –  Heiko Oberdiek Sep 19 '12 at 22:19
Thanks, this works. However, any chance you can add a support for non-numbered environments too, i.e. when I do \newtheorem*{myexample}{Example} instead (with a star)? I hope it's not too much of a hassle to upgrade your code to support this. (Currently, this leads to an error like this: "You can't use \relax' after \the.".) –  MayGodBlessKnuth Sep 20 '12 at 0:29
Of course, it works to just duplicate the code and comment out ~\csname the\mycont@counter\endcsname for this case, but it sure would be nice to have support for both numbered and non-numbered in a single command. –  MayGodBlessKnuth Sep 21 '12 at 11:00
I have added support for unnumbered theorems. –  Heiko Oberdiek Sep 21 '12 at 11:08

I would suggest you to use \newmdtheoremenv (from the mdframed package) to define your environment; using middleextra and secondextra you can easily achieve what you want; a little example (the legend "Example (Cont.)" will appear boldfaced and framed, but of course, you can choose the formatting that best suits your needs):

\documentclass{article}
\usepackage[paperwidth=12cm,paperheight=10cm]{geometry}% JUST FOR THE EXAMPLE
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\newmdtheoremenv[
hidealllines=true,
innerleftmargin=0pt,
innerrightmargin=0pt,
middleextra={%
\node[draw,anchor=west,yshift=0.7cm] at (P-|O) {\bfseries Example~\theexample~(Cont.)};
},
secondextra={%
\node[draw,anchor=west,yshift=0.7cm] at (P-|O) {\bfseries Example~\theexample~(Cont.)};
}
]{example}{Example}

\begin{document}

\begin{example}
\lipsum[1-3]
\end{example}
\begin{example}
\lipsum[1-3]
\end{example}

\end{document}


Of course, one can use a predefined style or define a own theorem style using \newtheoremstyle; here's an example using the settings for myexamplestyle as given in the question:

\documentclass{article}
\usepackage[paperwidth=12cm,paperheight=10cm]{geometry}% just for the example
\usepackage{amsthm}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\newtheoremstyle{myexamplestyle}
{}{}{}{}{\bfseries}{.}{ }{}
\theoremstyle{myexamplestyle}
\newmdtheoremenv[
hidealllines=true,
innerleftmargin=0pt,
innerrightmargin=0pt,
middleextra={%
\node[anchor=west,yshift=0.7cm,inner xsep=0pt] at (P-|O) {\bfseries Example~\theexample~(Cont.)};
},
secondextra={%
\node[anchor=west,yshift=0.7cm,inner xsep=0pt] at (P-|O) {\bfseries Example~\theexample~(Cont.)};
}
]{example}{Example}

\begin{document}

\begin{example}
\lipsum[1-3]
\end{example}
\begin{example}
\lipsum[1-3]
\end{example}

\end{document}


And here's another option using this time the thmtools package as a front-end for amsthm; using this approach, you define a style (compatible with the amsthm definitions) that can be applied to as many theorem-like structures (numbered or unnumbered) as you like. In the example below I show the style and how to use it for a numbered structure and for an unnumbered one; I also show how one can modify attributes for the structures, but keeping the "Continuation" style:

\documentclass{article}
\usepackage[paperwidth=12cm,paperheight=10cm]{geometry}% just for the example
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\makeatletter
\declaretheoremstyle[
notefont=\mdseries, notebraces={(}{)},
bodyfont=\normalfont,
\begin{mdframed}[hidealllines=true,
innerleftmargin=0pt,
innerrightmargin=0pt,
middleextra={\node[anchor=west,yshift=2ex,inner xsep=0pt] at (P-|O) {\bfseries\thmt@thmname~\csname the\thmt@envname\endcsname
~(Cont.)};},%
secondextra={\node[anchor=west,yshift=2ex,inner xsep=0pt] at (P-|O) {\bfseries \thmt@thmname~\csname the\thmt@envname\endcsname~(Cont.)};}%
]},
prefoothook=\end{mdframed}
]{myexamplestyle}
\makeatother
\declaretheorem[style=myexamplestyle,numbered=no]{example}

\begin{document}

\begin{example}
\lipsum[1-3]
\end{example}
\begin{remark}
\lipsum[1-3]
\end{remark}

\end{document}


And another option using \surroundwithmdframed; using this approach the theorems and their styles are defined in the usual way through amsthm (or ntheorem) and the resulting environments are given the desired "Continuation" style with the help of mdframed:

\documentclass{article}
\usepackage[paperwidth=12cm,paperheight=10cm]{geometry}% just for the example
\usepackage{amsthm}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

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

\surroundwithmdframed[
hidealllines=true,
innerleftmargin=0pt,
innerrightmargin=0pt,
middleextra={\node[anchor=west,yshift=2ex,inner xsep=0pt] at (P-|O) {\bfseries Example~(Cont.)};},%
secondextra={\node[anchor=west,yshift=2ex,inner xsep=0pt] at (P-|O) {\bfseries Example~(Cont.)};}%
]{myexample}

\begin{document}

\begin{myexample}
\lipsum[1-3]
\end{myexample}

\end{document}

-
That is a nice solution. I would like to implement it, but I want to give the text a different font style. How can I modify the font? for example, I don't want it to be italics and maybe I would change the font type. I tried adding the key font=\rmfamily\upshape,, but it doesn't work. It does work when I do \begin{example}\rmfamily\upshape..., but I want to define it within the environment definition. Thanks in advance! –  Felipe Aguirre Sep 17 '12 at 8:37
@FelipeAguirre changes in formatting can be done using a predefined theorem style (when using amsthm or ntheorem) or using \newtheoremstyle to further customize the formatting. See my updated answer. –  Gonzalo Medina Sep 17 '12 at 13:23
Thanks for the reply. However, is there any chance you can remove the box, and make it instead appear in the text itself (not above it)? –  MayGodBlessKnuth Sep 17 '12 at 14:00
@GonazaloMedina thank you! @MyGodBlessKnuth: You can do that by erasing the draw option given to the \node` command –  Felipe Aguirre Sep 17 '12 at 15:04
@GonzaloMedina: Nice idea of using tikz for this! –  Aditya Sep 19 '12 at 20:27