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.

I am in the process of cleaning up the code for Sieve of Eratosthenes in tikz, but saw that when I initially did the code, I could not figure out how to do a <= comparison within the \whiledo, and had resorted to doing a < comparison with one more than the desired number. This feels like a hack to me, and since @StefanKottwitz is considering posting this on another site with tikz examples, I'd prefer to not have to resort to such hackery.

Question:

How do I implement a <= comparison within a \whiledo such as:

\whiledo{\value{MyCounter} <= \MaxValue}{

Notes:

  • As much as I would like to, I can't use the \foreach from pgf.

Code:

\documentclass{article}
\usepackage{ifthen}

\newcounter{MyCounter}

\def\StartValue{5}
\def\MaxValue{10}

\begin{document}
Note that 10 is missing when comparing to MaxValue:

\setcounter{MyCounter}{\StartValue}
\whiledo{\value{MyCounter} < \MaxValue}{
    \theMyCounter
    \stepcounter{MyCounter}
}

\bigskip
\textbf{Hack:} Comparing to MaxValuePlusOne does the job:

\newcounter{MaxValuePlusOne}
\setcounter{MaxValuePlusOne}{\MaxValue}
\stepcounter{MaxValuePlusOne}
%
\setcounter{MyCounter}{\StartValue}
\whiledo{\value{MyCounter} < \value{MaxValuePlusOne}}{
    \theMyCounter
    \stepcounter{MyCounter}
}
\end{document}
share|improve this question
    
Is a LaTeX3 solution allowed? –  Marco Daniel Feb 22 '12 at 18:39
1  
\numexpr\value{MyCounter}-1<\MaxValue –  egreg Feb 22 '12 at 18:43
    
@MarcoDaniel: I think that would be a good to have here as a reference, but I think egreg's solution seems simpler. –  Peter Grill Feb 22 '12 at 18:46
    
@egreg: As usual, works great. Thanks. You should add that as an answer. –  Peter Grill Feb 22 '12 at 18:47
    
(<=) is equivalent to (not >) and \not is defined and works with ifthen package. –  Alain Matthes Feb 22 '12 at 20:25

2 Answers 2

up vote 6 down vote accepted

We can define \newwhiledo with LaTeX3 macros or use the \numexpr trick, take your pick:

\documentclass{article}
\usepackage{ifthen}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\newwhiledo}{m m}
  {
   \bool_while_do:nn { \int_compare_p:n {#1} } { #2 }
  }
\ExplSyntaxOff

\newcounter{MyCounter}

\def\StartValue{5}
\def\MaxValue{10}

\begin{document}
Note that 10 is missing when comparing to MaxValue:

\setcounter{MyCounter}{\StartValue}
\newwhiledo{\value{MyCounter} <= \MaxValue}{
    \theMyCounter
    \stepcounter{MyCounter}
}

\bigskip
\textbf{Hack:} Using \verb|\numexpr|

\setcounter{MyCounter}{\StartValue}
\whiledo{\numexpr\value{MyCounter}-1 < \MaxValue}{
    \theMyCounter
    \stepcounter{MyCounter}
}
\end{document}
share|improve this answer

<= is equivalent to not >

\documentclass{article}
\usepackage{ifthen}    
\newcounter{MyCounter}
\begin{document}
Note that 10 is not missing when comparing to MaxValue:

\def\StartValue{5}
\def\MaxValue{10}

\setcounter{MyCounter}{\StartValue}
\whiledo{\not{\value{MyCounter}>\MaxValue}}{%
    \theMyCounter
    \stepcounter{MyCounter}
}
\end{document} 
share|improve this answer

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.