Skip to content

typst eval subcommand #7344

Closed
Closed

Description

@laurmaedje

Description

A typst eval subcommand that evaluates a piece of Typst code and outputs the serialized results. Can be done in the context of a document to introspect said document.

The CLI design is to be determined, but one possible argument surface I could imagine is the following:

  • A string of Typst code (required).
  • --in file.typ: A file in whose context the code should be executed. This generalizes the typst query subcommand as it allows using the introspection functions to observe the document.
  • --mode=code|markup|math: While not extremely useful, it nicely mirrors the argument on the eval function and it might come in handy.
  • --scope x=hello: Also mirrors the argument on the eval function. Could be used for safe argument interpolation.
  • --format=json|yaml: The serialization format for the output (like for the existing typst query).
  • --pretty: Whether to pretty-print the serialized output.
  • Flags that affect the compilation itself (like for typst query).

Benefits over typst query:

  • Much more general: Many things that the current query subcommand does not support are trivial with the eval subcommand (e.g. running a transformation over the query).
  • Knowledge about Typst scripting directly translates to typst eval. No need to look up special flags like --field and --one.
  • Extends to use cases beyond querying.

I think these benefits are sufficiently strong to warrant deprecating typst query in favor of typst eval.

Use Case

Query-like use cases:

# Basic case
typst query main.typ raw
typst eval --in main.typ query(raw)
# => [{"func":"raw","lang":"typc", ...}]

# Extracting a field
typst query main.typ raw --one --field lang
typst eval --in main.typ query(raw).first().lang
# => "typ"

Query-like use cases that the current typst query is not capable of:

# Extracting the page number of the bibliography
typst eval --in main.typ locate(bibliography).page()
# => 27

# Extracting the page/x/y positions of all raw blocks
typst eval --in main.typ "query(raw).map(it => it.location().position())"
# => [{"page":1,"x":"10pt","y":"10pt"},{"page":2,"x":"10pt","y":"80pt"}]

Other fun examples:

# Calculations
typst eval 1+2
# => 3

# Testing how some Typst code behaves from the command line
typst eval '"âbc".clusters()'
# => ["â", "b", "c"]

# Taking input from stdin
echo "1+2" | typst eval -
# => 3

# Using a scope
typst eval "x+2" --scope x=$(echo 1)

Notes

Would resolve #7008

Activity

  1. added
    feature request
    scripting
    cli
    change request
    on Nov 9, 2025
  2. TimerErTim commented on Nov 10, 2025

    @TimerErTim
    Contributor

    I got a working prototype version of this locally. I am struggling however to come up with a design for correctly providing a context for context dependant functions like query or measure.

  3. laurmaedje commented on Nov 11, 2025

    @laurmaedje
    MemberAuthor

    Location context does not make sense since typst eval happens at no particular location in the document (e.g. here() makes no sense). That said, for introspection to work, we need at least some piece of context. For style context, we could just provide the default styles from the Library there. I'm not 100% sure it makes sense, but it might and to get things going it would be a good start.

    If we decide that it doesn't make sense to provide style context, we'd need to adjust the Context struct a bit to have an extra bit for allowing introspection without having either of location or style context.

  4. TimerErTim commented on Nov 12, 2025

    @TimerErTim
    Contributor

    Just for your interest, I got basically all of the cli surface working, including a World with empty main file, scope parsing etc.
    I have not yet managed to get it to evaluate context dependant operations as if we were inside a context. It should not take long anymore though (most dreaded statement of SE but hey).

  5. TimerErTim commented on Nov 12, 2025

    @TimerErTim
    Contributor

    Got it working, behaves as expected. Contextual expressions not requiring location information (such as query, locate, counter(...).final() work flawlessly. Those who need it (counter(...).display(), measure([...])) return the known

    error: can only be used when context is known
     = hint: try wrapping this in a `context` expression
     = hint: the `context` expression should wrap everything that depends on this function
    

    error message. I will now cleanup the code and then open a PR.

  6. ChadNedzlek commented on Oct 1, 2026

    @ChadNedzlek

    I can't figure out how to get the page count with this thing. WITHOUT "context", it reports "context is known" and with it, it just retunrs {"func":"context"}

  7. TimerErTim commented on Oct 2, 2026

    @TimerErTim
    Contributor

    Yes it's correct that with context only {"func":"context"} is returned. There is no location we can assume with the eval command in order to resolve the contextual function. However, I believe counter(page).final() should be non-contextual, since it really is not dependant on its location context. The return value is the same, regardless of where in the document you invoke it.

    However, that only returns the logical page count anyway, which might differ from the actual page count in PDFs. For that, you could compile the Typst file to PDF and pipe that directly to a PDF CLI tool like pdf-cli.

    @laurmaedje, is my observation correct?

  8. laurmaedje commented on Oct 2, 2026

    @laurmaedje
    MemberAuthor

    @TimerErTim I responded in the issue #8918 (comment)

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

    change requestclifeature requestscripting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions