1

Suppose I have an environment called test, how can I ensure, that the printed content of test contains always an even number of pages (such that if I print the document double sided, I never has the beginning of one test environment on the back page of the end of the last test environment). I.e. if it would occupy only a odd number of pages, insert an empty page at the end of the current test environment. A lualatex solution would be also ok.

\documentclass{article}

\usepackage{lipsum}

\newcounter{counttest}
\setcounter{counttest}{0}

\newenvironment{test}{
\newpage % Start test on a newpage
\refstepcounter{counttest}

\textbf{Test Environment No. \arabic{counttest}}\\

Some pretext \newline
}
{
Some Post text
\label{end}
%%Some code to ensure that test occupies an even number of pages, maybe insert a newpage
}


\begin{document}
\lipsum[1]
\begin{test}
\lipsum[2-6]
\end{test}

\begin{test}
\lipsum[6-10]
\end{test}

\begin{test}
\lipsum[10-14]
\end{test}

\end{document}

Maybe one can somehow catch the page number of the first document page where the current test environment starts and also the page number where it would end. Depending if the difference between those numbers is odd or even, it adds an empty page or not.

7
  • 1
    \cleardoublepage if you want the environments to start on an odd (right) page or \cleartoevenpage to start on even (some classes define that or there are definitions on this site) Jul 7, 2021 at 9:48
  • Doesn't seem to work in my example. For example on document after document page 2 there should be inserted an empty page (such that test contains two instead of one pages).
    – Julia
    Jul 7, 2021 at 9:56
  • And \cleartoevenpage seems to be an undefined macro.
    – Julia
    Jul 7, 2021 at 9:57
  • Wait, I have to use the twoside document option, will try it again...
    – Julia
    Jul 7, 2021 at 10:00
  • 1
    as I said cleartoeven is defined in lots of places, I'll find a llink. twoside only has different margins if you specify different margins for odd and even, it is easy (eg with geometry package) to specify the same margins for left and right sides Jul 7, 2021 at 10:31

1 Answer 1

2

Try this:

   \documentclass[twoside]{article}
    \usepackage[margin=2in]{geometry}% to have the same margin left and right (actually all 4 margins, but you can define them individually, see the documentation)
    \usepackage{lipsum}
    
    \newcounter{counttest}
    \setcounter{counttest}{0}
    
    \newenvironment{test}{
    \cleardoublepage% instead of \newpage % Start test on a newpage
    \refstepcounter{counttest}
    
    \textbf{Test Environment No. \arabic{counttest}}\\
    
    Some pretext \newline
    }
    {
    Some Post text
    \label{end}
    %%Some code to ensure that test occupies an even number of pages, maybe insert a newpage
    }
    
    
    \begin{document}
    \lipsum[1]
    \begin{test}
    \lipsum[2-6]
    \end{test}
    \begin{test}
    \lipsum[6-10]
    \end{test}
    \begin{test}
    \lipsum[10-13]
    \end{test}
    \begin{test}
    \lipsum[10-14]
    \end{test}
    \end{document}
5
  • Ok, adding \cleardoublepage to the beginning seems to be better. But this introduces an unwanted empty page before the first test environment and if the last environment contains only an odd number of pages it doesn't insert an empty page as well. The last point isn't too bad because this doesn't make a difference in printing.
    – Julia
    Jul 7, 2021 at 10:47
  • To begin with the last point: If you want to have an empty page at the end of the pdf: insert a \cleardoublepage after the last \end{test}.
    – C. Peters
    Jul 7, 2021 at 10:56
  • Regarding the first point: what do you intend by omitting the empty page before the first test: using the preceding text just once or always again before a new test? Starting on an even page with seperately distributed tests, fells quite odd for the recipient.
    – C. Peters
    Jul 7, 2021 at 11:04
  • Maybe better to add \cleardoublepage also at the end of the definition of the test environment.
    – Julia
    Jul 7, 2021 at 11:04
  • Regarding the first point: You are right, it would be a better behaviour to have the empty page in this case.
    – Julia
    Jul 7, 2021 at 11:08

You must log in to answer this question.

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