Label as parameter in function possible?

Hello, I’m coming from LaTeX to check whether Typst is better suitable for simple student papers.

Now I’m learning scripting in Typst and fail in creating a function just for the page number of a label:

… On #ref(@mylabel, form: “page”) we discussed …

should become

… On #pageref(mylabel) we discussed …

but my approach

#let pageref(label) = ref(label, form: “page”)

fails. I’ve found the varioref package, but it uses the same syntax and fails too.

Is there any (other) way to use a label as parameter in a Typst-function to achieve this?

Thanks in advance!

Sure, your function can work. The main thing is that ref wants an actual label value.

So your code should be:

#let pageref(label) = ref(label, form: "page")

= Some heading <mylabel>

We discussed this on page #pageref(<mylabel>).

The difference is that <mylabel> is a label, while mylabel without the angle brackets is treated as a variable name.

If you want to call it with a string instead, you can also do:

#let pageref(labelname) = ref(label(labelname), form: "page")

#pageref("mylabel")

So you were very close. The function itself is fine, you just need to pass the label in the right form.

3 Likes

Thank you very much!