3

I want to define the fonts to use in my RMarkdown PDF document. I’d like to use a Google Font, say, Roboto, but I do not have admin privileges to install it directly. Nevertheless, I think that I can work around these privileges, as I’ve installed many R packages that come parcelled with their own fonts (e.g., tint).

Normally, to set a custom font for the markdown document I’d do something like this in my YAML:

---
title: "Title"
output:
  pdf_document:
    latex_engine: xelatex
mainfont: Calibri
---

This works if the font is installed, but, as I say, I cannot install fonts. I can use Google Fonts through the showtext package, like in my example below, but this is only for figures:

---
title: "Title"
output:
  pdf_document
---

# Header One  

## Header Two
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

```{r fonts, message=FALSE}
library(showtext)
font_add_google("Lobster", "lobster")
```

```{r fig.showtext=TRUE, fig.align='center'}
plot(1, pch = 16, cex = 3)
text(1, 1.1, "A fancy dot", family = "lobster", col = "steelblue", cex = 3)
```

enter image description here

As you can see, I use the Lobster font in my figure, which is great. I’d like to extend this example so that the Lobster font is used to the main body of the document, but doesn’t need to be installed directly. Is this possible?

Dan
  • 11,370
  • 4
  • 43
  • 68
  • 2
    I'm pretty sure there are 2 different mechanisms happening: one is the text being turned into pixels in an image with `showtext` and `plot`, and one is rendering to Latex, which is a whole different beast. Does the output have to be a PDF? It should be possible to change the font if you render to HTML by changing the CSS. One workaround might be creating an HTML page with your custom font, then converting that to PDF – camille Mar 25 '19 at 15:37
  • @camille Thanks for the insight, camille. Yes, unfortunately it has to be PDF. That's a good workaround (i.e., HTML then PDF), but I hoped there was a standard way of doing this. Do you have any idea how a package like `tint` installs a font, as I'm pretty sure that's used as an actual font rather than an image in `showtext`? – Dan Mar 25 '19 at 15:41
  • 1
    I haven't used `tint`, but it looks like it installs some tex packages for additional fonts...? https://github.com/eddelbuettel/tint What I meant about images is that `showtext` is giving you access to a font to use in an image, vs text rendered in a document like a PDF that can be highlighted, edited, etc – camille Mar 25 '19 at 15:56
  • 1
    As a test, I just knit one of the HTML examples from `hrbrthemes`, which has custom fonts set in its CSS (haven't looked at it to see exactly what the styles are). Opened the HTML in Firefox, went to print, and printed to a PDF. It looks nice and has the same fonts. That's pretty simple to do because I'm on a Mac. Not a full solution since I don't know how that would work on a different OS, or exactly how the fonts are embedded in the PDF. – camille Mar 25 '19 at 16:06
  • @camille Thanks for testing it. I think I have an alternative solution (or at least partial solution) thanks to your comments above. I'll post it below. – Dan Mar 25 '19 at 16:08

1 Answers1

6

As @camille mentioned above, tint installs TeX packages to use fonts. (There's a pretty extensive list of font packages here, including Google Fonts like Roboto.) If I wanted to use Lobster for the main body, I could include the appropriate TeX file in the header like this and it's downloaded and installed automatically upon knitting:

---
title: "Title"
output:
  pdf_document
header-includes:
  - \usepackage{LobsterTwo}
---

# Header One  

## Header Two
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

enter image description here

Not a perfect solution, as it relies on the existence of a TeX package for that font, but still...

Dan
  • 11,370
  • 4
  • 43
  • 68
  • And that works with not having admin permissions on your computer? – camille Mar 25 '19 at 16:27
  • @camille Yes, but I won't pretend to know why, though. Perhaps fonts used by LaTeX are kept in a different directory (which I *do* have permissions for) than system fonts? – Dan Mar 25 '19 at 16:30