Can I configure my document (e.g. draft/release version, color theme) when creating a PDF without modifying the Typst file directly?

There absolutely is: the sys.inputs dictionary contains values that have been set from the command line, via the --input key=value syntax. To my knowledge, the web app currently doesn’t have a way to put values into that dictionary, so be aware of that.

It’s often useful to have default values for these parameters, and check whether values are valid. Let’s say you want to assume --input theme=light and --input solutions=false, you could process the sys.inputs dictionary like this:

let theme = {
  let valid-values = ("light", "dark")
  let value = sys.inputs.at("theme", default: "light")
  assert(value in valid-values, message: "`--input theme` must be light or dark")
  value
}

let solutions = {
  let valid-values = ("true": true, "false": false)
  let value = sys.inputs.at("solutions", default: "false")
  assert(value in valid-values, message: "`--input solutions` must be true or false")
  valid-values.at(value)
}

Using this code, theme will be a string value (default "light") and solutions will be a boolean value (default false).

For more complex data input needs, using one of the data loading functions is probably the best idea:

  • directly in the command line: --input 'data={"some-num": 1, "some-bool": true}'
    then read via json.decode(sys.inputs.data)
  • in a separate file: --input 'file=data.json'
    then read via json(sys.inputs.file)
    For this to work, the file must be accessible to Typst, i.e. it must be in the Typst root directory. By default, that is the directory where the main Typst file you’re processing is located.

(JSON is of course just one option for a data format that can be read like this.)

6 Likes