[Tex/LaTex] If Then Else for odd page/even page

conditionalspage-numbering

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

if odd then
  command 1
else
  command 2

Best Answer

You can use the changepage package, a minimal working example is

\documentclass{article}
\usepackage[strict]{changepage}
\begin{document}

\checkoddpage
\ifoddpage odd\else even\fi

\newpage
\checkoddpage
\ifoddpage odd\else even\fi

\newpage
\checkoddpage
\ifoddpage odd\else even\fi

\end{document}

If you use the memoir class, then this feature is built in automatically, but you should write \strictpagecheck in the preamble to make the test robust.

Note that if the \checkoddpage falls between two pages the result of the test could still be wrong even if strict mode is on. E.g., you finish a paragraph, you're on the end of page 4, write \checkoddpage which gives false, then insert something which breaks over to the next page. In that case you need to write \leavevmode\checkoddpage or somehow get the check into whatever is being typeset next, rather than before it.

However, most of the time \checkoddpage is used in contexts were this isn't an issue so it shouldn't be a major concern.