防止 Verbatim 环境和方程式之间出现分页符

防止 Verbatim 环境和方程式之间出现分页符

我有一个文档,其中显示的方程式经常在使用创建的环境之后立即出现幻想VRB包。如何防止中间出现分页符?

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

答案1

环境在最后code执行\penalty\@envparpenalty,通常值为 -51,它允许(实际上更喜欢)分页符。

定义code为发出“无休息惩罚”。

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

在此处输入图片描述

答案2

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

在此处输入图片描述

相关内容