# How to avoid widow lines after displayed equations

If a displayed equation is close to the bottom of the page, TeX likes to insert a page break even if the current paragraph contains only one more line after the equation, as e.g. in

... it follows that
$a_n = ...$
for all $n\in\N$.


(And this happens although there is enough space in the current page.) The problem appears to be that TeX doesn't regard a widow line following a displayed equation as a widow.

Since I don't like this behaviour, I came up with the following workaround: I write \avoidbreak before the closing \], where \avoidbreak is defined by

\newcommand{\avoidbreak}{\postdisplaypenalty=100}


Of course it's a nuisance that now I have to write \avoidbreak at every instance of a possibly "bad" page break. I don't want to globally define \postdisplaypenalty=100 since this would discourage page breaks after displayed equations even at the end of a paragraph: For a new paragraph right after the display it is quite alright if it starts on the next page (even if it consists of only one line, see also the MWE below).

Now my question: Is there a clean solution that works without having to manually add something like \avoidbreak? I'd appreciate some code that I can just add to my preamble.

\documentclass{article}
%Remove the following % to get "one line of text" onto the first page:
%\postdisplaypenalty=100
\setlength\textheight{3.65cm}
\setlength\textwidth{4cm}
\begin{document}
Two lines of text followed by a formula
$a=b$
Two lines of text followed by a formula
$a=b$
%With my code, there will be no change if you put an empty line at this place!
%If "one line of text" below starts a new paragraph, then
%that paragraph should be allowed (even encouraged) to start on a new page.
%I only want to try and avoid the page break if no new paragraph is started here.
one line of text

New paragraph
\end{document}


(Let me mention that TeX has a \displaywidowpenalty parameter, but this one is about widows before displayed equations.)

-
The TeXbook has an elaborate system for merging and discarding penalties at various junctures; I'm pretty sure there's a way to insert a penalty that gets discarded if a new paragraph follows (or maybe if one doesn't). Hopefully one of the people who know the system by heart can show us how to use it... – alexis Dec 4 '13 at 16:00

I can't see any problems with setting \postdisplaypenalty globally—if you encounter any problems, they are probably signs of a package bug. The default values are probably fine for Knuth; but these settings are implemented as parameters because you should customize them.

-
Please note that I reformulated my question, among other things to explain why changing \postdisplaypenalty globally is not so good. – Hendrik Vogt Mar 9 '13 at 20:15

Ok, I don't know how much of a bad hack this is, but here's an attempt which tries to redefine \par at the end of \] so that it can detect whether it comes directly after the displayed math.

\documentclass{article}
\usepackage{etoolbox}
%Remove the following % to get "one line of text" onto the first page:
\postdisplaypenalty=100
\setlength\textheight{3.65cm}
\setlength\textwidth{4cm}

\makeatletter
\patchcmd\]{\ignorespaces}
{%
% We are in horizontal mode here again!
% Insert a penalty so we can recognize whether \par comes directly
% after.
\penalty6666
% This might conflict with \par redefinitions in lists, so only
% proof of concept!
\def\par
{%
% check whether we are directly behind formula
\@tempcnta=\lastpenalty\unpenalty
\@par
% in that case - insert negative penalty!
}%
\ignorespaces
}{}{}
\makeatother

\begin{document}
\tracingoutput1
\showboxdepth1000
Two lines of text followed by a formula
$a=b$
Two lines of text followed by a formula
$a=b$
%With my code, there will be no change if you put an empty line at this place!
%If "one line of text" below starts a new paragraph, then
%that paragraph should be allowed (even encouraged) to start on a new page.
%I only want to try and avoid the page break if no new paragraph is
%started here.

one line of text

New paragraph
\end{document}


It's probably at best a first proof of concept, but at least you can start doing a real test based on this.

-
@HendrikVogt Yes I was too lazy adding the penalty. It should work better with my edit. As I said, only proof of concept at the moment :-) – Stephan Lehmke Dec 15 '13 at 4:48
Great, now it works as I would expect - thanks a lot! It would be great if one could somehow determine the end of displayed math and do the penalty thing there. Of course I'd like to that thing to work for align and friends, too. But I don't use too many different display environments, so it might be possible to do it manually. – Hendrik Vogt Dec 15 '13 at 10:35
@HendrikVogt It wouldn't be too much work to patch other displayed math environments this way, as almost every one of them ends directly in , which is the place where this patch should go. But the question is really how much sense this would make. For instance, when page breaking inside an align environment is allowed, you'd get a "widow line" anyway because it'd break off together with some equation lines. – Stephan Lehmke Dec 17 '13 at 4:35

If you really dont want to set the postdisplaypenalty value globally, you could create a new environment which modifies the display penalty inside the environment, and resets it prior to leaving.

\makeatletter
\newenvironment{tighten}{
\let\pen@old\postdisplaypenalty
\postdisplaypenalty=100
}{
\let\postdisplaypenalty=\pen@old
}
\makeatother


Modified MWE code:

\documentclass{article}

\makeatletter
\newenvironment{tighten}{
\let\pen@old\postdisplaypenalty
\postdisplaypenalty=100
}{
\let\postdisplaypenalty=\pen@old
}
\makeatother

%Remove the following % to get "one line of text" onto the first page:
%\postdisplaypenalty=100
\setlength\textheight{3.65cm}
\setlength\textwidth{4cm}
\begin{document}
\begin{tighten}
Two lines of text followed by a formula
$a=b$
Two lines of text followed by a formula
$a=b$
\end{tighten}
%It doesn't matter if you have a new line at this place or not!
%I only want to avoid the page break if no new paragraph is started here.
one line of text

New paragraph
\end{document}

-
Thanks for your answer! I'm not sure at all, but I've got the impression that your code works due to some kind of LaTeX bug: If "one line of text" starts a new paragraph, then it goes to the next page as I want - so far so good. But somehow this only happens because then the first page ends with an empty line which leaves no space for "one line of text". I have no idea at all where this empty line comes from! – Hendrik Vogt Dec 6 '13 at 8:37
Note that TeX is in horizontal mode after \] with \ignorespaces active, so the end of the tighten environment together with the trailing space somehow produces an empty paragraph. Possibly putting \ignorespaces at the end of the tighten environment (and avoiding redundant spaces) could avoid this. – Stephan Lehmke Dec 13 '13 at 11:17