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
\foreachfrompgf.
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}
\numexpr\value{MyCounter}-1<\MaxValue– egreg Feb 22 '12 at 18:43xifthenextends the range of allowed tests (and is a recommended substitute ofifthenanyway); it has a\cnttestthat admits<=and>=. Nevertheless, I'd recommendetoolboxor betterexpl3for more complicated tests. – egreg Feb 22 '12 at 23:32\orhas a different meaning than expected. – Werner Sep 03 '13 at 14:08