How to implement sub-numbering for custom figures and equations?

After reading a post here about this topic for equations where the first number of the numbering should be from the level 1 heading I found this solution:

#set heading(numbering: "1.")
#show heading.where(level:1): it => {
  counter(math.equation).update(0)
  it
}

#set math.equation(
  numbering: it => {
    let count = counter(heading.where(level: 1)).at(here()).first()
    if count >0 {
      numbering("(1.1)", count, it)
    } else {
      numbering("(1)",it)
    }
  }
)

How I can implement it also for this custom figure (theorem):

#show figure.where(kind: "thm"): it => [
  / #it.supplement #context(it.counter.display(it.numbering)).: #it.body
]
#let thm(it) = figure(
  kind: "thm",
  supplement: "Theorem",
  caption: none,
  placement: none, 
  it
) 

What will be the most correct way to implement a thing like this? Any tips on making this numbering function to be more generic or to define the theorem better would help a lot!

Setting the kind argument will automatically generate a counter. I learned a bit more since the last post (e.g., .get() is the same as .at(here())).

#set heading(numbering: "1")

#show heading.where(level:1): it => {
  counter(figure.where(kind: "thm")).update(0)
  it
}

#show figure.where(kind: "thm"): it => {
  let num = numbering(
    "1.1",
    counter(heading).get().first(),
    it.counter.get().first()
  )
  [/ #it.supplement #num: #it.body]
}

#let thm(it) = figure(
  kind: "thm",
  supplement: "Theorem",
  // default values of caption & placement are already none :)
  it
)


= one chapter

#thm(lorem(20))
#thm(lorem(20))
#thm(lorem(20))
#thm(lorem(20))

= another chapter

#thm(lorem(20))
#thm(lorem(20))
#thm(lorem(20))
#thm(lorem(20))

Follow-up question though… I had never seen the [\ text:] styling before for bolding. Where is that from? Is it just another way to bold text?

@miles-1 The slash syntax is for term lists.

@Ido As you may already know, there are packages like great-theorems which take care of all of this.

1 Like

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

For future posts, make sure your title is a question you’d ask to a friend about Typst. :wink:

1 Like

Thanks a lot! I am having some rough time adding the if statement to this implementation for the figure numbering… any help on that?

If I’m understanding your initial code correctly, you want to make all theorems before the first header just have a theorem counter number (e.g., (1)), and all theorems after a header to have that header’s number and the theorem counter number (e.g., (1.1)), yes? If that’s the case, inside your show rule for thm figures, you will want to:

  • save current header count in a variable (e.g., let header_count = ...)
  • save current theorem count in a variable (e.g., let thm_count = ...)
  • use an if statement to use one of the two numbering uses based on header_count, and save its result to a variable (e.g., let num = if header_count > 0 {...} else {...})

Give that a try!

Something like this?

#show figure.where(kind: "thm"): it => {
    let header_count = counter(heading.where(level: 1)).get().first()
    let thm_count = it.counter.get().first()
    let thm_num = if header_count > 0 {
      numbering("1.1.",header_count,thm_count)
    } else {
      numbering("1.",thm_count)
    }
    set align(left)
    terms[/ #it.supplement #thm_num: #it.body]
}

Thanks a lot!!

1 Like