71

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
current_user
  • 5,235
Regis Santos
  • 14,463

6 Answers6

56

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.

  • 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 03 '20 at 00: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 '20 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 '20 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 '20 at 00:43
  • Thanks, that is helpful, the \strictpagecheck is supposed to be in the preamble, I made suggested editions to your answer, thanks! – zyy May 14 '20 at 01:12
  • @zyy that's not what I said, it should work fine to put \strictpagecheck anywhere shouldn't it? – Will Robertson May 15 '20 at 02:59
  • 1
    @zyy Since you edited my answer I figured I may as well add the full detail of our testing... – Will Robertson May 15 '20 at 03:04
  • That looks good, thanks! – zyy May 15 '20 at 03:50
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}
cgnieder
  • 66,645
  • Thanks, super useful! makeatother for completeness? – Joe Corneli Dec 10 '12 at 01:26
  • From KOMA-Script 3.28 \ifthispageodd is deprecated and should be replaced by \Ifthispageodd (uppercase I). AFAIK the change was because of an advice by the LaTeX3 Project. – cabohah Aug 01 '23 at 05:52
23

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}
AlexG
  • 54,894
9

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

current_user
  • 5,235
SDrolet
  • 4,908
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.

Werner
  • 603,163
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}
}