60

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
  • 6
  • 6
    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
  • 1
    Isn't this question a duplicate of tex.stackexchange.com/q/5894 ? – Frédéric Grosshans Nov 29 '10 at 16:09
  • 1
    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
  • 3
    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
51

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.

| improve this answer | |
  • It is so weird, if I include the command \strictpagecheck, the first two pages of my article are not checked correctly, but the third page is fine. If I remove \strictpagecheck, all three pages will work just fine. – zyy May 3 at 0:11
  • @zyy - did you compile twice? It takes at least two compilations to work properly. I'm not aware with any major problems with this package... – Will Robertson May 12 at 14:10
  • I did, I just tried again and every page is even. Is this supposed to be used in the preamble only? – zyy May 12 at 18:48
  • I don't think it's been extensively tested but should be fine to turn on and off mid-document. I can reproduce an issue if \checkoddpage falls between two pages it could be wrong. (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.) If you have more issues please ask a new question on the site. – Will Robertson May 14 at 0:43
  • 1
    @zyy Since you edited my answer I figured I may as well add the full detail of our testing... – Will Robertson May 15 at 3:04
35

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}
| improve this answer | |
  • Thanks, super useful! makeatother for completeness? – Joe Corneli Dec 10 '12 at 1:26
20

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}
| improve this answer | |
  • This is basically what the changepage package, suggested by Will, does. – Martin Scharrer Sep 12 '11 at 22:32
7

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.

| improve this answer | |
5

With the etoolbox package (Philipp Lehman, Joseph Wright): \ifnumodd{\thepage}{\RightSideBar}{\LeftSideBar}

\documentclass[twoside]{article}
\usepackage{lipsum}
\usepackage{atbegshi}
\usepackage{tikz}
\usepackage{adjustbox}
\usepackage{etoolbox}

\begin{document}

\def\LeftSideText{Left side text}
\def\RightSideText{Right side text}

\newcommand{\LeftSideBar}{
\begin{tikzpicture}[remember picture,overlay]
\draw[fill,color=red]
([xshift=1cm,yshift=0cm]current page.south west) rectangle ([xshift=0cm,yshift=0cm]current page.north west);
\node [rotate=90] at ([xshift=0.5cm,yshift=0cm]current page.west) {\sffamily\bfseries\resizebox{!}{0.5cm}{\color{white}{\LeftSideText}}};%
\end{tikzpicture}
}% 

\newcommand{\RightSideBar}{
\begin{tikzpicture}[remember picture,overlay]
\draw[fill,color=blue]
([xshift=-1cm,yshift=0cm]current page.south east) rectangle ([xshift=0cm,yshift=0cm]current page.north east);
\node [rotate=90] at ([xshift=-0.5cm,yshift=0cm]current page.east) {\sffamily\bfseries\resizebox{!}{0.5cm}{\color{white}{\RightSideText}}};%
\end{tikzpicture}
}% CadreDePage


\AtBeginShipout{
\ifnumodd{\thepage}{\RightSideBar}{\LeftSideBar}
}


\lipsum[1-20]

\end{document}

enter image description here enter image description here

| improve this answer | |
0

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}
}
| improve this answer | |
  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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