4

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

\define\mymacro{
    \section{A}
        There is some text.
    \section{B}
        This is some more text.
    \section{C}
        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?

Village
  • 13,603
  • 23
  • 116
  • 219

1 Answers1

4

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.

\definecounter[mystuff]

\startluacode
    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.section("A")
            context("We are at counter %s, the first counter.", counter_val)
        else
            context.section("A")
            tex.sprint("We are at counter \\rawcountervalue[mystuff]")
        end

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

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

% Define a TeX command to call the Lua function
\def\domystuff{\ctxlua{userdata.domystuff()}}

% Let's try it out
\starttext
    \startcolumns[n=2]
        \dorecurse{3}{
            \domystuff
        }
    \stopcolumns
\stoptext

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

Esteis
  • 2,917