How to override heading numbering behavior in Typst

135 views Asked by At

I'm submitting to a journal that requires headings of the following form:

I. TOP LEVEL HEADING

A. First subheading

1. Second subheading

It's easy enough to do the capitalization and italicization using #show heading.where() and to get the numbering almost correct by setting the heading numbering to "I.A.1.". The problem is that each heading shows all of its parents numbers, as well as its own. So the third subsection of the first section appears as I.C Subsection title when I want it to just be C Subsection title. Does anyone know of a way to get around this?

I tried to construct a show heading statement that trims off everything before the last two characters before the last space. But the heading object you get access to isn't a string, so you can't do string operations on it. You can extract the heading body, but not the number (since that's not passed to the heading() function; it's evaluated later). Maybbe there's a way to override numbering()? Or intervene before numbering() gets called and only pass it the last number?

1

There are 1 answers

0
Galactic Ketchup On

I figured it out! The answer was much simpler than what I was initially attempting to do. It turns out you can pass a function to numbering() instead of a string. The set statement below was all I needed.

set heading(numbering: (..numbers) => {
  let level = numbers.pos().len()
  if (level == 1) {
    return numbering("I", numbers.pos().at(level - 1))
  } else if (level == 2) {
    return numbering("A", numbers.pos().at(level - 1))
  } else {
    return numbering("1", numbers.pos().at(level - 1))
  }
})