How do I make a reference that inserts name of section, and page number?

I’m using typst to reference manuscripts that are added at the end of the document. I’ve got pages with a heading and a label, and also the page number is set manually to match.

I would like a way to reference the label of that Manuscript heading, in which typst prints out “Manuscript IV (p. 56)” or something like that.
The heading would look like

= Manuscript IV
<paper4>

Here is a possible solution:

#show ref: it => if it.element.func() != heading { it } else {
  let l = it.target // label
  let h = it.element // heading
  link(l, [#h.body (p. #h.location().page())])
}

A reference to @paper4

#lorem(100)

// #pagebreak() // This will change "(p. 1)" to "(p. 2)".

= Manuscript IV
<paper4>

#lorem(20)

image

If you have the heading on the 2nd page:image

2 Likes

Is it possible to do #show ref.where(it=>it.element.func() == heading) or something like that?

Yeah, I don’t think so. I did want to use .where() clause but ref() has only target and supplement fields: Reference Function – Typst Documentation, although it didn’t give an error when .where(element) is used. Unfortunately, element is of type content (or sometimes none, I think), so using it inside of .where() is basically useless. And there is no feature that adds lambda function inside .where() clause. Perhaps you can open an issue in the repository explaining the desired feature. It would be cool if you can filter ref occurrences (via .where()) by element type of ref.element field.

1 Like

It’s not possible, for a few reasons outlined in this Github issue (and linked Discord thread):