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}
\newtheoremstyle{prf}rather than looking into the existingproofenvironment? 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\clearpagebefore 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:13amsmathhas close to zero relevance to theorems, other than supporting the\qedfacility ofamsthm, and does not affect page breaking at all in that environment, so I've removed the tag. – barbara beeton Dec 22 '20 at 01:43webmac) – Don Hosek Dec 22 '20 at 03:38proofenvironment ofamsmathhas 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\trivlistdo not work? Thanks! – user21820 Dec 23 '20 at 09:25trivlist. 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:25trivlist" or give a very brief explanation of what is happening withtrivlistand 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