Skip to content

The counter documentation does not help with multi-level counters #5442

@gasche

Description

@gasche

Description

I needed a multi-level counter, but I found that the counter documentation only really discusses single-level counter, which made it much harder than it should have been.

I wanted to index "questions" with numbers of the form S.C, where S is the section counter and C is a question-specific counter. (For example: 1.1, 1.2, then 2.1). Here is the code that I ended up writing:

#let question_counter = counter("question")
#question_counter.update((0, 0))
#show heading: it => {
  if it.level == 1 {
    question_counter.update((s, c) => (s+1, 0))
  }
}

#let question(body) = {
 question_counter.update((s, c) => (s, c+1))
 strong([ Question #context{ question_counter.display() } ])
 body
}

= Example usage
#question[Foo]
#question[Bar]

= More example
#question[Baz]

This required too much trial-and-error, I believe that the counter documentation should be improved to make this easy. For example:

  1. There are no examples of update with multi-level counters.
  2. The behavior of step on multi-level counters is not explained.
    • Does the indexing start at 0 or 1?
      Answer: it appears to start at 1.
    • is question_counter.step(level: 1) equivalent to question_counter.update((s, c) => (s+1, c)) or to question_counter.update((s, c) => (s+1, 0))?
      Answer: in my tests, neither, it appears to be unusable with 2-level counters.
  3. I get weird errors if I remove the #question_counter.update((0, 0)) initialization, why? Is there a way to indicate that a counter is multi-level at creation time?

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions