3

I have a document in which displayed equations often appear immediately after an environment created using the fancyvrb package. How can I prevent page breaks from appearing in between?

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{mathtools}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{code}{Verbatim}{commandchars=\\\{\},}
\begin{document}
\rule{1pt}{467pt}
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
\begin{code}
Int( x^2 , x ) ;
\end{code}
% *** Don't break here! ***
\begin{equation*}
\int x^2 \, \mathrm{d} x
\end{equation*}
\end{document}

2 Answers 2

3

The code environment does \penalty\@envparpenalty at the end, the usual value is –51, which allows (actually prefers) a page break.

Define code to issue a “no break penalty”.

\makeatletter
\newenvironment{code}
 {\@endparpenalty=\@M\VerbatimEnvironment\begin{Verbatim}[commandchars=\\\{\}]}
 {\end{Verbatim}}
\makeatother

enter image description here

3
  • Is it possible to forbid a break if and only if the next item is a displayed formula? (My thinking is that if the \@envparpenalty were to be removed altogether, then the \predisplaypenalty would do the job, but I might be wrong.) Commented Mar 30, 2016 at 20:42
  • @IanThompson I don't think so; glue is also added: LaTeX lists are wild beasts, I'm afraid.
    – egreg
    Commented Mar 30, 2016 at 21:06
  • ok, I'll just define a separate environment for the minority of cases in which an example code isn't followed by a displayed formula. Commented Mar 30, 2016 at 21:16
1

Does this suffice? I surround the unbreakable stuff with a \setbox0=\vbox{...}\box0.

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{mathtools}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{code}{Verbatim}{commandchars=\\\{\},}
\usepackage{needspace}
\begin{document}
\rule{1pt}{467pt}
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
\setbox0=\vbox{%
\begin{code}
Int( x^2 , x &%$$\{\}[])^&#  ;
\end{code}
% *** Don't break here! ***
\begin{equation*}
\int x^2 \, \mathrm{d} x
\end{equation*}
}\box0
\end{document}

enter image description here

1
  • It would work, but it would be awkward in cases where there is more than one line of code (breaks between lines of code are ok; I just don't want the result orphaned on the next page). I should probably clarify the question. Commented Mar 30, 2016 at 20:44

You must log in to answer this question.

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