How can I list all labels that aren't referenced in the document?

Hi, welcome to the Typst forum!

I don’t think there’s a way to get all labels, but you can query for all elements of a particular type and see which label they have, so you can do something like this:

#set math.equation(numbering: "1.")

#let check-labels() = context {
  let targets = query(ref).map(x => x.target)
  let unreferenced = query(selector.or(
    figure,
    math.equation,
    // ... add other referencable elements here
  )).map(x => x.at("label", default: none))
    .filter(x => x != none and x not in targets)

  if unreferenced.len() > 0 {
    set text(red)
    [
      Unreferenced labels: #unreferenced.map(str).join(", ").
      #metadata(unreferenced)<unreferenced-labels>
    ]
  }
}

#check-labels()

$ x $<eq1>
$ y $<eq2>
$ z $<eq3>

See @eq2.

And to retrieve the list from the command-line: typst query file.typ '<unreferenced-labels>

2 Likes