Posted by & filed under LaTeX, R.

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='')
 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.

http://blog.nguyenvq.com/wp-content/uploads/2009/05/BetaHat-vs-r-DISCRETE.jpeg

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

2 Responses to “Computer modern (latex) font in R plots”

  1. karim

    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

    Reply

Trackbacks/Pingbacks

  1.  Creating even NICER, publishable, embeddable plots using tikzDevice in R for use with LaTeX

Leave a Reply

  • (will not be published)