How to get the page number when referencing?

Here is a discussion about how to get the page number when referencing an image or table. (And if I understand correctly, it should also work for headings.): https://github.com/typst/typst/issues/2873.

In May, Laurenz posted a solution for Typst 0.11:

#let pageref(label) = context {
  let loc = locate(label)
  let nums = counter(page).at(loc)
  link(loc, numbering(loc.page-numbering(), ..nums))
}

You can use it as

see #pageref(<myref>)

This doesn’t seem to work for me using Typst 0.12 and gives an error. Not sure whether the code should be updated or I simply don’t really understand how to use it properly. Here is what I tried:

#let pageref(label) = context {
  let loc = locate(label)
  let nums = counter(page).at(loc)
  link(loc, numbering(loc.page-numbering(), ..nums))
}

= Foos and Bars
<myref>

#lorem(1000)

For more on foo, see #pageref(<myref>).

There is also a similar question by Goossa and there is a solution by Andrew: https://forum.typst.app/t/how-do-i-make-a-reference-that-inserts-name-of-section-and-page-number/461, and it works. But having 2 working solutions is better than 1, right? :slight_smile:

The page’s numbering is none by default, so you need to either set it to something else via

#set page(numbering: "1")

or explicitly handle that case in the pageref function.

2 Likes

Hey @jsx97, I’ve updated your post title to better fit our guidelines for question posts: How to post in the Questions category

Make sure your post title is a question you’d ask to a friend about Typst. :wink:

1 Like