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
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
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.
\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
\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
\strictpagecheck is supposed to be in the preamble, I made suggested editions to your answer, thanks!
– zyy
May 14 '20 at 01:12
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}
\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
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}
changepage package, suggested by Will, does.
– Martin Scharrer
Sep 12 '11 at 22:32
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}
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}
}
refcountpackage provides support for this. – Dec 06 '12 at 06:59