规则和自定义定理环境之间无分页符

规则和自定义定理环境之间无分页符

我正在创建一个自定义定理环境。下面是我的问题的一个最小示例,其中有一些从 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
{\bfseries\thmcolour}% header font
{.}% punctuation
{.5em}% after theorem header
{}% header specification (empty for default)

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

\newcommand{\patchthm}[1]{
  \AtBeginEnvironment{#1}{%
    \goodbreak%
    \begin{adjustwidth}{2.5em}{2.5em}%
    \textcolor{\rulecolour}{%
      \rule{\linewidth}{0.4pt}%
    }\nopagebreak%
  }
  \AtEndEnvironment{#1}{%
    \par\nopagebreak\noindent%
    \textcolor{\rulecolour}{%
      \rule{\linewidth}{0.4pt}%
    }%
    \end{adjustwidth}\par\goodbreak%
  }
}
\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.

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

\end{document}

99% 的时间,这会给出美观的定理,其上下有规则并且有一些缩进,以便定理在页面的其余部分中脱颖而出:

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 

然而,在某些情况下,LaTeX 并不关心我的nopagebreaks,而是以一条规则结束一页,在下一页开始定理/定义文本,这是不好的。

我希望 LaTeX 永远不要在开头规则和定理文本之间或定理文本结尾和结束规则之间设置分页符。如果定理包含多个段落,那么只要规则不单独占据一页,我就不会在意它们之间的分页符。

有什么建议吗?或者我犯了一个简单的错误,比如把 nopagebreak 放错了位置?或者有没有更好的方法来实现我想要的效果?

编辑:我把它变成了 MWE——当我编译它时,定义的开头规则位于第 1 页,其余部分位于第 2 页。

我还rulecolour从命令更改\color为命令\textcolor,因为这个问题显然color,它产生了一个“whatsit”(???)并\goodbreak在开场规则之前放置了一个,但是仍然没有运气。

相关内容