Repository navigation
Closed
Labels
change requestA proposal for changes to an existing featurecliAbout Typst's command line interface.feature requestNew feature or requestscriptingAbout Typst's coding capabilities
Description
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 thetypst querysubcommand 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 theevalfunction and it might come in handy.--scope x=hello: Also mirrors the argument on theevalfunction. Could be used for safe argument interpolation.--format=json|yaml: The serialization format for the output (like for the existingtypst 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--fieldand--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
Reacted by Zaitara, Andreas Kröpelin, Noam Zaks, Carl Kuhlemann, Y.D.X., Philipp Niedermayer, Yves Parès, Michael Fortunato, Ludwig Austermann, Laurenz Stampfl and 3 more
Reacted by Joseph Alexander Wilson
Reacted by sitandr
Reacted by Liara, Zaitara and natri0
Metadata
Metadata
Assignees
Labels
change requestA proposal for changes to an existing featurecliAbout Typst's command line interface.feature requestNew feature or requestscriptingAbout Typst's coding capabilities
Activity
TimerErTim commentedon Nov 10, 2025
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
queryormeasure.laurmaedje commentedon Nov 11, 2025
Location context does not make sense since
typst evalhappens 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 theLibrarythere. 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
Contextstruct a bit to have an extra bit for allowing introspection without having either of location or style context.TimerErTim commentedon Nov 12, 2025
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).
TimerErTim commentedon Nov 12, 2025
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 knownerror message. I will now cleanup the code and then open a PR.
ChadNedzlek commentedon Oct 1, 2026
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"}TimerErTim commentedon Oct 2, 2026
Yes it's correct that with context only {"func":"context"} is returned. There is no location we can assume with the
evalcommand in order to resolve the contextual function. However, I believecounter(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?
laurmaedje commentedon Oct 2, 2026
@TimerErTim I responded in the issue #8918 (comment)