How to create dynamic reference and label?

Hi,

When I try to reference a label using @test, it’s not working properly. Here’s my code:

#set heading(numbering: "1.")
#outline(
  indent: auto
)
#show link: underline
#show ref: it => {
  let el = it.element
  if el != none and el.func() == heading {
    let nums = counter(heading).at(el.label)
    link(el.location(), numbering(el.numbering, ..nums) + el.body)
  }
}
= T1
== T2
#let name = "Test"
#let labelName = lower(name).replace(" ",":")
=== #name #label(labelName)
#lorem(10)
\
#name \
#labelName
=== Test 2 <test2>
This is reference for @test . Missing!!!
\
#lorem(10)

In my code, I’m trying to create a dynamic label using a variable name and then reference it with @test, but I get none in reference function. I have defined the label using #label(labelName) where labelName is created from the name variable.

I’ve tried:

  1. Using #ref("test") instead of @test
  2. Checking if the label is properly created with labelName
  3. Using an explicit label with <test>

But none of these approaches worked. What am I doing wrong? How can I make the reference work correctly?

This is an unfortunate instance of the wrong precedence being used here. The fix:

#[=== #name] #label(labelName)

Although I have to say, I’m a bit unsure why (without the show rule) the error you get is “label <test2> occurs multiple times in the document” – that is misleading.