# If Then Else for odd page/even page

How to use if-then-else structure in LaTeX? I need a example of odd and even page.

if odd then
command 1
else
command 2

-
– Charles Stewart Nov 29 '10 at 10:23
Beware, though, that TeX does not in general know what page the output will end up on while typesetting a paragraph, due to the asynchonous nature of TeX processing. What exactly are you trying to accomplish? – Harald Hanche-Olsen Nov 29 '10 at 13:04
Isn't this question a duplicate of tex.stackexchange.com/q/5894 ? – Frédéric Grosshans Nov 29 '10 at 16:09
I also thought it was a duplicate, but I guess it's not if you concentrate on the odd/even pages. – Hendrik Vogt Nov 29 '10 at 17:50
Would you please change the accepted answer? It's sort of embarrassing to have this old mistake of mine around and in any case it's not appropriate as the one with the check. – Ryan Reich Jun 24 '13 at 12:24

Use the changepage package:

\usepackage{changepage}
\strictpagecheck
...
\checkoddpage
\ifoddpage
...
\else
...
\fi


If you use the memoir class, then this feature is built in automatically.

-

For the sake of completeness: KOMA-Script offers the command \ifthispageodd{<true>}{<false>}. It can be used in the standard classes by loading scrextend:

\documentclass{article}
\usepackage{scrextend}

\begin{document}
\pagenumbering{roman}
\ifthispageodd{I'm odd}{I'm even}% I'm odd
\newpage
\ifthispageodd{I'm odd}{I'm even}% I'm even
\newpage
\pagenumbering{arabic}
\ifthispageodd{I'm odd}{I'm even}% I'm odd
\newpage
\ifthispageodd{I'm odd}{I'm even}% I'm even
\end{document}


memoir also offers a similar conditional \ifoddpage ... \else ... \fi which needs to be preceded by \checkoddpage. Additionally one should call \strictpagecheck so the page numbers get written to the aux file.

\documentclass{memoir}
\strictpagecheck
\makeatletter
\newcommand*\ifthispageodd{%
\checkoddpage
\ifoddpage
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother
\begin{document}
\pagenumbering{roman}
\ifthispageodd{I'm odd}{I'm even}% I'm odd
\newpage
\ifthispageodd{I'm odd}{I'm even}% I'm even
\newpage
\pagenumbering{arabic}
\ifthispageodd{I'm odd}{I'm even}% I'm odd
\newpage
\ifthispageodd{I'm odd}{I'm even}% I'm even
\end{document}

-
Thanks, super useful! makeatother for completeness? – Joe Corneli Dec 10 '12 at 1:26
@JoeCorneli Thanks, added! – clemens Dec 10 '12 at 9:51

As Harald has pointed out already, the use of \thepage isn't foolproof to decide whether we are on an even or odd page. Try this extended version of Ryan's example:

\documentclass{article}
\usepackage{ifthen}
\newcounter{mycount}

\begin{document}
\noindent\whiledo{\themycount<130}{%
\ifthenelse{\isodd{\thepage}}%
{Odd\\}%
{Even\\}%
\stepcounter{mycount}%
}
\end{document}


You will read Odd' on every page.

Instead, it is safer to use a \label+\pageref approach:

\documentclass{article}
\usepackage{ifthen}
\newcounter{mycount}

\begin{document}
\noindent\whiledo{\themycount<130}{%
\label{mylabel\themycount}%
\ifthenelse{\isodd{\pageref{mylabel\themycount}}}%
{Odd\\}%
{Even\\}%
\stepcounter{mycount}%
}
\end{document}

-
I will test. Thanks. – Regis da Silva Nov 29 '10 at 15:49
This is basically what the changepage package, suggested by Will, does. – Martin Scharrer Sep 12 '11 at 22:32

Checking for page numbering can also be done using a \label-\ref approach. Below I use the expandable \getpagerefnumber from refcount to condition on the page number:

\documentclass{article}
\usepackage{refcount}% http://ctan.org/pkg/refcount
\newcounter{oddpagecheck}
\makeatletter
\newcommand{\ifoddpage}{% \ifoddpage{<odd>}{<even>}
\stepcounter{oddpagecheck}% For unique labels
\label{opc-\theoddpagecheck}% Place label
\ifodd\getpagerefnumber{opc-\theoddpagecheck}
\expandafter\@firstoftwo% Page is odd
\else
\expandafter\@secondoftwo% Page is not odd (even)
\fi}
\makeatother
\begin{document}
\ifoddpage{odd}{even}
\end{document}


Since this uses the \label-\ref system, you need to compile at least twice whenever there are labels that have moved around.

-

If you want an environment, which automatically right or left justifies depending on page odd/evenness, I use something like the below.

For example, I have boxes for my equations or key derivations, done with tikzpicture, and wrapping that inside the below environment does the expected. I wouldn't, however, use it around regions that can page-break mid way, like paragraphs of text, since a variable is stored (def\mylr{...}) at the start of the environment, that variable remains as is.

\newenvironment{autoLR}{
\ifthenelse{\isodd{\value{page}}}%
{\def\mylr{flushright}}%
{\def\mylr{flushleft}}%
\begin{\mylr}
}{%
\end{\mylr}
}
`
-
This may not work for the same reasons given in the answer below and in the 2nd part of Martins comment. – AlexG Dec 6 '12 at 12:05