# nopagebreak between rule and custom theorem env

I'm making a custom theorem environment. Here's a minimal example of my problem, with some filler text borrowed from the GPL:

\documentclass{scrbook}

\usepackage[dvipsnames]{xcolor}
\usepackage{amsthm}
\usepackage{etoolbox}
\usepackage{changepage}

\newcommand{\thmcolour}{\color{blue}}
\newcommand{\rulecolour}{blue}

% Indent theorem-like environments on both sides.
\newtheoremstyle{mytheorem}
{3pt}% space before
{3pt}% space after
{}% body font
{}% indent
{.}% punctuation
{}% header specification (empty for default)

\theoremstyle{mytheorem}\newtheorem{definition}{Definition}[chapter]

\newcommand{\patchthm}[1]{
\AtBeginEnvironment{#1}{%
\goodbreak%
\textcolor{\rulecolour}{%
\rule{\linewidth}{0.4pt}%
}\nopagebreak%
}
\AtEndEnvironment{#1}{%
\par\nopagebreak\noindent%
\textcolor{\rulecolour}{%
\rule{\linewidth}{0.4pt}%
}%
}
}
\patchthm{definition}

\begin{document}

Here is some text. The following rule represents a large figure that takes up a lot of space on the page.

\rule{0.5cm}{12cm}

The GNU General Public License is a free, copyleft license for software and other kinds of works.

The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too.

When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.

The following definition breaks in the wrong place with my page margins.

If a rule is separated from its content, this is bad.
\end{definition}

\end{document}


99% of the time this gives good-looking theorems with rules above and below and some indentation so that theorems stand out from the rest of the page:

text text text text text text text text text text text text

-------------------------------------------------

Definition 4 (foo). text text text text text text
text text text text text text text text text ...

-------------------------------------------------

text text text text text text text text text text text text


However, in some cases LaTeX doesn't care about my nopagebreaks and ends one page with a rule, starting the theorem/definition text on the next page which is bad.

What I'd like is for LaTeX never to make a pagebreak between the opening rule and a theorem's text or the end of a theorem's text and a closing rule. If a theorem contains several paragraphs then I don't care about pagebreaks between them as long as the rules don't stand on a page on their own.

Any suggestions, or have I made a simple mistake like putting the nopagebreak in the wrong place? Or is there a better way to get the effect I'm after?

EDIT: I've turned it into a MWE - when I compile this, the opening rule of the definition lands on page 1 and the rest on page 2.

I also changed rulecolour from a \color to a \textcolor command due to this question as color apparently produces a "whatsit" (???) and put a \goodbreak before the opening rule, still no luck though.

• perhaps the needspace package might be helpful here. theoremprework Error with ntheorem demonstrates with ntheorem but you should be able to adapt it :) – cmhughes Jun 27 '13 at 14:52
• without an example can't say a lot but it is worth changing \rulecolour\rule{\linewidth}{0.4pt}}\nopagebreak to \noindent\rulecolour\rule{\linewidth}{0.4pt}}\par\nopagebreak more like you have at the end. a color command in vertical mode can be strange and \nopagebreak in horizontal mode is rather odd too. – David Carlisle Jun 27 '13 at 20:48
• Example added. Changed color to textcolor. The reason I don't have a par in the opening command is that this produces an empty line between the opening rule and the text of the definition. – Bristol Jun 28 '13 at 8:28