# Do something on every second page

Does there exist a way to only print the main text in the document on every second page while running some other code on the other pages? What I want is something that runs like in the below example:

\documentclass{memoir}
\usepackage{lipsum}

\begin{document}
\begin{atevenpages}
The content of this environment is supposed to be printed on even-numbered pages. On the other hand, even-numbered pages are supposed to contain none of the main text.
\end{atevenpages}

This is the main text, which is only to appear on odd-numbered pages.
\lipsum[1-22]
\end{document}

• this question asks for much the same result; unfortunately, it doesn't have an answer. an approach to parallel texts across a two-page spread is given in this question. Mar 17 '14 at 19:22
• Have a look at tex.stackexchange.com/questions/6143/…, I guess it answers your question Mar 17 '14 at 19:29
• @UweZiegenhagen, I may very well be wrong, but I don't see any obvious way to apply that to this problem. I need the same code to be executed at every even page, not just once if the page is even. Mar 17 '14 at 19:34
• @barbarabeeton, there may very well be a solution there, but I'm afraid I don't understand much of the code in your second link. :-( Mar 17 '14 at 19:35

Have a look at the everypage and ifthen package. everypage introduces a hook which is called after each page has been set. So I need to call my \checkthatpage command only for the first page manually, for all other pages the hook is used. There may be more sophisticated solutions, but I haven't found them, yet.

\documentclass{article}
\usepackage{ifthen,everypage}

\newcommand{\checkthatpage}[2]{%
\ifthenelse{\isodd{\value{page}}}%
{#1}%
{#2}%
}%

\begin{document}

Foo

\clearpage

Bar

\end{document}

• Sounds very promising indeed! I'll try it out and return shortly. :-) Mar 17 '14 at 19:57
• For some reason, it stops working after the second page. Try adding some more \clearpages and notice the result. "Even" and "Odd" are completely missing. Mar 17 '14 at 20:25
• Strange, I'll have a closer look tomorrow. Mar 17 '14 at 20:27
• Perhaps you meant everypage? Mar 17 '14 at 20:30
• I would use \value{page} instead of \thepage, as the latter may return roman numbers or other kinds of fancy formatting. See what-is-the-proper-method-of-accessing-a-counter Mar 18 '14 at 8:08

I would tackle this problem at a lower level: The \shipout routine, which is invoked by TeX when a page is full and needs to be shipped out. At this stage, we can just add another page with the intended content:

\documentclass{memoir}
\usepackage{lipsum}

\usepackage{atbegshi}

\AtBeginShipout{%
\ifodd\value{page}%
\EvenPageContent%
\newpage
\fi
}

\newcommand{\EvenPageContent}{%
The content of this environment is supposed to be printed on even-numbered pages. On the other hand, even-numbered pages are supposed to contain none of the main text.
}

\begin{document}

This is the main text, which is only to appear on odd-numbered pages.
\lipsum[1-22]
\end{document}

• Well, thinking a bit more about it, that is basically the same approach as suggested by Uwe. It is just based on other packages. Mar 17 '14 at 23:19
• Yes, for most purposes, it works exactly the same way. But actually, this approach made some things work better in my concrete document. :-) Mar 18 '14 at 9:35
• So thanks for the answer, it may be the one I end up using. :-) Mar 18 '14 at 10:11
• I don't really understand the purpose of the code \AtBeginShipoutOriginalShipout \box\AtBeginShipoutBox. It rendered a warning in my document, and when I commented it out, everything still worked. I looked into the documentation of atbegshi, but did not get many answers there. What does that code do? Mar 19 '14 at 7:44
• @Gaussler: You are absolutely right, it is not necessary! The purpose was to ship out the current page (provided in \AtBeginShipoutBox) before inserting the \EvenPageContent, so that the latter does not make it accidentally onto an odd page. However, that was a logical mistake: When the shipout handler is invoked, LaTeX has already finished the construction of the odd page, so new content goes to the next page anyway. I have simplified the code. Mar 19 '14 at 8:58