Hey there, so, I’ve seen the two posts on automatic headings, yes they did not fulfill what I needed fully, so here I am posting : D
I have:
= Chapter 1
== Definition
== Lemma
===
some statemenet
===
statement 2
and the numbering convection is 1.1.i.a, so when I type e.g.
@1.2
I want it to render as:
Lemma 1.2, not as 1.2 (which I currently have).
here is the code (it is inside the template hence no shebangs)
// Make a key like "1.13" from a numbering tuple (1, 19)
let make-number-key = nums => {
nums.map(str).join(".")
}
// Extract first word of heading text, e.g. "Lemma" / "Definition"
let heading-kind = it => {
let text = {
if it.has("text") and type(it.text) == "string" {
it.text
} else if it.has("children") {
it.children
.map(c => if c.has("text") and type(c.text) == "string" { c.text } else { "" })
.join("")
} else {
""
}
}
text = text.replace("\n", " ").trim()
if text == "" {
// "Section" // this was removed to inspect its effect
} else {
text.split(" ").at(0)
}
}
show heading: it => context {
// current heading numbering, e.g. (1, 19)
let nums = counter(heading).get()
let key = make-number-key(nums)
// this will be "Lemma", "Definition", etc.
let kind = heading-kind(it.body)
// attach a figure so the heading becomes referenceable
let fig = figure(
kind: "heading",
numbering: (..numbers) => numbering(heading-numbering, ..nums),
supplement: kind, // used if you manually show this figure
)[]
[
#it
#v(-1em)
#fig
#label(key)
]
}
show ref: it => {
let el = it.element
if el == none {
// element not discovered yet or wrong label: fall back
return it
}
// We only change references that point to headings
if el.func() != heading {
return it
}
// Extract first word of heading for the supplement
let kind = heading-kind(el)
// Display: "<Kind> <number>", e.g. "Lemma 1.19"
link(
el.location(),
[
#kind #space()
#numbering(el.numbering, ..counter(heading).at(el.location()))
],
)
}