Skip to content

CLI "eval" command can't do anything useful with contexts, no way to get page count, for example #8918

Open

Description

@ChadNedzlek

Description

typst 0.15.1 (9dfd3a0, linux x86_64)

typst eval --in doc.typ "context counter(page).final()"
{"func":"context"}
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?

Operating system

linux x86_64

Typst version

typst 0.15.1 (9dfd3a0, linux x86_64)

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Oct 1, 2026
  2. added
    cli
    introspection
    on Oct 2, 2026
  3. laurmaedje commented on Oct 2, 2026

    @laurmaedje
    Member

    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:

    // typst c hi.typ -f bundle
    
    #document("a.pdf")[
      #context counter(page).final()
      #pagebreak()
      #pagebreak()
      #pagebreak()
    ]
    
    #document("b.pdf")[
      #context counter(page).final()
      #pagebreak()
    ]

    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.

  4. TimerErTim commented on Oct 2, 2026

    @TimerErTim
    Contributor

    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.

  5. TimerErTim commented on Oct 6, 2026

    @TimerErTim
    Contributor

    I found a pretty straightforward workaround/solution:

    Prepare the Typst document

    Add a metadata field at the end:

    #metadata([]) <document-end>

    Or more foolproof, at start of document:

    #show: doc => [
      #doc
      #metadata([]) <document-end>
    ]

    Query the page counter at that location

    In the CLI:

    typst eval --in your/document.typ 'counter(page).at(<document-end>).first()'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcliintrospection

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions