thmtools:如何防止定理和证明之间的分页?

thmtools:如何防止定理和证明之间的分页?

我正在使用 thmtools 来格式化定理及其证明的集合。

有什么方法可以防止在定理陈述的最后一行和证明的第一行之间出现分页符?

我尝试插入\nopagebreak,像这样

\end{thorem}
\nopagebreak[4]
\begin{proof}

...但它无法抑制分页符。

任何一页上第一个证明的第一行应该至少有两行定理陈述。(证明总是紧跟在陈述之后;中间绝不会存在不属于任何一个环境的文本)。

仅供参考:我在 thmtools 文档中搜索了“pagebreak”、“page break”、“nopagebreak”的多种变体,但没有找到。

谢谢!

答案1

鉴于定理和证明环境是完全分开的,完全禁止它们之间的所有分页符确实有点棘手。我只能建议你加载包needspace并插入命令

\Needspace{3\baselineskip} 

接近定理/推论/其他陈述的结尾;注意大写在宏的开头。根据软件包手册needspace,命令“\Needspace{} 效率低于 \needspace{},但保留了所需的空间。它只应在段落之间使用。”鉴于您基本上面临这种情况,您可能想尝试一下。

您可能需要进行一些练习来确定此命令的最佳位置以及所需的基线跳过次数,以便始终使证明语句的至少两行与相关定理显示在同一页面上。

答案2

下面创建一个命令,\reservespace如果将(整个)定理作为参数传入,它将使用该\needspace命令确保定理后没有分页符。

\usepackage{needspace,calc}
\newlength{\heightRecaller}
\newcommand{\reservespace}[1]{%
    \let \oldstepcounter \stepcounter%
    \renewcommand{\stepcounter}[1]{}%
    \settototalheight{\heightRecaller}{\parbox{\textwidth}{#1}}%
    \let \stepcounter \oldstepcounter%
    \needspace{\heightRecaller+4\baselineskip}%
    #1%
}

下面是一个完整的工作示例,使用amsthm。以下是一些注意事项:

  • 如果不临时重新定义\stepcounter,任何增加的计数器都会增加两次——一次是在仅用于计算高度的“幻影传递”期间,另一次是在定理的实际排版期间。然而,这种重新定义可能会产生超出我有限知识范围的不良副作用,因此手动重置所有您期望使用的计数器可能更安全。我的答案最下面有一个“更安全的版本”。
  • 使用此命令,无法将定理陈述拆分到跨页,如果有很长的陈述,这可能会有问题。
  • 该命令似乎增加了使用它的定理陈述之前的空间。(请注意,如果在定理陈述之前使用\needspace\Needspace命令,似乎也会发生同样的情况,因此这个困难并不特定于这个答案。)

完整的工作示例,说明了促使我研究这个问题的必要性 - 即,当解决方案框与问题陈述不在同一页面上时,我的学生会感到困惑:

\documentclass[letterpaper]{article}
\usepackage{lipsum}

\usepackage{amsthm}
\theoremstyle{plain}
\newtheorem{exercise}{Exercise}

\usepackage{xcolor}
\newcommand{\blank}[1]{\textcolor{white}{#1}}
%\newcommand{\blank}[1]{#1}

\usepackage{framed}
\newenvironment{solution}%
{\begin{proof}[Solution]\begin{oframed}}%
{\end{oframed}\end{proof}}

\usepackage{needspace,calc}
\newlength{\heightRecaller}
\newcommand{\reservespace}[1]{%
    \let \oldstepcounter \stepcounter%
    \renewcommand{\stepcounter}[1]{}%
    \settototalheight{\heightRecaller}{\parbox{\textwidth}{#1}}%
    \let \stepcounter \oldstepcounter%
    \needspace{\heightRecaller+4\baselineskip}%
    #1%
}

\begin{document}
\lipsum[1-4]
\reservespace{
\begin{exercise}
This is an exercise that involves an equation, which is printed below.
\begin{equation}
1+1=2
\end{equation}
Please study the equation and somehow do something in the Solution box,
which may or may not be filled in depending on which line is commented out.
Please study the equation and somehow do something in the Solution box,
which may or may not be filled in depending on which line is commented out.
\end{exercise}
}
\begin{solution}
\blank{%
\lipsum[5-6]\qedhere%
}
\end{solution}
%
%
\reservespace{
\begin{exercise}
Another exercise goes here.
\end{exercise}
}
\begin{solution}
\blank{%
\lipsum[7]\qedhere%
}
\end{solution}
\end{document}

这是该命令的“更安全版本” \reservespace,可用于上述工作示例。(它手动重置equationexercise计数器,而不是临时重新定义\stepcounter。)

\usepackage{needspace,calc}
\newlength{\heightRecaller}
\newcounter{temp_exercise}
\newcounter{temp_equation}
\newcommand{\reservespace}[1]{
\setcounter{temp_exercise}{\value{exercise}}
    \setcounter{temp_equation}{\value{equation}}
    \settototalheight{\heightRecaller}{\parbox{\textwidth}{#1}}
    \setcounter{exercise}{\value{temp_exercise}}
    \setcounter{equation}{\value{temp_equation}}
    \needspace{\heightRecaller+4\baselineskip}
    #1
}

相关内容