3

If I type

par(family='CM Roman')

then I can get a plot to display with the Computer Modern font. However, when I try to save it as a PDF, it comes up as a blank document, unlike if I take out that line.

How can I get it to save correctly as a PDF?

Also, how can I use Latin Modern instead of Computer Modern?

rhombidodecahedron
  • 7,693
  • 11
  • 58
  • 91

2 Answers2

5

This is how I did it in Windows:

  1. Install the extrafont package.
  2. Install Latin Modern fonts, e.g. from http://www.fontsquirrel.com/fonts/latin-modern-roman. Watch out, you need to install the TTF version of the font, font_import() can't handle OTF.
  3. Import the fonts using font_import().
  4. Load the fonts using loadfonts(device = "win"). Use the device = "Win" parameter to make the preview in R Studio work.
  5. Set the font family graphics parameter using par(family = "LM Roman 10").
  6. Plotting in R Studio now works and so does pdf export (see the pictures below).

This is the full code you need to use:

# Run once
install.packages("extrafont")
library(extrafont)
# Install **TTF** Latin Modern Roman fonts from www.fontsquirrel.com/fonts/latin-modern-roman
# Import the newly installed LModern fonts, change the pattern according to the 
# filename of the lmodern ttf files in your fonts folder
font_import(pattern = "lmroman*")


# Run each time
library(extrafont)
loadfonts(device = "win")
par(family = "LM Roman 10")
x <- seq(1, 10, 1)
y <- seq(1, 10, 1)
plot(y ~ x, main="This plot uses LaTeX font!", ylab = expression(alpha))

R Studio preview:
R Studio preview

Exported pdf:
PDF export

Augustin
  • 2,444
  • 23
  • 24
  • 1
    How would this be different in a Mac computer? – Javier Sep 03 '20 at 03:25
  • 1
    I think it should be essentially the same process. `extrafont` works on Mac as well and you can install Latin Modern easily on Mac (download the TTF, open it and click the install button). You will probably need to change the arguments to `loadfonts`. – Augustin Sep 08 '20 at 11:50
-1

I followed the steps mentioned by @Augustin and received following error message for all fonts I wanted to import:

Scanning ttf files in my/path/to/fonts ...   
Extracting .afm files from .ttf files...
my/path/to/fonts/lmroman10-bold-webfont.ttf : No FontName. Skipping.

This error could be traced back to the package Rttf2pt1 version 1.3.9 (see here).

So I downgraded Rttf2pt1 to version 1.3.8:

package_url <- "https://cran.r-project.org/src/contrib/Archive/Rttf2pt1/Rttf2pt1_1.3.8.tar.gz"
install.packages(package_url, repos=NULL, type="source")

Afterwards, following the steps from @Augustin worked fine.

bt-koch
  • 59
  • 5