gadenbuie / xaringanthemer

😎 Give your xaringan slides some style

Home Page:https://pkg.garrickadenbuie.com/xaringanthemer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Weird clash with xaringanthemer and custom graphics fonts in Rmd

mikedecr opened this issue · comments

Hi there, I have been having a graphics font issue that initially arose in a non-xaringan context, but I've nonetheless traced the cause back to something with xaringanthemer. I don't know much about the specifics, but I can describe what I know and provide a MWE that shows the problem at least on my machine (MacOS Catalina). Here's what I know:

  • An Rmarkdown project that I'm working on uses ggplot graphics with custom fonts. Ever since upgrading to R 4.0 (maybe relevant?), those fonts stopped appearing in compiled Rmd documents. Fonts would still work in interactive R sessions and if I saved graphics from an interactive session using ggsave().
  • Compiled Rmd documents would print this warning around these failed graphics: ## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font ## family 'Source Sans Pro' not found, will use 'sans' instead. (This happens with any font, not just Source Sans Pro.)
  • After some debugging, I realized the issue was arising because I was calling xaringanthemer::lighten_color(). I never fully attached the package, only called a function from it. If I delete any reference to xaringanthemer, all fonts in the compiled Rmd work correctly.

I created a minimal Rmd that reproduces the problem on my machine found here. Something diagnostically interesting is the fact that the problem doesn't arise in the same chunk where xaringanthemer is invoked, even if I create a graphic after invoking the package. The problem only arises in subsequent chunks, which the MWE highlights. This feels consistent w/ the finding that interactive sessions still work without a hitch for me.

Although I came across this problem in a non-Xaringan Rmd document, Xaringan slideshows have the same issue for me as well.
I'm concerned this is something idiosyncratic about my machine or font library or R package library (and might have causes that are deeper down than xaringanthemer itself), since customizing fonts to be consistent w/ a Xaringan theme seems like a pretty common for users of this package to want to do, and I'm not finding any info elsewhere about people having similar problems as me. If that's the case, I'm sorry to bug you about it, and maybe I just need to reconfigure my R setup from scratch or something. Thanks for the help and for such a cool package!

Thanks for the detailed report! Looks like this could be tricky to debug. It might be helpful to add a chunk to the reprex Rmd with

devtools::session_info()

then render the Rmd and copy the session info output into this issue.

Oh wait, @mikedecr, thanks to your detailed report I know exactly what the problem is.

Something diagnostically interesting is the fact that the problem doesn't arise in the same chunk where xaringanthemer is invoked, even if I create a graphic after invoking the package. The problem only arises in subsequent chunks, which the MWE highlights.

The theme matching code requires showtext to work correctly and to work correctly in R Markdown documents, showtext requires the knitr chunk argument fig.showtext be set to TRUE. Without that setting, showtext just doesn't work. It's a hard to diagnose (but known and documented) problem that's hard to figure out when you're using showtext directly, let alone using a package that wraps showtext.

For the sake of my users, I thought it would be easier to just set this option for them on package load. That's why it affects subsequent chunks.

I'll have to think about how to best handle this, but in the meantime here are a few options:

  1. If all you need is lighten_color(), feel free to copy the code into your project.

    xaringanthemer/R/color.R

    Lines 23 to 28 in e5ecb0e

    lighten_color <- function(color_hex, strength = 0.7) {
    stopifnot(strength >= 0 && strength <= 1)
    color_rgb <- col2rgb(color_hex)[, 1]
    color_rgb <- (1 - strength) * color_rgb + strength * 255
    rgb(color_rgb[1], color_rgb[2], color_rgb[3], maxColorValue = 255)
    }
    Better yet, colorspace::lighten() might be a better fit.

  2. Reset the chunk option. Add knitr::opts_chunk$set(fig.showtext = FALSE) after the call to lighten_color().

Hope this helps and thanks again for all the detail!

While not completely resolving the issue, you can now keep xaringanthemer from setting the fig.showtext chunk option by setting it globally to FALSE or by setting fig.showtext = FALSE in the chunk where xaringanthemer is attached.

```{r setup, include=FALSE}
# Either this option
knitr::opts_chunk$set(fig.showtext = FALSE)
```

```{r fig.showtext=FALSE}
# or this option
xaringanthemer:lighten_color("#123abc")
```