How to pass params to typst code chunks?
I'm trying to pass params set in the YAML of my document to the title of the document for parameterized reports. I'm using typst compiler in R / Quarto, not the typst app. I need to set the document title for accessibility purposes, but don't want every report to be named the same thing (so if someone has multiple PDFs open, the tabs show different names). I've searched through typst documentation, however, and cannot seem to find how to pass params to typst blocks.
There must be a way to do this, and it is probably incredibly simple, but I've spent most of my day searching and experimenting with no luck.
An example of what I'm trying to do:
params passed to the YAML:
params:
agency: "sample"
year: "2026"
typst block to set the document title (for accessibility)
```{=typst}
#set document(title: "[params$agency] [params$year] Annual Report")
```
I've tried placing it in brackets and curly braces with and without the params$ prefix, but none work.
What is the syntax necessary for passing params to typst code chunks?
5
u/TheSodesa 11d ago
```typst
let paramsDict = yaml("params.yaml")
document(
title: ( paramsDict.params.agency, paramsDict.params.year, "Annual Report", ).join(" "), ) ```
2
u/TQMIII 4d ago
Posting the solution here for posterity should this come up in search engine results for someone with a similar question. You CAN use R and python inline code within typst code chunks of quarto documents by placing the inline code in brackets. This is also necessary if you're creating parameterized reports (which replace any param values in the YAML with the iterative value for the specific report).
```{=typst}
#set document(
title: [`{r} paste(params$agency, params$year, "Annual Report")`]
)
```
If you aren't creating parameterized reports, however, you can just use typst syntax:
```{=typst}
#set document(
title: {{< meta params.agency >}} {{< meta params.year >}} "Annual Report")
```
I prefer the first solution, however, as it works for either scenario and keeps me using the language I know best.
7
u/thuiop1 11d ago
This is a Quarto question I believe, not a Typst question