I have some text in my document which appears repeatedly many times throughout the document, so I have defined a custom macro, e.g.:

        There is some text.
        This is some more text.
        This is yet some more text.

This appears like this:

|                       |
| 1.1 A                 |
|    This is some text. |
| 1.2 B                 |
|    This is some more  |
|    text.              |
| 1.3 C                 |
|    This is yet some   |
|    more text.         |

I need the macro to render differently in two situations.

  • The first time the macro appears in the document, the title and contents of section A should not appear. Instead, alternative text apears.
  • The last time the macro appears in the document, the title and contents of section C should not appaer. Instead, nothing appears there.

How can I create a conditional which checks if it is the first or last time the macro has been used, and modifies the text displayed accordingly?

  • I don't know if this solution is relevant to ConTeXt but for the first occurrence maybe a similar technique could be used? For the last occurence, could you increment a counter at each call, store the value of the counter somewhere (as in your question here) and have within your macro a check and branch for the final count? That might be a naive suggestion, I'm not familiar with ConTeXt.
    – Scott H.
    Jun 12, 2012 at 0:47
  • 2
  • 1
    Yes, I spent several hours attempting a solution, before posting, but had very limited success. Is it possible that the code for ConTeXt macros related to counters has been changed quite a bit from release to release? I have had very poor luck getting them to work with various versions of ConTeXt, hense many questions related to them.
    – Village
    Jun 12, 2012 at 23:31
  • Ahh, I see. I know ConTeXt used to use a set of commands ending in 'number': \getnumber, \incrementnumber, and the like; \setpagenumber & co. were part of that same system. Nowadays ConTeXt uses a more LuaTeX-integrated system with \incrementcounter and the like, and the 'number' commands have been redefined as synonyms for the 'counter' system. Then there are the enumerations, which use one of the two? That's all I know; but I don't know what changed when, or what MkII uses/d.
    – Esteis
    Jun 13, 2012 at 11:21
  • My best advice for you, since you use MkIV: go skim through strc-num.mkiv, and that should give you a good impression of which user-facing commands are available to you. The counter system and its commands haven't been documented on the wiki yet; I hope to get around to that soon.
    – Esteis
    Jun 13, 2012 at 11:25

1 Answer 1


Counters are useful things. Here, I access them mainly from the Lua side, using structures.counters.value for value access, structures.counters.add for incrementing, and structures.counters.last to obtain the highest value attained occurs in the document. From the TeX side, I would have used \rawcounter, \incrementcounter, and \lastcounter. For more on the counter API, see http://wiki.contextgarden.net/Counters, and read strc-num.mkiv and strc-num.lua.


    userdata = userdata or { }
    function userdata.domystuff() 
        -- * We must increment before, not after, or else the last value
        --   of the counter will always be one more than the value at last
        --   printing
        -- * I don't know why all these functions need a 1 as their second argument.
        structures.counters.add("mystuff", 1, 1)
        counter_val = structures.counters.value("mystuff", 1)
        if counter_val == 1 then
            context("We are at counter %s, the first counter.", counter_val)
            tex.sprint("We are at counter \\rawcountervalue[mystuff]")

        tex.sprint("section B's text")

        if not (counter_val == structures.counters.last("mystuff", 1)) then
            context("Last", counter_val)
            context.par() -- start a new paragraph
            context("(Last section omitted)")

% Define a TeX command to call the Lua function

% Let's try it out

Thanks to Aditya's answer here, and to Scott H. for his comment pointing at it.

You must log in to answer this question.

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