# Computer modern (latex) font in R plots

NOTE: embedFonts() did not work for me in R.

I wrote my own embedFonts2() function (you can re-write the original function, changing just the execution command to make it work):

embedFonts2 <- function(file, format='pdfwrite', outfile=file, fontpaths=c('~/KeepInHome/cm-lgc/fonts/type1/public/cm-lgc','~/KeepInHome/cmsyase')){
tmpfile <- tempfile('Rembed')
cmd <- paste('GS\_FONTPATH=', paste(fontpaths, collapse=':', sep='')
, ':. gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -sDEVICE='
, format, ' -sOutputFile='
, tmpfile
, ' '
, file
, sep='')
system(cmd)
file.copy(tmpfile, outfile, overwrite=TRUE)
invisible(cmd)
}


The key for me is the GS_FONTPATH declaration in the beginning. The -sFONTPATH option did not work for me.

Kind of cool to get the font in your plots to match the computer modern font in LaTeX. I don’t use this much though, unless I need mathematical annotations on the plots. I absolutely hate the default font in R for this.

Another way to do this is to use psfrag where you can replace text in your plots (postscript files) with LaTeX. Kind of neat, but it requires too much effort I think. I guess it’s OK to do for a paper.

This is a jpeg version (converted to jpeg from pdf using ImageMagick’s convert utility). Here is the original pdf file.

## 3 thoughts on “Computer modern (latex) font in R plots”

1. karim says:

Hello

very nice!
can you please tell me what kind of paths you have used in this embedFonts2 function?
I’m using miktex for latex…

kind regards
karim