Why does my heading label errors with "cannot reference text"?

If you have a programatic labelled heading, i.e., your label is contained in a typst variable

#let lab= label("asdf")
= asdf #lab

you may have encountered the error

error: cannot reference text
@asdf // error: cannot reference text
1 Like

You should write instead,

= asdf
#lab

Why does this happen?

This occurs because your code is not parsed as

#heading(level: 1, [asdf]) #asdf

but

#heading(level: 1, [asdf #asdf])

See [1] and [2] for details.

[1] Programmatic label placed on the same line with heading is confusing · Issue #3512 · typst/typst · GitHub
[2] How to reference a composite variable label? · typst/typst · Discussion #4043 · GitHub

4 Likes

Another way to get the same behavior, if you should prefer the heading on one line, is this:

#[= asdf] #lab

honestly I find your solution nicer though: it keeps the = at the beginning of the line and thus seems more readable to me.

1 Like