# How to prevent page break in the last three lines of an environment?

I want to create an environment, thats content should not allowed a page break in the last three lines of the last paragraph.

If the last three lines of the text inside the environment reach the page border, the page break should occur after the forth line from the bottom.

Outside of the environment the normal behavior of the page breaks should not be changed.

The environment will only contain normal text, but with increased margin on the left and right side. There may occur some inline math equations as well, but that should be it.

\documentclass{scrartcl}

\usepackage{lipsum}

\newenvironment{env}{%
}{%
}%

\begin{document}
\lipsum[1-3]

\begin{env}
\lipsum[1]
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This last lines should not be broken to the next page.
This last lines should not be broken to the next page.
This last lines should not be broken to the next page.
\end{env}
\end{document}


Edit:

I'm not sure, if I can make clear, what I try to achieve: I won't normally know, where exactly the last lines will start. Instead, it is more like to prevent widows, but not only prevent the last line of the paragraph to get broken to the next page, but the last three lines of text inside the environment {env} shall always stay together, either on the actual page or the next page.

• What will this environment contain? "Complex" stuff, or just paragraph text? Could you provide an example of its usage? – Werner Mar 15 '14 at 14:36
• I edited the question regarding the expected contend and tried to provide a minimal example. – leviathan Mar 15 '14 at 14:55
• Wouldn't \vspace{2em}\begin{minipage} do what you want? – John Kormylo Mar 16 '14 at 14:55
• @John I'm not sure, I understand your suggestion correctly, but with a minipage, I would need to know beforhand, what the last three lines should contain. – leviathan Mar 16 '14 at 22:18
• Or more precisely, where the last three lines start. Yes. – John Kormylo Mar 17 '14 at 15:58

You can use \widowpenalties; with

\widowpenalties 3 10000 10000 0


you add a penalty of 10000 between the last three lines and none between preceding lines. See also How to avoid page-breaks inside paragraphs?

\documentclass{scrartcl}

\usepackage{lipsum}

\newenvironment{env}
{\begin{addmargin}{2em}\widowpenalties 3 10000 10000 0 }

\begin{document}
\lipsum[1-3]

\begin{env}
\lipsum[1]
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This is just some filler text, to reach the page break.
This last lines should not be broken to the next page.
This last lines should not be broken to the next page.
This last lines should not be broken to the next page.
\end{env}
\end{document}