# [Penalties]: How to force new page when not enough room on page?

This is not a duplicate of preventing a pagebreak.

# What I want to achieve

Consider an itemize environment, where one of the \items are split over the bottom of one page and the beginning of the next:

I want the \item (alternatively the entire itemize environment) to go on top of the next page instead of being split over two pages.

However, if there is any (reasonable) way to put it on the current page, then it should prioritize that.

I want this to be done automatically, so without e.g. needspace package.

# What I've tried

I suspect this will be done with penalties. I've been playing around with the ones in the \samepage definition as well as the ones listed in this answer.

For example \@itempenalty\@M does not work, which makes me a little puzzled.

It might just be that I don't quite understand what the penalties actually do. I understand it so that if a certain penalty is high then the interpreter will attempt to avoid breaking at this penalty. What I don't understand is how to encourage the page break iff there is no way that it can insert the content (under normal restrictions).

# Code

I'm working on a package, and this is just a MWE.

The itemize splits across pages, and in this example there is no room, so I want the itemize environment (I'm also fine with just the \item in question) to jump to the next page. (I added a \makeatletter so you don't have to.

\documentclass{article}
\usepackage{lipsum}
\setlength{\parindent}{0em}
\makeatletter

\begin{document}
\lipsum[1-4]
\bgroup
%\bgroup is okay here, right?
% (Then the penalties should go back at \egroup?)

%% Magic, amazing penalty code can go here. %%
%% ... or wherever it fits best ...         %%

\begin{itemize}
\item\lipsum[10-12]
\end{itemize}
\egroup
\end{document}


This is not really a question about penalties, although I wouldn't mind links or explanations that would clarify how the compiler (i.e. pdftex) deals with penalties.

Feel free to suggest, or do, edits to the title so it's more to the core of the problem. I feel a little on thin ice with this title?

• You could put the itemize environment in a minipage, to prevent it breaking up. – Steven B. Segletes Apr 3 '18 at 16:57
• That like a hacky fix? I'll give it a go though. – Andreas Storvik Strauman Apr 3 '18 at 16:58
• I wouldn't call it hacky. It's the ultimate penalty...it won't pagebreak in the middle. – Steven B. Segletes Apr 3 '18 at 17:01
• By the way, if you try the minipagein your MWE, you need to use the [b] option, to get the exit spacing right. You may need to add a \strut to you first \item. Else, use the [t] option and add a \strut at the end of the last \item. – Steven B. Segletes Apr 3 '18 at 17:03
• Look for \filbreak – egreg Apr 3 '18 at 17:04

You might use \filbreak (see https://tex.stackexchange.com/a/72787/4427)

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}

\begin{document}

\lipsum[1-4]
\begin{itemize}\preto\item{\filbreak}
\item test\\on\\three lines
\item test
\item\lipsum[10-12]
\end{itemize}
\lipsum[1-4]

\end{document}


• \preto does what it seems to do? "Before every..."? – Andreas Storvik Strauman Apr 3 '18 at 18:01
• @AndreasStorvikStrauman The \preto command prepends tokens to a parameterless macro, in this case \item (only locally, so at the end of itemize the change will be undone. So when LaTeX processes \item it will first do \filbreak, then the normal action of \item. – egreg Apr 3 '18 at 21:21
• Shouldn't there be a \filbreak after the itemize environment too? Otherwise, TeX might prefer a page break before the last item, even if it would fit on the page. (Or so I think.) – Harald Hanche-Olsen Apr 28 '18 at 10:06
• @HaraldHanche-Olsen That’s quite likely – egreg Apr 28 '18 at 12:02