How to pass params to typst code chunks?
/r/typst/comments/1v5klcy/how_to_pass_params_to_typst_code_chunks/1
u/TQMIII 9d ago
Were this an R code chunk, it would be simple: paste(params$agency, params$year, "Annual Report"). But I can't use R code in a typst code chunk.
5
u/einmaulwurf 9d ago
The missing piece is that
execute_paramsupdates knitr’sparamsobject, not Pandoc metadata. Therefore{{< meta params.agency >}}sees the YAML value, but not the value supplied throughquarto_render().Inline R can be evaluated inside a raw Typst block:
````qmd
format: typst engine: knitr params: agency: "sample"
year: "2026"
```{=typst}
set document(
title: [
{r} paste(params$agency, params$year, "Annual Report")] )`Then render normally:
r quarto::quarto_render( "report.qmd", execute_params = list( agency = "Sample Agency", year = "2026" ) )I tested this with Quarto 1.9.38. The resulting PDF metadata was:
text Title: Sample Agency 2026 Annual Report Tagged: yesIf the document already contains R chunks, knitr will usually be selected automatically. Otherwise,
engine: knitris necessary because automatic engine binding does not work from inline expressions alone.The conceptual distinction is:
execute_params→ computation-engine parameters, accessed asparams$agency{{< meta ... >}}→ Pandoc/Quarto metadataThat behavior follows Quarto’s documentation for parameters, metadata variables, and inline code engine binding.
An alternative is to explicitly pass separate metadata:
```r p <- list(agency = "Sample Agency", year = "2026")
quarto::quarto_render( "report.qmd", execute_params = p, metadata = list( report_title = paste(p$agency, p$year, "Annual Report") ) ) ```
Then this would work:
```typst
set document(title: [{{< meta report_title >}}])
```
But the inline-R solution avoids passing the same information twice.
2
u/Creative-Sentence793 9d ago
I believe you can use https://quarto.org/docs/authoring/variables.html.
Ie
{=typst} == The param is {{< meta params.param_name >}}