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.

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

2 Answers 2

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):

    \newcounter{ct}
    \setcounter{ct}{1}
    \whileboolexpr{test {\ifnumcomp{\value{ct}}<{\value{page}+10}}}
      {\arabic{ct} \stepcounter{ct}}
    
share|improve this answer
2  
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:

\documentclass{article}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\int_new:N \ct
\int_set:Nn \ct {10}

\bool_while_do:nn
  { \int_compare_p:n { \ct <= 17 } }
  {
    [ \int_use:N \ct ]
    \int_incr:N \ct
  }
\ExplSyntaxOff
\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.