[Tex/LaTex] How to prevent page break in the last three lines of an environment

environmentspage-breakingwidows-orphans

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{addmargin}{2em}%
}{%
    \end{addmargin}
}%


\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.

Best Answer

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 }
 {\end{addmargin}}


\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}

enter image description here