When does counter step apply when used in a show rule?

In your second approach, it looks like that current_child_count is always zero in Typst鈥檚 first layout run. As a result, we are creating another enum(..., start: 0) in show enum.where(start: 0), causing the maximum show rule depth exceeded error.

You can circumvent it by adding a if guard:

#show enum.where(start: 0): it => {
  let args = it.fields()
  for i in args.remove("children") {
    enum_child_counter.step()
    context {
      let current_child_count = enum_child_counter.get().first()
      if current_child_count > 0 { // 馃憟 Exclude the recursive case
        enum(..args, start: current_child_count, i)
      }
    }
  }
}

Related links:

When you define and use a custom counter, in general, you should first step the counter and then display it. This way, the stepping behaviour of a counter can depend on the element it is stepped for.

(Personally speaking, I think that鈥檚 only relevant to multi-level counts.)

1 Like