There has been extensive discussion about this in the past, but this functionality (often referred to as “shell escape” in reference to LaTeX’s equivalent functionality) unfortunately isn’t really compatible with several of Typst’s aspects, including portability (being able to compile the same document in different environments), purity (function calls in Typst must always produce the same result to make memoization possible), security (compiling a Typst document must not produce unexpected side-effects in your system) and others.
However, you can still run non-Typst code within a Typst document through the usage of WASM plugins, which are portable. This allows you to run anything that can compile to WASM, including even languages like Python (pyrunner – Typst Universe) or JavaScript (jogs – Typst Universe). As a fun fact, this also made it possible to bring some tools from the JS ecosystem to Typst, such as charts with echarts
: echarm – Typst Universe
For more information on how to create a plugin (if you’re a developer), you can check out this page: Plugin Type – Typst Documentation
Now, if you really do need to run an external command (because, say, your program can’t easily be compiled to WebAssembly), it will be necessary to run the command before compiling and using its output. However, you can make this process easier, and even determine which command to run from inside the Typst document, thanks to typst query
, which allows fetching information from the document. Check out this package which may help: prequery – Typst Universe