TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The example for the forloop package is \newcounter{ct}\forloop{ct}{1}{\value{ct} < 5}{\arabic{ct}}. I need to compare two counters having the first run up to the second. <= doesnt work, neither does \leq or ...+1. I know I can solve it with \not or \or, but those are workarounds. How does it really work?

  1. Can I and if so how can I do a "real" <=?
  2. Can I and if so how can I use formulas (like \value{a}<\value{b}+1)?
share|improve this question
up vote 9 down vote accepted
  1. No. It is based on TeX's \ifnum, and only <, =, > is supported.

  2. Use eTeX primitive \numexpr:

    \newcounter{ct} \forloop{ct}{1}{\value{ct} < \numexpr\value{page}+10}{\arabic{ct} }

    Or you can use etoolbox package (looks awful for this simple bool expression, I agree):

    \whileboolexpr{test {\ifnumcomp{\value{ct}}<{\value{page}+10}}}
      {\arabic{ct} \stepcounter{ct}}
share|improve this answer
In the first case, you may want to add \relax to be sure to end \numexpr at the right place (\numexpr will remove it). Otherwise, \numexpr may end up trying to expand some code which is placed after that argument. It turns out that \ifthenelse has a \relax at the right place, but I wouldn't rely on that. – Bruno Le Floch May 1 '11 at 6:09

The expl3 programming package allows you to use extended comparison operators including <= and so on; there's a few ways to perform your specific task here (e.g., also see \prg_stepwise_inline:nnnn), but here's one example that uses the ‘do while’ method explicitly:

\int_new:N \ct
\int_set:Nn \ct {10}

  { \int_compare_p:n { \ct <= 17 } }
    [ \int_use:N \ct ]
    \int_incr:N \ct
share|improve this answer

Your Answer


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.