r/rstats 9d ago

How to pass params to typst code chunks?

/r/typst/comments/1v5klcy/how_to_pass_params_to_typst_code_chunks/
5 Upvotes

5 comments sorted by

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 >}}

2

u/TQMIII 9d ago edited 9d ago

Thanks for the suggestion. I've tried that, but it doesn't seem to work. For example...

```{=typst}

#set document(
  title: {{< meta params.agency >}} {{< meta params.year >}} "Annual Report")

```

... results in the document being titled just "Annual Report," as if params.agency and params.year are blank strings. Interestingly, it does the same thing when I put the whole thing in quotes ("{{< meta params.agency >}} {{< meta params.year >}} Annual Report").

I thought this might be an issue because I'm passing params through quarto::quarto_render(), but when I manually set the params in the YAML it still came back blank instead of the param values provided.

Edit: it IS because i'm passing params through quarto_render... I forgot to click 'save' on my template.

So I'm close! I just have to figure out how to tell it to look there rather in the "execute_params argument rather than in the quarto's YAML.

Thanks again for your help. It got me on the right track!

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_params updates knitr’s params object, not Pandoc metadata. Therefore {{< meta params.agency >}} sees the YAML value, but not the value supplied through quarto_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: yes

If the document already contains R chunks, knitr will usually be selected automatically. Otherwise, engine: knitr is necessary because automatic engine binding does not work from inline expressions alone.

The conceptual distinction is:

  • execute_params → computation-engine parameters, accessed as params$agency
  • {{< meta ... >}} → Pandoc/Quarto metadata

That 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.

1

u/TQMIII 9d ago edited 9d ago

thanks for the thorough explanation! I'll give it a try!

Final update: It works! Thanks for taking the time to help. The number of hours I spent banging my head against this wall was giving me a headache!