How to get the page number of a marker

This is similar to this post.

I want to merge one external pdf into a typst-generated one, with something like

pdftk A=a1.pdf B=b1.pdf cat A1-3 B1-3 A4-end output combined.pdf

In a dynamic document, I do not know the page number where to insert the pdf, so my idea was to get the page number from some <insert_here> marker. From the previous post, I learned that writing from typst is not possible (fair enough). Is there any alternative other than using some search into the pdf to find the correct location?

I gave this advice for a similar issue on Discord recently:

Basically, you’d insert a page number as metadata into your document (edited to reflect feedback):

#context [#metadata(here().page())<insert_here>]

Then, query that with

typst query --one --field value '<insert_here>'

and insert the value you got into your pdftk command

2 Likes

I had tried with pdftk, pdfinfo and awk, but your’s is much simpler.

1 Like

… but I failed to get it to work.

=== Case report form (CRF)
#context metadata(here().page()) <crfhere>

=== Participant informed consent
#metadata("This is a note") <note>

Test

typst query index.typ "<crfhere>" --field value
[]
typst query index.typ "<note>" --field value 
["This is a note"]

whoops, I think you need brackets (edited to reflect Andrew’s feedback):

#context [#metadata(here().page())<crfhere>]

because otherwise, the label is attached to the context instead of the metadata

I’ll edit my original answer to reflect that

2 Likes

With inline content, this might introduce a space. Solution:

#context [#metadata(here().page())<crfhere>]

See fix(layout): removed leading space from prequery() by Andrew15-5 · Pull Request #3 · typst-community/prequery · GitHub.

1 Like