I am looking for at way to plot graphs in R, using LaTeX fonts.

In the documentation for pdfFonts in R, it states that:

There are also mappings for "ComputerModern", "ComputerModernItalic" and, as from R 3.1.0, "ArialMT" (Monotype Arial).

When using pdf(file = outputFile, width=11.692, height=8.267, family = "ComputerModern") I get the following error message:

Error in pdf(file = outputFile, width = 11.692, height = 8.267, family = "ComputerModern",  : 
  unknown family 'ComputerModern'
Execution halted

I am using R version 3.2.2 (2015-08-14). How can I use LaTeX fonts in R? Preferable without installing extra libraries.

  • 2
    Welcome to TeX.SX! I'm not sure that this is entirely on-topic for this site. It would probably be more appropriate on Stack Overflow. Nonetheless, there's a fontcm package that can be used with the extrafont package (see here). I've never had much luck getting that to work. Instead, I've had luck installing cm-unicode and using it as "CMU Serif" with extrafont.
    – Adam Liter
    Commented Dec 16, 2015 at 22:37
  • Thanks for the welcome. I tried your proposed solution, but ended up with probelms for my PDF Viewer. I have posted my solution below.
    – YnkDK
    Commented Dec 17, 2015 at 11:16
  • Did you also embed the fonts in your PDF? If they're not embedded in the PDF, then the viewer would just default to a certain font. extrafont provides the command embed_fonts() to allow you to do this, which is discussed in the link from my first comment. You can also use CairoPDF() from the Cairo package to embed fonts, which is something I've had more success with than using extrafont's embed_fonts(). tikzDevice is a good solution, too, though!
    – Adam Liter
    Commented Dec 17, 2015 at 17:49
  • tikzDevice and knitr tex.stackexchange.com/a/330486/11604
    – Fran
    Commented Oct 7, 2019 at 21:27
  • This does not work on R4.0 + Commented Jul 1, 2021 at 5:42

4 Answers 4


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 **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 = "lmodern*")

    # Run each time
    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

  • 4
    On fontsquirrel.com/fonts/latin-modern-roman I don't see an option to download TTF's?
    – MERose
    Commented Feb 9, 2018 at 4:26
  • me neither...has it been removed?
    – Manuel R
    Commented Oct 19, 2018 at 12:39
  • Yes, the GUST foundry stopped generating TTFs with version 1.010x in 2007. It might still be possible to compile them in FontForge from the sources.
    – Davislor
    Commented Dec 4, 2018 at 1:59
  • There is, however, a TTF distribution of Computer Modern Unicode.
    – Davislor
    Commented Dec 4, 2018 at 2:01
  • loadfonts(device='win') returns an error Commented Jul 1, 2021 at 5:41

The selected solution.

It seems to be imposible to do without installing an external library. I tried using extrafont, but then my default PDF Viewer did not have the font used.

I ended up using the library tikzDevice, which exposes the method/device tikz, acts exactly like the pdf device, but outputs TikZ code instead. This seems more powerful, since the font is then entirely dependent on my LaTeX setting and I can include arbitrary math symbols.

Below is the code I am using, which plots the running time of my algorithms with standard deviation error bars and writes the output to a .tex-file.

tikz(file = outputFile, width=11.692, height=8.267)
ggplot(tgc, aes(x=E, y=wall.time, colour=Algorithm)) + 
  geom_errorbar(aes(ymin=wall.time-se, ymax=wall.time+se), width=.1) +
  geom_point() +
  xlab("Number of edges (|E|)") +
  ylab("Wall time (secs)") +
  ggtitle(paste0("Running time with |V| = ", dt$X.V.[1])) +
endoffile <- dev.off() 
  • 3
    With options() you can change the tikzDefaultEngine to luatex (or xetex), for example, and then you can add lines to the preamble of the output file with getOption() and tikzLuatexPackages (or tikzXelatexPackages) that would allow you to specify the font you want with fontspec. See §2.1 of the tikzDevice manual.
    – Adam Liter
    Commented Dec 17, 2015 at 17:56

I also tried several times and found that one need to use device=cairo_pdf to save the pdf figure. It looks like that (by the way, I used ggplot2):

d <- data.frame(x = seq(1, 10, 1), y = seq(1, 10, 1))
ggplot(d, aes(x, y)) +
  geom_line() +
  geom_point() +
  theme(text=element_text(family="LM Roman 10", size=20)) +
  ggtitle("This plot uses LaTeX font!")
ggsave("fig.pdf", width = 6, height = 4, device=cairo_pdf)

Refering to @Augustin answer fontsquirrel lets you download OTF files by default. OTF and TTF files are nevertheless same fonts shapes but

from library(extrafont) font_import(pattern = "lmodern*") only installs TTF files.

To install TTF follow the following steps.

  1. Open webpage https://www.fontsquirrel.com/fonts/latin-modern-roman
  2. Click "Webfont Kit", then check "TTF"
  3. Click "DOWNLOAD @FONT-FACE KIT", download a zip file containing TTF version fonts.
  4. Install "lmroman10-regular-webfont"

then use font_import(pattern = "lmodern*")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .