I'm looking for a general way to automatically match the font of a ggplot with the font used by the html's theme. In the code below, I'd like to detect that the 'yeti' theme uses the 'Open Sans' font for body text. Similarly, if the theme was 'united', it should be detected that the font is 'Ubuntu'.
---
title: "Untitled"
output:
html_document:
theme: yeti
date: "2022-09-14"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(dev = "ragg_png") # so that fonts render effortlessly
```
```{r plot}
library(ggplot2)
font <- magic_function_to_retrieve_current_font()
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme_gray(base_family = font)
```
In the code above, I'd like to know a function, here called magic_function_to_retrieve_current_font(), that does the following things:
- It detects that the document is being knit with the 'yeti' theme.
- It finds the theme's style sheet
- It looks up the CSS code for the body text in the style sheet
- From the body text properties, it extracts the 'font-family' property (and returns the first entry)
Now (3) and (4) I may figure out myself, but I'm having difficulty with steps (1) and (2). Is there some magic knitr/rmarkdown/quarto functions I could use to have access to the YAML/theme style sheet while knitting the document?