I would like to create templates that add headings along with some other content to the document. It would be nice if I would not have to specify the heading level repeatedly and just have the template detect it itself.
For this I have created a function that uses context to determine the heading level of the most recent heading level.
#let get_local_heading_level(increment: 0) = {
let headings = query(selector(heading).before(here()))
if headings.len() > 0 {
let most_recent_heading = headings.last()
return headings.last().level + increment
}
return 1 + increment
}
This works well, if all the calls are within one context block. Unfortunately, if the calls to the function are in separate context block it works for three headings and then suddenly starts applying wrong heading levels. E.g heading 2.5 and 2.6 are shown as heading 4.
#set heading(numbering: "1.")
// this works
#heading([Heading 1], level: 1)
#heading([Heading 1.1], level: 2)
#context [
#heading([Heading 1.2], level: get_local_heading_level())
#heading([Heading 1.3], level: get_local_heading_level())
#heading([Heading 1.4], level: get_local_heading_level())
#heading([Heading 1.5], level: get_local_heading_level())
#heading([Heading 1.6], level: get_local_heading_level())
]
// this doesn't work
#heading([Heading 2], level: 1)
#heading([Heading 2.1], level: 2)
#context [ #heading([Heading 2.2], level: get_local_heading_level())]
#context [ #heading([Heading 2.3], level: get_local_heading_level())]
#context [ #heading([Heading 2.4], level: get_local_heading_level())]
#context [ #heading([Heading 2.5], level: get_local_heading_level())] // generates: 4. Heading 2.5
#context [ #heading([Heading 2.6], level: get_local_heading_level())] // generates: 4. Heading 2.6
Does anyone have an idea what the issue with the second example is?
Using the single context example is a fallback option, but since I am trying to create templates for my coworkers, I’d like to keep them as simple to use as possible.