How to mutate variables in a show rule?

No, you are pretty much spot on. Here is a slightly modified version:

#set page(paper: "a5")

#show outline.entry.where(level: 1): it => {
  let i = counter("i-counter")
  i.step()
  context if calc.even(i.get().first()) {
    strong(it)
  } else {
    it
  }
}

#outline(depth: 3, indent: auto)

= First
= Second
= Third
= Fourth

image

If you don’t use the counter anywhere else, then it’s a good idea to put it where it is used. You can also use functions like even which will improve readability.

From https://typst.app/docs/reference/introspection/counter/:herb:

Counts through pages, elements, and more.

With the counter function, you can access and modify counters for pages, headings, figures, and more. Moreover, you can define custom counters for other things you want to count.

Since counters change throughout the course of the document, their current value is contextual. It is recommended to read the chapter on context before continuing here.

2 Likes