I have two counters, \definenumber[words] and \definenumber[items]. I have tried to use TeX conditionals to compare them, e.g.:

    This will print some text if the two counters have the same value.

This does not work. How can I use TeX conditionals to compare two counters in ConTeXt?


\definenumber is a synonym for \definestructurecounter. You can use the expandable version \rawstructurecountervalue to get the value of the structure counter and use them in TeX conditionals.

An alternative is to use the commands.doifelse(...) lua function to do the comparison. I present both versions below (using named arguments so that the macro is easy to understand. Either save this in a file with an extension .mkvi or use % macros=mkvi on the first line of the .tex file).

% macros=mkvi


    {\ctxcommand{doifelse(\rawstructurecountervalue[#counterA] #operator \rawstructurecountervalue[#counterB])}





  One is bigger
  Two is bigger

\doifelsecomparestructurecounter{>=}{one}{two}{Counter one is bigger}{Counter two is bigger}
\doifelsecomparestructurecounter{<}{one}{two}{Counter one is smaller}{Counter two is smaller}
\doifcomparestructurecounter{<}{one}{two}{Counter one is smaller}
\doifcomparestructurecounter{>}{one}{two}{Counter two is smaller}
| improve this answer | |
  • Is there any other way to codify a numeric inequality conditional that's less abstruse? Why doesn't \ifnum\rawcountervalue[MyCounter]>0 work, for example? Could it be made to work without everyone adding their own unprotected/unexpanded macros to accomplish the same task? – Dave Jarvis Apr 5 at 18:34
  • Does \definestructurecounter exist anymore? – Dave Jarvis Apr 5 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.