How to use custom counters in heading titles?

Hello dear people,
I have the following problem and didn’t yet find an answer here:

I have a document with special sections for excercises (called “labs” here) and
I would like to have them numbered.

So, ich have chapter 1 with three labs (1, 2 and 3) , chapter 2 with 2 labs (4 and 5) and so on.
There is the counter type, easy.

The problem starts with the outline, it should be

1. chapter 1: foo
    1.1 Lab 1: fizz
    1.2 Lab 2: buzz
    1.3 Lab 3: fizzbuzz
2.chapter 2: bar
    2.1 Lab 4: blafoo
    2.2 Lab 5: foobla

but acutally it compiles to

1. chapter 1: foo
    1.1 Lab 0: fizz
    1.2 Lab 0: buzz
    1.3 Lab 0: fizzbuzz
2.chapter 2: bar
    2.1 Lab 0: blafoo
    2.2 Lab 0: foobla

The code looks kind like this (try to condense it):

theme/utils file

#let labCounter = counter("lab")
#let lab-slide(title: []) = {
  labCounter.step()
  heading([Lab #context labCounter.display(): #title)
  ...
}

and the main file

...
#outline()

#include("./chapter1.typ")
#include("./chapter2.typ")

and then chapter1.typ

...
#lab-slide(title: "Try this for real")
...

I hope, this is understandable and that someone is able to tell me where
I am holding it the wrong way around.

Thanks :slight_smile:

The outline always uses the value of the labCounter in the current context. Since the outline is located before any of the lab slides, the counter will just be 0.

If you place the entire heading() into the context, the heading titles are displayed correctly in the outline.

#set heading(numbering: "1.1")
#let lab-counter = counter("lab")

#let lab-slide(title) = {
  lab-counter.step()
  context { heading(level: 2, [Lab #lab-counter.display(): #title]) }
}

#outline(indent: auto)

= foo
#lab-slide("fizz")
#lab-slide("buzz")
#lab-slide("fizzbuzz")

= bar
#lab-slide("blafoo")
#lab-slide("foobla")

I am not sure about the technical reasons for this behavior. The outline just seems to grab all the headings and it does not care if the function heading() was called in a specific context? I created a minimal example project to show this behavior here. If you hover over the it inside the show rule for the outline entries, the context() is not mentioned anywhere. In your case the entry.heading.body would be sequence([Lab], [ ], context(), [:], [ ], [fizz]).

1 Like

Ha, works like a charm :-) Thanks @janekfleper for the quick answer.

This problem also looks similar to the following:

The difference is, that post is about the proper heading numbering, while here you want the number as part of the title. Maybe it still helps or gives you more ideas.