# How to keep the last page number even?

I want to make sure the last page always be even, so I defined command as the following:

\documentclass{article}
\usepackage{lipsum}
\usepackage[paperwidth=302pt,paperheight=400pt, offset=0pt, hmargin=40pt, tmargin=50pt,

\newcommand{\OpenNewPageIfNeeded}{%
\ifodd\value{page}%
\newpage%
\hbox{}%
\else%
\fi%
}
\AtEndDocument{\OpenNewPageIfNeeded}

\begin{document}
\lipsum
\end{document}


This works for most of the cases, but fails sometimes, for example the above sample code.

It seems that this question is duplicated with Ensure last page is even and blank, I tried the solution but it still fails, any suggestions?

-

Quoting the UK TeX FAQ:

[T]he output routine is asynchronous, and (La)TeX will usually process quite a bit of the “next” page before deciding to output any page. As a result, the page counter (known internally in LaTeX as \c@page) is normally only reliable when you’re actually in the output routine.

Solution: Use, e.g., the \ifthispageodd macro from KOMA-Script's scrextend package. Note that this macro uses a \label/\ref mechanism, so two LaTeX runs are needed for the correct output.

\documentclass{article}

\usepackage{lipsum}
\usepackage[paperwidth=302pt,paperheight=400pt, offset=0pt, hmargin=40pt, tmargin=50pt,
\usepackage{scrextend}

\newcommand{\OpenNewPageIfNeeded}{%
\ifthispageodd{%
\newpage
\null
}{%
}%
}
\AtEndDocument{\OpenNewPageIfNeeded}

\begin{document}
\lipsum
\end{document}

-

The test should be later, after all the \clearpage at \end{document} and maybe after aux stuff. You may use either etoolbox or scrlfile:

\documentclass{article}
\usepackage{lipsum}
\usepackage[paperwidth=302pt,paperheight=400pt, offset=0pt, hmargin=40pt, tmargin=50pt,
\usepackage{scrlfile}
% If last already output page is odd, the current page is even but
% will not be output. So we add something to it and put it out.
\ifodd\value{page}\else
\typeout{Do it}%
\par\null\newpage
\fi
}
\makeatother
\begin{document}
\lipsum
\end{document}

-

An easy way to deal with this, provided that you are using arabic page numbers, is to use a last-page label:

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage[paperwidth=302pt,paperheight=400pt,offset=0pt,hmargin=40pt,tmargin=50pt,

\AtEndDocument{%
\label{lastpageofdoc}% Set label on last page.
\ifodd\pageref{lastpageofdoc}\newpage\mbox{}\fi% Add new page if necessary
}

\begin{document}
\lipsum
\end{document}


Alternatively (or perhaps more generally), the strict package option to changepage also allows for the correct checking of page values using \checkoddpage, followed by the condition \ifoddpage...\else...\fi:

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage[paperwidth=302pt,paperheight=400pt,offset=0pt,hmargin=40pt,tmargin=50pt,
\usepackage[strict]{changepage}% http://ctan.org/pkg/changepage

\AtEndDocument{%
\checkoddpage\ifoddpage\newpage\mbox{}\fi
}

\begin{document}
\lipsum
\end{document}

-
The trick of adding a new page is good, but I am interested whether we can do the reverse (even it does not make sense), i.e., removing the last page to make the total page even. Is it easy? –  kiss my armpit Aug 7 '12 at 8:46
@GarbageCollector: Yes, one could just drop the final page that is written to the output if it will make your page count odd. But what if that page has important stuff on it? –  Werner Aug 7 '12 at 14:11
I am just interested in the dropping technique that might be useful in the future. –  kiss my armpit Aug 7 '12 at 14:24

In ConTeXt, you can use \page[odd] to insert a page break so that the subsequent material starts on odd page (it is used in \setuphead[chapter][page=...] options, and hence the naming convention; other options are even, left and right. The latter two only work for doublesided layouts).

Thus, to ensure that the number of pages is always even, use:

\appendtoks
\page[empty,odd]
\to\everystoptext

\starttext
\input knuth
\stoptext


The empty option ensures that no headers and footers are included. If you need them, just use \page[odd].

Note that if you use double-sided layout, ConTeXt automatically ensures that the resulting pdf has an even number of pages.

-