Today I was writing a piece of documentation in Quarto, and I wanted to define a variable for the version number somewhere central and invoke it in the text. I didn’t know that you can define any custom variable in the Quarto YAML so long as they are not one of the standard Quarto ones.
---
title: My document
format: typst
magic-number: 17
---Then you just invoke it with this piece of Quarto syntax:
The magic number is {{< meta magic-number >}}.It works in R code chunks too via the rmarkdown package:
```{r}
magic <- rmarkdown::metadata$`magic-number`
print(magic)
2 * magic
```In Python chunks it works for display, but not for evaluation:
```{python}
magic = "{{< meta magic-number >}}"
print(magic)
print(f"The number is: {magic}")
if magic == 17:
print("matched")
else:
print("NOT matched")
try:
magic = int(magic)
except:
print("Doesn't work")
```