Using ConTeXt, I have a macro which can be given a unique key value:


I need to be able to access information on the number of times that value has appeared so far in the document and the number of times that value appears in total. The code above would give me this information:


The information is not simply displayed in the page, I need to access this information for use in conditionals.

How can I obtain information on how many times a macro has held a certain value and how many times it will in the entire document?

  • 6
    just noticed the context tag, you should probably make it explicit in the question if you want context rather than latex. My answer below is working with latex, but most of the code is plain so would work with context if someone could fill in the context versions of AtEndDocument and \@auxout which I'm sure must exist. Apr 2, 2012 at 23:53

2 Answers 2


The canonical way to do this in ConTeXt is to use counters (and context takes care of the appropriate number of runs).


Note: I had to use a low-level Lua function doifnotstructurecounter because there is a bug in the TeX wrapper \doifundefinedstructurecounter. Once this bug is fixed, you can replace the first line with \doifundefinedstructurecounter{macro::#1}.

You might also be interested in my experimental visualcounter module that, well, displays counters visually.

  • Do I use \rawstructurecounter[macro::#1] to obtain the value for use in a conditional e.g. within \startlua: if \rawstructurecounter[macro::#1] < 5 then?
    – Village
    Apr 3, 2012 at 0:06
  • 1
    (Untested) \rawstructurecounter is defined as an unexpanded macro, so it will not be expanded inside \startlua. However, all the counters are stored in the counters.data table, and you should be able to access a counter using counters.value("macro::#1"). See strc-num.lua for details (look at the latest beta, because some of the old commands were dropped last week).
    – Aditya
    Apr 3, 2012 at 0:12
  • 1
    @Aditya: would it be wise to have \unexpanded\define ? Apr 3, 2012 at 8:13
  • 1
    @TacoHoekwater: I think so, it is not expandable. In any case the macro will fail miserably when placed where expandable text is desirable (section headers and such)
    – Aditya
    Apr 3, 2012 at 16:40
  • This does not seem to compile. I added the definition of the macro in the preamble, then tried some sample macros between \starttext and stoptext. system > tex > error on line 9 in file test.tex: LuaTeX error, line 9 has \macro{000010}.
    – Village
    Apr 7, 2012 at 5:54

This takes a couple of runs to get the totals:


\expandafter\ifx\csname count#1\endcsname\relax
  \expandafter\gdef\csname count#1\endcsname{0}%
      \global\noexpand\@namedef{maxcount#1}{\csname count#1\endcsname}}}%
\count@\csname count#1\endcsname
\the\count@/\csname maxcount#1\endcsname
\expandafter\xdef\csname count#1\endcsname{\the\count@}}








enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .