How can I display labels in heading body?

Hi there. I’m quite new to typst and I need some help on this: I would like to assign a label to a heading

= Test <id1>

= New test<id7>

and have it rendered like

1 Test (id1)

2 New test (id7)

Is this possible? Thanks a lot for helping.

RG

You can override the heading’s full show rule to achieve this. I think we should prefer solutions where we don’t have to resort to this, and can instead add the required decoration as an add-on to the default style, but I don’t know a good way to do it for this case.

#show heading: it => {
  show: block
  if it.numbering != none {
    counter(heading).display()
    h(0.3em)
  }
  it.body
  if it.has("label") {
    h(0.3em)
    [(#str(it.label))]
  }
}
#set heading(numbering: "1")

= Test <id1>
= New test <id7>

1 Like

That’s perfect, thanks! I was using a function like this

#let q(id, title) = [
  = #title (#id)
  #label(str(id))
]

but I really want to use the normal heading syntax and your solution is exactly what I was lookin for.

Now I can go back to study Typst, really wonderful trip after almost 20 years of LaTeX and ConTeXt.

Thanks.

I understand. I think your solution should be the preferred one, but I would think along the same lines, I want all headings to use the same nice heading syntax (have the same color in the editor and so on).

For completness I’ve added also this code for the outline in order to have the
label properly shown in the outline items.

#show outline.entry: it => {
  let label = str(it.element.at("label", default: none))
  return block(it.inner() + " (" + label + ")")
}

I’m not sure is the best way to deal with the thing, but it seems to work.

Thanks again.