For example, some documents may be supposed to have the file name in the footer. Other documents may want to include all chapters or other contents that are defined as *.typ files in a specific directory.
Can one write something like this?
#set page(
footer: [#sys.filename],
)
#for chapter in sys.list-files("chapters/*.typ").sorted() {
include chapter
}
These specific commands don’t work, but is there something equivalent?
There is Directory walking · Issue #2123 · typst/typst · GitHub for reading/walking directories, it seems that this is delayed until the path type is added, which ties into resources (fonts, images, supplementary files) as a whole.
I couldn’t find anything regarding retrieving the file name, but I remember it being requested and closed as not planned I believe.
It’s possible to work around the directory walking case by passing the directories to Typst using sys.inputs. But this is currently limited to the CLI and will not respond to any changes on typst watch. Assuming a sh compatible shell:
#for chapter in json.decode(sys.inputs.chapter) {
include chapter
}
I’ve chosen json as the intermediate format here, as it’s supported as an output format by many CLI tools. One could use a script to automatically pass the right files to inputs automatically.
For example in nu this would look like:
let chapters = (ls | get name | to json)
typst compile --input $"chapters=(chapters)" main.typ main.pdf
In addition to what @Tinger wrote, one consideration is that Typst tries to not depend on one specific execution environment: the current file name or directory will exist when compiling via the command line, but not (or in the same way) in the web app, for example. A document that depends on this would not be as “portable”.
Using --input requires a bit more effort, but the sys.inputs dictionary is accessible no matter the environment, and can be filled with a path even if the environment has no input file path at all.
Any future improvement in this area will make sure that it applies not just to one of Typst’s execution environments.