How to change numbering in Appendix?

I’m writing a thesis and want to reset a numbering in Appendix.

I managed to reset the numbering of the headings by

#set heading(numbering: "A.1.1", supplement: [Appendix])
#counter(heading).update(0)
#text(size: 2em)[Appendix]

I also want to reset the numbering of Figures and Tables and to use a numbering like “Table A1”, “Figure A2”. Is it possible to implement it?

1 Like

Typst currently needs a workaround for this to work, as displaying the headings somehow doesn’t correctly track the numbering that’s set.

I’m in the same situation as you, and I currently got something like the following in my template:

#let backmatter(content) = {
	set heading(numbering: "A.1")
	counter(heading).update(0)
	state("backmatter").update(true)
	content
}
#set figure(numbering: it => {
	//workaround...
	let appx = state("backmatter", false).get()
	let alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	let hdr = counter(heading).get().at(0)
	if appx [#alph.at(hdr - 1).#it]
	else [#hdr.#it]
})
#show heading.where(level: 1): hdr => {
	counter(figure.where(kind:image)).update(0)
	counter(figure.where(kind:table)).update(0)
	hdr
}

// Example
#set heading(numbering: "1.1")
#let testfig(caption) = {
	figure(box(width:3cm, height:1cm, stroke: 1pt), caption: caption)
}
#set page(width: auto, height: auto, margin: 2em)

= Start
Citing the figures in order @first, @second, and @third.
#testfig[First Box] <first>

= Next
#lorem(10)
#testfig[Second Box] <second>

#show: backmatter

= Appendix
#lorem(10)
#testfig[Third Box] <third>

which produces

1 Like

It works! Thank you. Hope Typst will officially support these features.

Hey @kazuyanagimoto, welcome to the forum! I’ve changed your question post’s title to better fit our guidelines: How to post in the Questions category

Make sure your title is a question you’d ask to a friend about Typst. :wink:

1 Like

This is the proper way to do it (as of Typst 0.11 and 0.12)! The code inside the figure numbering function is given the original location of the figure as context, meaning any counter accesses in that function (including to the heading counter) will correctly return the value at where the figure was placed, even if the numbering is used in the outline. This won’t hold if you create a new context block inside the figure numbering function (then, the context will be relative to where the numbering is placed, not to the figure itself), so please don’t do that.

Regarding your solution, I’d note that the numbering function can be used to specify numbering formats (such as alphabetic) instead of having to implement that on your own: Numbering Function – Typst Documentation

Therefore, your figure set rule can be improved as follows (giving the same results):

#set figure(numbering: n => {
  let appx = state("backmatter", false).get()
  let hdr = counter(heading).get()
  let format = if appx {
    "A.1."
  } else {
    "1.1"
  }
  // Replace 'hdr.first()' by '..hdr' to display
  // all heading levels
  numbering(format, hdr.first(), n)
})
1 Like

huh, yeah you’re right, reimplementing the numbering isn’t necessary, not sure anymore why I ended up doing that.
my point about the numbering not being correctly tracked is that i’d like to be able to just use counter(heading).display(), and for it to use the correct numbering of the heading. This does however not work as I would expect.

Your answer did however get me thinking, and I took some time to tinker. I finally figured out how to get this to work without having multiple “sources of truth” as to what numbering to use where. It also does away with having to use the “backmatter” state, and is therefore cleaner.

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

this seems to work correctly, in the figures, references, and outlines as well.