Super Nerdy Cool

My (technical) adventures through the professional world…
May 11, 2009

Computer modern (latex) font in R plots

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):

<pre class="src src-R"><span style="color: #87cefa;">embedFonts2</span> <span style="color: #7fffd4;">&lt;-</span> <span style="color: #00ffff;">function</span>(file, format=<span style="color: #ffa07a;">'pdfwrite'</span>, outfile=file, fontpaths=c(<span style="color: #ffa07a;">'~/KeepInHome/cm-lgc/fonts/type1/public/cm-lgc'</span>,<span style="color: #ffa07a;">'~/KeepInHome/cmsyase'</span>)){

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.

3 Comments to “Computer modern (latex) font in R plots”

Leave a Comment