GeneralDoing modulus with counters

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
angelixd
Posts: 15
Joined: Thu Jul 26, 2007 4:53 pm

Doing modulus with counters

Post by angelixd »

I would like to be able to do something dependent on whether or not a counter is even. something like

Code: Select all

\include{ifthen}
...
\ifthenelse{\isEven{mycounter}}
           {% code for even case
           }
           {% code for odd case
           }

\newcommand{\isEven}[1]{
   \ifthenelse{ ??? }{TRUE}{FALSE}

}

The only thing is doing calculations in LaTeX is like pulling teeth, and I'm sure as hell that '%' is not a mathematical modulus operator... I've read through the calc package looking for clues, but I couldn't find any leads. Does anyone know i'd go about writing \isEven{} ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Doing modulus with counters

Post by gmedina »

You can use the \isodd command to test whether a given number is odd; in the following simple example the \myExample command will write its argument in \Huge size if \myCounter has an odd value and will write its argument in \tiny size if the counter has an even value.

Code: Select all

\documentclass{article}
\usepackage{ifthen}
\newcounter{myCounter}
\newcommand{\myExample}[1]%
 {\ifthenelse{\isodd{\themyCounter}}{\Huge #1 \par}{\tiny #1 \par}}

\begin{document}
\stepcounter{myCounter}
\myExample{text}
\stepcounter{myCounter}
\myExample{text}
\end{document}
Edit: I corrected some spelling issues
Last edited by gmedina on Fri Nov 09, 2007 1:10 am, edited 1 time in total.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
balfonsi
Posts: 93
Joined: Wed Mar 14, 2007 12:05 am

Re: Doing modulus with counters

Post by balfonsi »

It's much simpler than what you think: there is an \isodd command defined by the ifthen package. So you just have to write

\ifthenelse{\isodd{\value{myCounter}} {%code for oddcase%
}{%code for even case%
}%

Btw, % is just a comment sign for the rest of the line.

Best regards,
B.A.
angelixd
Posts: 15
Joined: Thu Jul 26, 2007 4:53 pm

Re: Doing modulus with counters

Post by angelixd »

Hey guys, thanks a whole bunch. Time to get to work!
Post Reply