extrafont
extrafont copied to clipboard
R 4.4.0 breaks some user-installed fonts in PDFs
Hi
I have found that upgrading from R 4.3.3 to R 4.4.0 causes certain fonts to no longer display properly in PDFs generated by converting R markdown code to PDF to using knitr. I have replicated this problem on Windows 10 and 11 machines.
Here's a working example:
A) Download and install the Open Sans collection of fonts from here:
https://fonts.google.com/specimen/Open+Sans
There are 36 ttf files in the collection.
B ) R markdown code
---
output:
pdf_document:
latex_engine: xelatex
dev: "cairo_pdf"
---
```{r}
library(extrafont)
extrafont::font_import()
library(ggplot2)
# Example plot
p <- ggplot(mtcars, aes(x=mpg, y = cyl)) +
geom_point() +
labs(title = "Main title test", subtitle = "Subtitle testing font")
# Use Open Sans Semi Condensed Light Font
p + theme_bw(base_family = "Open Sans SemiCondensed Light")
Figure Under R 4.3.3
When I Knit that code to PDF in R Studio, with the version of R set to R-4.3.3 under my Global Settings, the resulting image in the PDF looks like this
The font in the title / subtitle / axis labels is correct (Open Sans SemiCondensed Light).
Figure Under R 4.4.0
Running exactly the same code with the version of R set to 4.4.0 in R studio produces the following plot:
The font under 4.4.0 is the default sans serif for Windows (Arial), rather than Open Sans SemiCondensed Light.
Session Info
Other Info
- I have replicated this problem on machines running Windows 10 and 11.
- I have also tried kniting the R markdown file to PDF using the R 4.4.1 release candidate, and latest development build. Both produce a plot identical to that generated under R 4.4.0 (ie with Arial as the font, rather than Open Sans SemiCondensed Light).
- All the above plots were generated using knitr 1.46. Upgrading to 1.47 makes no difference under R 4.4.0 (and throws errors under R 4.3.3).
- The problem seems specific to fonts installed manually by the user on their Windows machine. Fonts that ship with Windows don't seem to exhibit the problem. For example, the font 'Calibri' that ships with Windows looks like this under both R 4.3.3 and R 4.4.0:
p + theme_bw(base_family = "Calibri")
In contrast, a different font I installed on the system myself (Econ Sans Condensed Bold Italic) looks like this under R 4.3.3:
p + theme_bw(base_family = "EconSansCndBolIta")
... but under R 4.4.0 the font face defaults back to Arial.
Interestingly, if the font is set to "Opens Sans", then the graph looks like this under both R 4.3.3 and R 4.4.0
p + theme_bw(base_family = "Open Sans")
So, even though I installed "Open Sans" myself graphs generated using that font look fine under both R 4.3.3 and R 4.4.0, whereas other fonts I installed myself ("Open Sans SemiCondensed Light" and "EconSansCndBolIta") work fine under 4.3.3 but fail under 4.4.0.