Finding if you’re on an odd or an even page

Another question discusses the issue of getting \marginpar commands to put their output in the correct margin of two-sided documents. This is an example of the general problem of knowing where a particular bit of text lies: the 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.

The solution is to use some version of the \label mechanism to determine which side of the page you’re on; the value of the page counter that appears in a \pageref command has been inserted in the course of the output routine, and is therefore safe.

However, \pageref itself isn’t reliable: one might hope that

\ifthenelse{\isodd{\pageref{foo}}}{odd}{even}
would do the necessary, but both the babel and hyperref packages have been known to interfere with the output of \pageref; be careful!

The changepage package needs to provide this functionality for its own use, and therefore provides a command \checkoddpage; this sets a private-use label, and the page reference part of that label is then examined (in a hyperref-safe way) to set a conditional \ifoddpage true if the command was issued on an odd page. The memoir class has the same command setting a conditional \ifoddpage. Of course, the \label contributes to LaTeX’s “Rerun to get cross-references right” error messages...

The (older) chngpage provides the same facilities (though it provides a \ifcpoddpage command; changepage (whose functions are extracted from the author’s memoir class) is the designated successor to chngpage.

The Koma-Script classes have an addmargin* environment that also provides the sorts of facilities that the chngpage offers. Koma-Script’s supporting command is \ifthispageodd{<true>}{<false>} executes different things depending on the page number.

changepage.sty
macros/latex/contrib/misc/changepage.sty
chngpage.sty
macros/latex/contrib/misc/chngpage.sty
KOMA script bundle
macros/latex/contrib/koma-script (gzipped tar, browse)
memoir.cls
macros/latex/contrib/memoir (gzipped tar, browse)

This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=oddpage