Computer modern (latex) font in R plots


Vinh Nguyen


May 11, 2009

I did read about this topic before I started the blog. I'll post instructions now.

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='')
 file.copy(tmpfile, outfile, overwrite=TRUE)

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.