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.

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
share|improve this question
6  
4  
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
    
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
2  
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

6 Answers 6

up vote 7 down vote accepted

This answer is wrong. Don't read it and don't vote on it. Thank you.

Use the ifthen package:

\documentclass{article}
\usepackage{ifthen}
\begin{document}
 \ifthenelse{\isodd{\thepage}}
   {Odd}
   {Even}
\end{document}
share|improve this answer
10  
This is wrong on several accounts: First \thepage doesn't have to be numeric, it can be 'iv' for example. You should use \value{page} instead. Second, the page counter value is incremented in the ship-out routine and TeX reads often more text to decide where to put the page break, so that the page counter is not reliable. The only way to get a reliable value is to use references. –  Martin Scharrer Sep 12 '11 at 22:31

Use the changepage package:

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

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

share|improve this answer

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

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}
share|improve this answer
    
Thanks, super useful! makeatother for completeness? –  Joe Corneli Dec 10 '12 at 1:26
    
@JoeCorneli Thanks, added! –  cgnieder Dec 10 '12 at 9:51

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.

share|improve this answer

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

 
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.