Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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,
    bmargin=75pt, noheadfoot]{geometry}

\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?

share|improve this question

4 Answers 4

up vote 9 down vote accepted

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,
    bmargin=75pt, noheadfoot]{geometry}
\usepackage{scrextend}

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

\begin{document}
\lipsum
\end{document}
share|improve this answer

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,
    bmargin=75pt, noheadfoot]{geometry}
\usepackage{scrlfile}
\AfterReadingMainAux{%
  % 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}
share|improve this answer

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,
    bmargin=75pt,noheadfoot]{geometry}% http://ctan.org/pkg/geometry

\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,
    bmargin=75pt,noheadfoot]{geometry}% http://ctan.org/pkg/geometry
\usepackage[strict]{changepage}% http://ctan.org/pkg/changepage

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

\begin{document}
\lipsum
\end{document}
share|improve this answer
    
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? –  stalking is prohibited 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. –  stalking is prohibited 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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