1

I would like to control the exact penalty for page-breaking between a theorem and proof. For that reason, I cannot use the needspace package as suggested here. I am using both amsthm and cleveref, so upon digging around in the source code I figured out that the final theorems are generated by \cref@thmnoarg and \cref@thmoptarg. I tried inserting a \nobreak before the \trivlist in each of them, but it did not work. Neither did changing \@beginparpenalty to the maximum value just before the \trivlist. I included my code below.

Note that in the special case that the desired penalty is the maximum (i.e. \nobreak), the result should have the page-break before the second theorem. Also note that I do want the penalty after a theorem to be low (i.e. 150) unless it is immediately followed by a proof. But I am unable to figure out how to achieve that.

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{mathptmx}
\usepackage[T1]{fontenc}
\usepackage{etoolbox}
\usepackage{setspace}

\let\oldtrivlist\trivlist \makeatletter \def\nobreaklist{\nobreak\oldtrivlist} \patchcmd{\cref@thmnoarg}{\trivlist}{\nobreaklist}{}{} \patchcmd{\cref@thmoptarg}{\trivlist}{\nobreaklist}{}{} \makeatother

\usepackage{enumitem}

\newtheoremstyle{thm}{0em}{0em}{\normalfont}{0em}{\bfseries}{.}{.3em plus .3em}{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}} \newtheoremstyle{prf}{0em}{0em}{\normalfont}{0em}{\itshape}{.}{.3em plus .3em}{\thmname{#1}\thmnote{ (#3)}} \newlength{\thmspace}\setlength{\thmspace}{.3em plus .1em minus .1em} \newlength{\prfspace}\setlength{\prfspace}{.3em plus .1em minus .1em} \newcommand\thmbefore{\par\addvspace{\thmspace}\vspace{\parskip}\addpenalty{-200}} \newcommand\prfbefore{\par\addvspace{\prfspace}\vspace{\parskip}} \newcommand\thmafter{\par\addvspace{\thmspace}\addpenalty{-150}} \newcommand\prfafter{\par\addvspace{\prfspace}\addpenalty{-150}} \newcommand\thmbegin{\setlength{\parskip}{0em}} \theoremstyle{thm}\newcommand{\theoremname}{Theorem} \newtheorem{thm}{\protect\theoremname} \BeforeBeginEnvironment{thm}{\thmbefore} \AfterEndEnvironment{thm}{\thmafter} \AtBeginEnvironment{thm}{\thmbegin} \renewcommand{\proofname}{Proof} \theoremstyle{prf}\newtheorem{prf}{\protect\proofname} \renewenvironment{proof}[1][]{\prfbefore\begin{prf}[#1]\pushQED{\qed}}{\popQED\end{prf}\prfafter} \setlength{\parskip}{.5em plus .3em minus .2em}

\begin{document} \let\ea\expandafter \def\double#1{\ea\ea\ea\def\ea\ea\ea#1\ea\ea\ea{\ea#1#1}} \def\text{This is some text. } \double\text \double\text \double\text \double\text \text \par \text \par \text \par \text \par \text \par \text \par \text \par \begin{thm} \text \end{thm} \begin{proof} \text \end{proof} %\penalty-3000 \begin{thm} \text \end{thm} \begin{proof} \text \end{proof} \end{document}

user21820
  • 868
  • 7
  • 19
  • Interesting question, and I shall have a look at it. But why are you using a \newtheoremstyle{prf} rather than looking into the existing proof environment? The two are quite different, and you've had to reimplement the qed mechanism. There are other (good) reasons to want to do that but I'm not sure this is one of them. – barbara beeton Dec 21 '20 at 19:54
  • it's not at all clear what output you want from the supplied example. If you prevent a page break before the final proof you will get a page break mid-way through the theorem text, is that what you want? Or do you want the entire theorem to go to page two? – David Carlisle Dec 21 '20 at 19:57
  • 1
    page breaking is very localised, unless you prevent page breaks at every line end in a theorem then preventing a page break before the prof is unlikely to make the page break be before the theorem. You could consider fixing this by hand as needed, just add \clearpage before theorem 2. The time taken to adjust a few page breaks should not be significant compared to the time writing the text. – David Carlisle Dec 21 '20 at 20:13
  • amsmath has close to zero relevance to theorems, other than supporting the \qed facility of amsthm, and does not affect page breaking at all in that environment, so I've removed the tag. – barbara beeton Dec 22 '20 at 01:43
  • It would be helpful to know what your desired layout would be. Many times, the direct solution to a problem won't work and the actual answer is a bit more subtle (e.g., the page-breaking algorithm between sections in Knuth's webmac) – Don Hosek Dec 22 '20 at 03:38
  • @DavidCarlisle: Yes, the idea is that I can increase the inter-line penalty to encourage each paragraph to stay in one piece, and so if the theorem is short enough then it should go to the next page to accompany the proof. – user21820 Dec 22 '20 at 18:38
  • @barbarabeeton: Thanks for looking at it! I do have reasons for what I did (including using a theorem environment to implement the proof environment), which is why I included all of my code that I thought might be relevant to the page-breaking issue. Anyway I believe the same issue occurs with the original proof environment, and the linked question was never really solved. – user21820 Dec 22 '20 at 18:43
  • @user21820 -- You are correct that the proof environment of amsmath has the same problem. It would be easy to "fix" if you never had unrelated text between the theorem and its proof; just add a boolean switch. But with that restriction, the test is thrown into the realm of \everypar, over which control is nearly impossible (as far as I'm concerned), since too many packages do their own thing using this intervention, and making even the slightest change is likely to unwittingly foul up something else. I'm afraid I have to recommend manual intervention as the safest approach. – barbara beeton Dec 22 '20 at 23:30
  • @barbarabeeton: Yes before a proof I always have a theorem environment. Can you post an answer explaining how to use a boolean switch to solve the problem in this case, and also why my attempts to patch the \trivlist do not work? Thanks! – user21820 Dec 23 '20 at 09:25
  • @user21820 -- After a number of attempts, I have not succeeded in even forcibly suppressing a page break after a theorem; I haven't found how to subvert the closure of trivlist. So I will not be able to post an answer, at least not soon. I'm pretty sure this is the way to go, but I haven't found the key. – barbara beeton Dec 24 '20 at 03:25
  • @barbarabeeton: Can you tell more what you mean by "closure of trivlist" or give a very brief explanation of what is happening with trivlist and where the penalties are actually added? And take your time to answer; I'll wait patiently! =) By the way, the reason I would like to suppress page-break before a proof specifically rather than after a theorem is that we can have theorem+proof or lemma+proof, and sometimes we might want to state a theorem without proof. – user21820 Dec 24 '20 at 04:48
  • @user21820 -- Digging into this requires debugging and tracing. I'll get back to you later, and request a separate chat stream since this is getting rather long. – barbara beeton Dec 24 '20 at 21:42
  • @barbarabeeton: Hi! Did you ever get a chance to look into this? By the way you can ping me in this room if you would like to continue in chat and don't want to create a new room. Thanks! – user21820 Jan 24 '21 at 11:17
  • @user21820 -- I've done a little bit, but don't have the facilities I used to have before I retired. So it's slow going. Thanks for the pointer to separate room. – barbara beeton Jan 24 '21 at 14:52

0 Answers0