2

Is there a way to create a command \notbottom{mytext} so that a pagebreak is automatically inserted before mytext if mytext is at the bottom of a page ?

  • 1
    you can use for example needspace package but normally where this is needed (for example a section heading) you do not need to measure you just arrange that the penalty for breaking after the text is maximum. (10000) so the page is forced to break earlier. – David Carlisle Oct 14 '17 at 19:45
  • \newcommand{\notbottom}[1]{\par\needspace{2\baselineskip}#1} – Werner Oct 15 '17 at 07:12

1 Answers1

1

Here is a solution:

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\newsavebox{\mybottombox} % Box to save the text of the command 
\newlength{\mybottomlength} % The length of our text inside the command
\newlength{\availafter} % The available length left on the page after placing our text


% Optional argument is the minimum length after the nobottom text for not pagebreak. Change it to your needs
\newcommand{\nobottom}[2][60pt]{\savebox{\mybottombox}{\vbox{#2}}\setlength{\mybottomlength}{\ht\mybottombox}%
\setlength{\availafter}{\dimexpr\textheight-\mybottomlength-\pagetotal\relax}\ifdim\availafter<#1%
\pagebreak\noindent\usebox{\mybottombox}%
\else%
\noindent\usebox{\mybottombox}%
\fi%
}%

\begin{document}
\lipsum[1-3]
\nobottom{\color{red}\lipsum[1]}

\lipsum[1-10]
\nobottom[80pt]{\color{blue}Test text here that need at least 80 pt of space left under the page}
\end{document}

With minimum height needed after defined as 60pt (default) the (red) text will be appeared in the page because there are 60pt remaining as space under the text for usage of other (next) text. with mindim 70pt the (red) text will go to next page because the space that will left for new text after red is less than 70pt.

Output for default 60pt:

enter image description here

(For 70 pt there will be a \pagebreak before red text -You can test it by changing the defoult value or give an optional parameter=[70pt] to the command-)

Also the blue text needs at least 80pt and thus it moves to the next page as you can see in the image below:

enter image description here

koleygr
  • 20,105