Reference parameter in figure caption with Quarto

Question:

Is there a way to reference a parameter in a Quarto figure or table caption? In the example below, I am able to reference my input parameter txt in a regular text block, but not in a figure caption. In the figure caption, only the raw text is displayed:

---
title: "example"
format: html
params: 
  txt: "example"
---

## I can reference it in text
Here I can reference an input parameter: `r params$txt`

```{r}
#| label: fig-example
#| fig-cap: "Example: I cannot reference a parameter using `r params$txt` or params$txt." 
plot(1:10, 1:10)
```
Asked By: ColinB

||

Answers:

Try with !expr

   ```{r}
   #| label: fig-example
   #| fig-cap: !expr params$txt
   plot(1:10, 1:10)
    ```

-output

enter image description here


If we need to add some text, either paste or use glue

#| fig-cap: !expr glue::glue("This should be {params$txt}")
Answered By: akrun
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.