# How to prevent page break between single line paragraph and math display

I need to bind single line paragraph and math display with \nobreak. \clubpenalty, \widowpenalty, \displaywidowpenalty has no effect because it's a single line and there is \par between math display. Uncommenting \nobreak the result is fine. Single line goes to the next page. Manual intervention to tex file is not an option.

MWE:

\documentclass{article}
\usepackage[paperwidth=5cm,paperheight=3cm]{geometry}
\geometry{verbose,tmargin=.5cm,bmargin=.5cm,lmargin=.5cm,rmargin=.5cm}
\clubpenalty=10000
\widowpenalty=10000
\displaywidowpenalty=10000

\begin{document}
Long long long long long long long long long text.

Single line paragraph:

%\nobreak
$$a+b$$
\end{document}


Perfect:

I am using Luatex and with post_linebreak_filter callback I could recognise single line paragraphs, but I don't see math in here. I want to create penalty node and add it before display. mlist_to_hlist could find math but I don't see any correlation with other callbacks. It seems this callback works later. Any ideas would be wery helpfull

• never leave a blank line before a displayed math environment the spacing will be completely wrong as well as allowing bad page breaks – David Carlisle Jul 13 '17 at 10:00
• You say manual changes to the tex is not an option but the markup is wrong, unfortunately it just makes bad output rather than an error but it is still incorrect the bad spacing can still be seen even in your "perfect" image above. You could of course try to use lua's callback to edit the input on the fly to correct markup errors but that seems an inherently fragile way to address the problem. – David Carlisle Jul 13 '17 at 10:28

Never have a blank line before a displayed equation, this forces a spurious single line paragraph that just consists of white space to be generated before the equation (so it looks a bit like vertical space, but is not) so this makes poor vertical space and allows poor page breaks.

if you use

\documentclass{article}
\usepackage[paperwidth=5cm,paperheight=3cm]{geometry}
\geometry{verbose,tmargin=.5cm,bmargin=.5cm,lmargin=.5cm,rmargin=.5cm}
\clubpenalty=10000
\widowpenalty=10000
\displaywidowpenalty=10000

\begin{document}
Long long long long long long long long long text.

Single line paragraph:
$$a+b$$
\end{document}


It all fits on one page and if you add one extra line

\documentclass{article}
\usepackage[paperwidth=5cm,paperheight=3cm]{geometry}
\geometry{verbose,tmargin=.5cm,bmargin=.5cm,lmargin=.5cm,rmargin=.5cm}
\clubpenalty=10000
\widowpenalty=10000
\displaywidowpenalty=10000

\begin{document}
Long long long long long long long long long text.\\
zz

Single line paragraph:
$$a+b$$
\end{document}


Then the whole second paragraph (the equation is now part of the second paragraph) moves on to page 2.

• David, I agree that it's incorrect input, but I want to fix it on the fly. – Linuxss Jul 14 '17 at 5:51