如何使环境不包含双分页符

如何使环境不包含双分页符

我正在使用 LuaLaTeX 并定义我自己的由 Lua 处理的环境。我希望此环境的内容(始终从新页面开始)在从 Lua 打印时不会跨过双页 - 也就是说,只有当环境整体适合时,它才能位于右侧页面,否则它应该像在\cleardoublepage它之前一样。这怎么可能呢?

答案1

在她的评论中,Ulrike 提到检查页面标签。该changepage包会帮您检查页面标签,以确定您是在奇数页还是偶数页。为了确定您环境中内容的高度,我们使用该environ包来捕获它,然后将其塞入保存框中。

答案假设奇数页是右边,而偶数页是左边。

\documentclass{article}

\usepackage{lipsum, calc, xcolor, environ, ifthen, lmodern, changepage}
\strictpagecheck

\newsavebox{\mybox}
% auxiliary macro for measuring the content height
\newcommand\measurebox{\dimexpr\ht\mybox+\dp\mybox\relax}

\NewEnviron{myenv}{% 
    % put content into savebox
    \begin{lrbox}{\mybox}\noindent\begin{minipage}{\linewidth}\BODY\end{minipage}\end{lrbox}
    \checkoddpage
    \ifoddpage % on odd (right) pages, we simply advance one page
        %{\color{red} odd page detected}
        \clearpage
    \else % on even pages, we test the content height and if needed advance
          % two pages. A simple \cleardoublepage didn't work, hence
          % the double \null\newpage. Maybe there's a better way
        \ifthenelse{\measurebox>\textheight}{\null\newpage\null\newpage}{\clearpage}%
    \fi
    \BODY % we don't use \usebox{\mybox}, since this will fail if the content
    % is higher than one page. 
}

\begin{document}

Some random text for the first page: \lipsum[1-2]

% comment and uncomment this text for testing   
%    \clearpage
%    Some more stuff to fill another page
%    \lipsum[3-4]

\begin{myenv}
\color{blue}
Some stuff in a myenv that fills two pages:
\lipsum[3-12]
\end{myenv}

\end{document} 

答案2

您可以添加开始和结束标签并进行比较。粗略的想法如下:

\documentclass{book}

\usepackage{lipsum}
\usepackage[abspage]{zref}
\usepackage{xcolor}

\newcounter{myenvcnt}
\makeatletter

\newenvironment{myenv}{%
 \refstepcounter{myenvcnt}%
 \ifnum \zref@extractdefault{myenvstart-\themyenvcnt}{abspage}{1}=\zref@extractdefault{myenvend-\themyenvcnt}{abspage}{1}
 \else
 \cleardoublepage
 \fi
 \zref@label{myenvstart-\themyenvcnt}\ignorespaces}
 {\zref@label{myenvend-\themyenvcnt}}

\makeatother
\begin{document}

Some random text for the first page: \lipsum[1-2]

\begin{myenv}
\color{blue}
Some stuff in a myenv that fills two pages:
\lipsum[3-12]
\end{myenv}

\newpage
\lipsum[1]
\begin{myenv}
\color{red}
Some stuff in a myenv that fills one page:
\end{myenv}


\end{document} 

在实际代码中,您必须考虑一下开始和结束。例如,如果您需要段落分隔符等。

相关内容