How do I reset the figure numbering after a header using Quarto

I am using a Typst template in Quarto for a report and I want to number my images and tables. I want them to be numbered as Figure 1.1, Figure 1.2 in chapter 1 and as Figure 2.1, Figure 2.2 in chapter 2. Currently, the figures in chapter 2 are numbered as Figure 2.3, Figure 2.4. The first number is correct, but I want the second number to be resetted at the start of each chapter.

I import my typst template in the yaml. In the typst template I use the following pieces of code:

show heading.where(level: 1): hdr => {
	  counter(figure.where(kind:image)).update(0)
	  counter(figure.where(kind:table)).update(0)
	  hdr
  } 

set figure(numbering: n => {
	              let hdr = counter(heading).get().first()
	              let num = query(selector(heading).before(here())).last().numbering
	              numbering(num, hdr, n)
  })

To my understanding, the first piece of code should reset the second number in the figure and table numbering when a new chapter is made by placing a new header.

Other people using typst directly (so no Quarto) do use the same code, and for them the counter resets correctly (see for example: Do I need different code for numbering equations and figures? - #2 by Eric or How to change numbering in Appendix? - #6 by slowjo). If this a bug between Quarto and typst?

Hello. These rules do work correctly in Typst. I don’t know what Quarto is, but there is little chance it can compile Typst code, so it shouldn’t matter what you use, what matters is what Typst code is being compiled. Without providing code that doesn’t work, I can’t help. See https://sscce.org/.

Also, please read How to post in the Questions category.

There is also subpar – Typst Universe.

The general approach works for Quarto, but you have to target the custom kind that are generated from your .qmd file.

So, update the show rule to:

show heading.where(level: 1): hdr => {
	  counter(figure.where(kind: "quarto-float-fig")).update(0)
	  counter(figure.where(kind: "quarto-float-tbl")).update(0)
	  hdr
  } 

To avoid having to manually reset all kinds separately, you can also query for all figure kinds used in the document:

#show heading.where(level: 1): it => {
  let kinds = query(figure).map(fig => fig.kind).dedup()
  for kind in kinds {
    counter(figure.where(kind: kind)).update(0)
  }
  it
}

This way you don’t need to know that quarto uses different figure kinds (though this of course requires that you actually want to reset all figure kinds).

1 Like