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.


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

% auxiliary macro for measuring the content height

    % put content into savebox
    \ifoddpage % on odd (right) pages, we simply advance one page
        %{\color{red} odd page detected}
    \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
    \BODY % we don't use \usebox{\mybox}, since this will fail if the content
    % is higher than one page. 


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]

Some stuff in a myenv that fills two pages:

  • 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:




 \ifnum \zref@extractdefault{myenvstart-\themyenvcnt}{abspage}{1}=\zref@extractdefault{myenvend-\themyenvcnt}{abspage}{1}


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

Some stuff in a myenv that fills two pages:

Some stuff in a myenv that fills one page:


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.