You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CLI "eval" command can't do anything useful with contexts, no way to get page count, for example #8918
typst eval --in doc.typ"counter(page).final()"
error: can only be used when context is known
┌─ <input-expression>:1:0
│
1 │ counter(page).final()
│ ^^^^^^^^^^^^^^^^^^^^^
│
= hint: try wrapping this in a `context` expression
= hint: the `context` expression should wrap everything that depends on this function
I can't find any way to get anything useful out of the "eval" command. It either demands a context... or returns the context. Neither of which is terribly informative. Maybe I'm just holding it wrong?
The error message is quite uninstructive. The thing is that typst eval only provides style context, not location context because it's not at a specific location in the document.
Conceptually, final might seem like it does not need to know where it is called. And before 0.15 that was true and final only required any kind of context (either style or location). It must at least be somehow contextual because if you just call it top-level in document outside of any context, the introspector will not even be populated. However, style context was sufficient to give that guarantee.
With 0.15 and bundle export, I changed it to require location context because it will resolve the final page count in the document that it's in. For example, here the two give different results:
I did not anticipate the unfortunate interaction with typst eval.
Maybe it would be possible to fix this by checking whether we are in bundle mode and only then requiring the location. Not sure whether that would run into any issues.
I see. What about adding a labelled metadata(...) <...> at the end of the document and requesting counter(page).at(query(label(...)).first().location()) or something along the line?
I am currently not at home to test this. Maybe someone can upfront bring up concerns.
Activity
laurmaedje commentedon Oct 2, 2026
The error message is quite uninstructive. The thing is that
typst evalonly provides style context, not location context because it's not at a specific location in the document.Conceptually,
finalmight seem like it does not need to know where it is called. And before 0.15 that was true andfinalonly required any kind of context (either style or location). It must at least be somehow contextual because if you just call it top-level in document outside of any context, the introspector will not even be populated. However, style context was sufficient to give that guarantee.With 0.15 and bundle export, I changed it to require location context because it will resolve the final page count in the document that it's in. For example, here the two give different results:
I did not anticipate the unfortunate interaction with
typst eval.Maybe it would be possible to fix this by checking whether we are in bundle mode and only then requiring the location. Not sure whether that would run into any issues.
TimerErTim commentedon Oct 2, 2026
I see. What about adding a labelled
metadata(...) <...>at the end of the document and requestingcounter(page).at(query(label(...)).first().location())or something along the line?I am currently not at home to test this. Maybe someone can upfront bring up concerns.
TimerErTim commentedon Oct 6, 2026
I found a pretty straightforward workaround/solution:
Prepare the Typst document
Add a
metadatafield at the end:Or more foolproof, at start of document:
Query the page counter at that location
In the CLI: