# Forcing even number of pages in output (LaTeX — pdflatex) [duplicate]

Possible Duplicate:
How to keep the last page number even?

Is there a way of forcing an even number of pages in output ?

What I mean is to make pdflatex document "intelligent" it will add or not, blank page on the end to ensure there is always produced even number of pages.

(Purpose: I produce many of such pdfs and later combine them with external tool together in order to produce on pdf for double-side printing)

-

## marked as duplicate by lockstep, Marco Daniel, percusse, diabonas, clemensAug 28 '12 at 18:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

## 3 Answers

Issue \clearpage at the end of the document; then check whether the page number is odd and do nothing in this case, otherwise eject an empty page; the following code in the document preamble will do:

\AtEndDocument{\clearpage\ifodd\value{page}\else\null\clearpage\fi}


This works independently of the oneside or twoside option. In the latter case

\AtEndDocument{\cleardoublepage}


is sufficient (as stated by Ulrike).

-
Isn't testing for \value{page} unreliable, as shown in the MWE to the linked question? – lockstep Aug 28 '12 at 16:11
@lockstep If the test is put just after \clearpage it gives the correct result, because the output routine has just been performed. – egreg Aug 28 '12 at 16:19
Note: add a \pagestyle{empty} after the \clearpage to suppress the final page number. – wchargin May 13 '15 at 23:08

This here gives two pages:

\documentclass{book}
\begin{document}
abc
\cleardoublepage
\end{document}

-
Is it book class specific or general solution for any documentclass? – Grzegorz Wierzowiecki Aug 28 '12 at 10:15
It works for any document class. – ℝaphink Aug 28 '12 at 10:18
@UlrikeFischer Raphink suggests \cleardoublepage adds page always. I need to add page only when is it needed. What do you think about it ? – Grzegorz Wierzowiecki Aug 28 '12 at 10:19
@GrzegorzWierzowiecki No, \cleardoublepage works like you want only if twoside mode is active. With oneside (default e.g. in report or article) \cleardoublepage calls only \clearpage. But you can easily define your alwayscleardoublepage without this restriction. The original definition is in latex.ltx. – Ulrike Fischer Aug 28 '12 at 10:23
@UlrikeFischer Could you provide please an one more example showing how to make it working with article documentclass? I believe it will useful for other readers. – Grzegorz Wierzowiecki Aug 28 '12 at 10:27

This may not be the cleanest way, and probably someone will come with something better:

\documentclass{article}

\usepackage{lipsum}
\usepackage{ifthen}

\begin{document}

\lipsum

\lipsum

\lipsum

\ifthenelse{\isodd{\thepage}}
{\cleardoublepage\mbox{}}
{}

\end{document}


Add/remove \lipsum to see how the number of pages evolves. The document will always have an even number of pages.

Invert {} and {\cleardoublepage\mbox{}} to ensure an odd number of pages.

-
What's the advantage above using only \cleardoublepage ? – Grzegorz Wierzowiecki Aug 28 '12 at 10:16
It's dynamic. Using \cleardoublepage will add a new page, whether the number of pages is even or not. This code only adds a new page if the number of pages is odd, ensuring an even number of pages. – ℝaphink Aug 28 '12 at 10:17
@Raphink No, \cleardoublepage will not add always a new page. It contains the same test you are doing (and it contains a \clearpage before the test to avoid problems with the asynchronous page which your code is missing.) – Ulrike Fischer Aug 28 '12 at 10:29
@Raphink I would love to see your solution improved by Ulrike suggestions, as It's much more flexible to standard \cleardoublepage. On the other hand, you might like to show how to use it for setting up "this page is intentionally blank" : tex.stackexchange.com/questions/69115/… – Grzegorz Wierzowiecki Aug 28 '12 at 10:30
The clean way of doing it is @egreg's (not surprisingly). – ℝaphink Aug 28 '12 at 10:41