1

What is the best way to avoid a page break just after a \epigraph command from epigraph package? I'm adding epigraphs after section titles.

\documentclass{article}
\usepackage[nopar]{lipsum}
\usepackage{epigraph}

\begin{document}

    \section{Aaaa}
    \epigraph{$1+2 \neq 3$}{Matlab}

    \lipsum[1-4]


    \section{Bbbb}
    \epigraph{$1+2 \neq 3$}{Matlab}

    \lipsum[1-4]

\end{document}

I tried with \penalty1000, with \nopagebreak, and with \clubpenalties without success.

I managed to do that with \needspace like in this answer, adding to the preamble:

\usepackage{etoolbox}
\usepackage{needspace}
\preto\section{\needspace{7\baselineskip}}

Is this a good way? Is there a better one?

1
  • I just realized that even in the first page of epigraph package manual, there's a page break just after a section and an epigraph... Commented Mar 4, 2019 at 16:47

1 Answer 1

1

From an old answer of mine that is here:

\documentclass{article}
\usepackage[nopar]{lipsum}
\usepackage{epigraph}


\newsavebox{\mybottombox} % Box to save the text of the command 
\newlength{\mybottomlength} % The length of our text inside the command
\newlength{\availafter} 
% 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}

    \section{Aaaa}
    \epigraph{$1+2 \neq 3$}{Matlab}

    \lipsum[1-4]


    \section{Bbbb}
    \nobottom{\epigraph{$1+2 \neq 3$}{Matlab}}

    \lipsum[1-4]

    \nobottom[90pt]{\section{Cccc}}
    \epigraph{$1+2 \neq 3$}{Matlab}

    \lipsum[1-4]

\nobottom[90pt]{\section{Dddd}
\epigraph{$1+2 \neq 3$}{Matlab}}

\lipsum[1-4]

\end{document}

I gave a second example of usage that included the \section command inside my \nobottom in case you wish to use it like this (preferable for me), but added a third example to with both \section and \epigraph commands inside my command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .