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.

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?

share|improve this question
    
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 '12 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 '12 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 '12 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 '12 at 11:25

1 Answer 1

up vote 4 down vote accepted

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.

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.