# How to make environment never contain doublepage break

I am using LuaLaTeX and define my own environment processed by Lua. I want the contents of this environment (which always start on a new page) when printed from Lua to never break across double page - that is the environment can be on the right-hand page only if it fits there as a whole, otherwise it should be as if a \cleardoublepage was before it. How would that be possible?

• You can add a label at the begin and at the end and then compare their \pageref. If they are different, insert a \cleardoublepage. – Ulrike Fischer Aug 3 '17 at 14:09

In her comment, Ulrike mentioned checking page labels. The changepage package does that for you in order to determine if you are on an odd or an even page. To determine the height of the content in your environment, we use the environ package to capture it and then stuff it inside a savebox.

The answer assumes that odd pages are right, whereas even pages are left.

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

• I am trying that out, but I ran into a problem with environ - The \BODY command discards newlines (literal, not \) which makes it unusable in my application... Is there any way to prevent this? Otherwise, holy smokes, such a detailed answer - thank you! – Mlha Aug 3 '17 at 17:00
• @Mlha - do you mean it strips empty lines at the beginning and end? Because that is all I find -- newlines in the middle of the text seem to be retained. – Michael Palmer Aug 3 '17 at 17:35
• No I mean that if i put in \begin{myenv} first line second line \end{myenv} Then the newline character between first and second line (there are no newlines here in comments...) is stripped. Because I am then using Lua to process the environment contents, I am using Lua to change \n to \ so I need those newline characters – Mlha Aug 3 '17 at 18:05
• You want to both preserve the white space in the document source and at the same time typeset it? Maybe this answer tex.stackexchange.com/questions/38150/… can help you? – Michael Palmer Aug 3 '17 at 18:47
• Yeah, that's what I've been using. The thing is, now instead of the startrecording and stoprecording functions I have just one function operating on \BODY, but there are no newline (\n) characters there this way, it comes as one continuous string. – Mlha Aug 3 '17 at 18:50

You can add start and end label and compare them. The rough idea looks like this:

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


In your real code you will have to think a bit about the begin and the end. E.g. if you need a paragraph break and so on.