# Theorem decorations that stay with theorem environment

I am typesetting a document using the memoir documentclass. Currently, my theorems have the following definition (using macros provided by the ntheorem package):

% THEOREMS
\theorembodyfont{\itshape}%
\theoremsymbol{}%
\theoremseparator{\ }%
\theoremprework{\needspace{2\baselineskip}\noindent\theoremhang\vspace*{-1.3ex}}%
\theorempostwork{\nopagebreak\noindent\theoremhung}%
\setlength{\theorempreskipamount}{-1ex}
\setlength{\theorempostskipamount}{-1ex}
\newtheorem{theorem}{Theorem}[chapter]%


where the \theoremprework and \theorempostwork respectively use the following two macros to decorate the theorem:

\newcommand{\theoremhang}{% top theorem decoration
\begingroup%
\setlength{\unitlength}{.005\linewidth}% \linewidth/200
\begin{picture}(0,0)(1.5,0)%
\linethickness{0.45pt} \color{black!50}%
\put(-3,2){\line(1,0){206}}% Top line
\multido{\iA=2+-1,\iB=50+-10}{5}{% Top hangs
\color{black!\iB}%
\put(-3,\iA){\line(0,-1){1}}% Top left hang
\put(203,\iA){\line(0,-1){1}}% Top right hang
}%
\end{picture}%
\endgroup%
}%

\newcommand{\theoremhung}{% bottom theorem decoration
\nobreak
\begingroup%
\setlength{\unitlength}{.005\linewidth}% \linewidth/200
\begin{picture}(0,0)(1.5,0)%
\linethickness{0.45pt} \color{black!50}%
\put(-3,0){\line(1,0){206}}% Bottom line
\multido{\iA=0+1,\iB=50+-10}{5}{% Bottom hangs
\color{black!\iB}%
\put(-3,\iA){\line(0,1){1}}% Bottom left hang
\put(203,\iA){\line(0,1){1}}% Bottom right hang
}%
\end{picture}%
\endgroup%
}%


Here is a common view of a theorem based on these definitions:

The problem I am experiencing occurs around page breaking, where either of the graphics (\theoremhang or \theoremhung) occur close to the end of the page. I sometimes have \theoremhang being orphaned at the bottom of a page and not 'sticking' to the theorem environment. Other times \theoremhung ends up being orphaned on a following page if the theorem environment ends right at the bottom of the page.

My attempts at trying to keep things together used \needspace and some negative vertical alignment (via \vspaces). Although the former may help, the latter feels like a bit of a hack and neither sticks to the theorem environment like I'd hope it would. Any idea how I can fix the problem and perhaps even optimize this look?

For completeness, here is a minimal working example containing the above definitions (including a proof environment, also via ntheorem) as well as a sample theorem/proof:

\documentclass{memoir}
\usepackage{amsmath}
\usepackage[amsmath]{ntheorem}%
\usepackage{lipsum,multido,graphicx,xcolor}

% THEOREMS
\theorembodyfont{\itshape}%
\theoremsymbol{}%
\theoremseparator{\ }%
\theoremprework{\needspace{2\baselineskip}\noindent\theoremhang\vspace*{-1.3ex}}%
\theorempostwork{\nopagebreak\noindent\theoremhung}%
\setlength{\theorempreskipamount}{-1ex}
\setlength{\theorempostskipamount}{-1ex}
\newtheorem{theorem}{Theorem}[chapter]%

% PROOF
\theorembodyfont{\upshape}%
\theoremstyle{nonumberplain}%
\theoremseparator{\ }%
\theoremsymbol{}%
\setlength{\theorempreskipamount}{1ex}%
\newtheorem{proof}{Proof}%

\newcommand{\theoremhang}{% top theorem decoration
\begingroup%
\setlength{\unitlength}{.005\linewidth}% \linewidth/200
\begin{picture}(0,0)(1.5,0)%
\linethickness{0.45pt} \color{black!50}%
\put(-3,2){\line(1,0){206}}% Top line
\multido{\iA=2+-1,\iB=50+-10}{5}{% Top hangs
\color{black!\iB}%
\put(-3,\iA){\line(0,-1){1}}% Top left hang
\put(203,\iA){\line(0,-1){1}}% Top right hang
}%
\end{picture}%
\endgroup%
}%

\newcommand{\theoremhung}{% bottom theorem decoration
\nobreak
\begingroup%
\setlength{\unitlength}{.005\linewidth}% \linewidth/200
\begin{picture}(0,0)(1.5,0)%
\linethickness{0.45pt} \color{black!50}%
\put(-3,0){\line(1,0){206}}% Bottom line
\multido{\iA=0+1,\iB=50+-10}{5}{% Bottom hangs
\color{black!\iB}%
\put(-3,\iA){\line(0,1){1}}% Bottom left hang
\put(203,\iA){\line(0,1){1}}% Bottom right hang
}%
\end{picture}%
\endgroup%
}%

\begin{document}
\chapter{First chapter}
\lipsum[1]

\begin{theorem}%
If~$G$ is a connected graph of order $n\geq 3$ and size~$m$, then
$g(G)\geq \frac{m}{6}-\frac{n}{2}+1.%$

\begin{proof}%
Suppose a connected graph~$G$ of order $n\geq 3$ and size~$m$ is embedded in a surface of genus~$g(G)$, thus producing~$f$ faces. Then it follows by Theorem~XX that every face is a 2-cell and hence by Theorem~YY that $n-m+f=2-2\,g(G)$. An identical argument as in the proof of Theorem~ZZ shows that $3f\leq 2m$ and so
$2-2\,g(G)=n-m+f\leq n-m+\frac{2m}{3},%$
from which the desired inequality follows.%
\end{proof}%
\end{theorem}%

\lipsum[2]

\end{document}

-

The problem is that theorems are lists and lists introduce break points at their beginning and ending; as you put proofs inside theorems, your theorem ending has two break points (so preventing the break needs to overcome both).

The simplest would probably be to rewrite the theorem environments without using lists, but that means loosing (or rewritting) support of ntheorem features. And it's not guaranteed it will always work as things like $...$ may also introduce break points (but setting \postdisplaypenalty=10000 inside the theorem does the trick).

Here's an attempt; I have tried to eliminated all possible break points, but using things like {center}, {enumerate}, {itemize} might introduce a break point (with the enumitem package you can use the endpenalty=10000 key to prevent this). This implementation supposes that your proofs will always end just before \end{theorem}.

\documentclass{memoir}

\usepackage{amsmath}
\usepackage{lipsum}
\usepackage{multido,xcolor}

\newcounter{theorem}[chapter]
\newenvironment{theorem}
{\par\noindent\theoremhang\par\nobreak\noindent
\refstepcounter{theorem}\postdisplaypenalty=10000 %
{\sffamily\bfseries\upshape Theorem \thetheorem}\ \ignorespaces}

\newenvironment{proof}
{\par\nobreak}% no spacing here so that supposes you will always use {proof} inside {theorem} or else there might be some problems.

\newcommand{\theoremhang}{% top theorem decoration
\begingroup%
\setlength{\unitlength}{.005\linewidth}% \linewidth/200
\begin{picture}(0,0)(1.5,0)%
\linethickness{0.45pt} \color{black!50}%
\put(-3,2){\line(1,0){206}}% Top line
\multido{\iA=2+-1,\iB=50+-10}{5}{% Top hangs
\color{black!\iB}%
\put(-3,\iA){\line(0,-1){1}}% Top left hang
\put(203,\iA){\line(0,-1){1}}% Top right hang
}%
\end{picture}%
\endgroup%
}%

\newcommand{\theoremhung}{% bottom theorem decoration
\begingroup%
\setlength{\unitlength}{.005\linewidth}% \linewidth/200
\begin{picture}(0,0)(1.5,0)%
\linethickness{0.45pt} \color{black!50}%
\put(-3,0){\line(1,0){206}}% Bottom line
\multido{\iA=0+1,\iB=50+-10}{5}{% Bottom hangs
\color{black!\iB}%
\put(-3,\iA){\line(0,1){1}}% Bottom left hang
\put(203,\iA){\line(0,1){1}}% Bottom right hang
}%
\end{picture}%
\endgroup%
}%

\begin{document}

\chapter{First chapter}
\lipsum[1]

\begin{theorem}%
If~$G$ is a connected graph of order $n\geq 3$ and size~$m$, then
$g(G)\geq \frac{m}{6}-\frac{n}{2}+1.%$

\begin{proof}%
Suppose a connected graph~$G$ of order $n\geq 3$ and size~$m$ is embedded in a surface of genus~$g(G)$, thus producing~$f$ faces. Then it follows by Theorem~XX that every face is a 2-cell and hence by Theorem~YY that $n-m+f=2-2\,g(G)$. An identical argument as in the proof of Theorem~ZZ shows that $3f\leq 2m$ and so
$2-2\,g(G)=n-m+f\leq n-m+\frac{2m}{3},%$
from which the desired inequality follows.%
\end{proof}%
\end{theorem}%

\lipsum[2]

\begin{theorem}%
If~$G$ is a connected graph of order $n\geq 3$ and size~$m$, then
$g(G)\geq \frac{m}{6}-\frac{n}{2}+1.%$
\end{theorem}%

\lipsum[3]

\begin{theorem}%
\lipsum[4]
\end{theorem}%

\lipsum[5]

\end{document}

-