Using tikzDevice with Quarto documents

R
CRAN
package
LaTeX
Quarto
Author

Ralf Stubner

Published

August 4, 2024

Recently Esther posted about something that is dear to my heart. Keeping fonts in sync between text and graphics:

When I suggested tikzDevice as an alternative method, I realised that there is no good documentation for using it in a Quarto document. Some time ago I had written a blog post showing usage with R Markdown, but that is meanwhile outdated and only accessible via the wayback machine.

So how does one use tikzDevice together with Quarto? First you should make it the default graphics device by adding

knitr:
  opts_chunk: 
    dev: tikz

to the YAML header of the document. Alternatively, you could include

if (knitr::is_latex_output())
  knitr::opts_chunk$set(dev = "tikz")

in a R code chunk. The latter approach would also work with R Markdown or if you would create other output formats besides PDF from the same source code. However, I really like the clean look when options like this are defined in the header instead of a code chunk.

Next you might have to tell tikzDevice which fonts you are using, e.g.:

library(tikzDevice)
options(tikzLatexPackages = c(
  getOption("tikzLatexPackages"),
  "\\usepackage[osf]{mathpazo}",
  "\\usepackage[T1]{fontenc}",
  "\\usepackage{textcomp}"
))

With that you can create documents like this:

See the download links in the right-hand column for the source and ouput files.