Pretty Figures with tikzDevice


The standard way to integrate graphics into LaTeX is still by using the graphicx package. If you happen to create most of your graphs with GNU/R like me, you can instead use the tikzDevice package. Note that by the time of this blog post the tikzDevice package is not available in the archive due to failing tests. Anyhow, the package is fully functional (at least as far as I can tell). Read my other blog post on how to install it anyway.

The advantage of the tikzDevice is the integration into the LaTeX document. Rather than including the figure from an PDF of EPS file, the tikzDevice renders the figure as an actual TeX file using the tikz macro package.

Therefore, all fonts will be type set by LaTeX during it’s compilation phase and thus can include formulas, special characters, or any other LaTeX package commands you are using in your document (e. g. abbreviations, siunitx, etc.). Besides that, the fonts in the figure will match the LaTeX font you use in your document (unless you specify otherwise) and thus won’t break the layout.

Furthermore, all images, lines, symbols show up amazingly pretty as they are actually plotted by LaTeX itself rather than created and pre-rendered by an external application into an PDF/EPS file.

To use the tikzDevice, you need to add a preemble to your R script. I have mine in my snippets list, so I don’t need to copy it all the time.

# load tikzDevice package

# configure tikz params - width and height in inches
tikzheight <- 3.2
tikzwidth <- 3.2
fntsize <- 0.8
options( tikzDocumentDeclaration = c(
  "\\fxuselayouts{index}") )
# prepare output medium
tikz( file="figures/outfile.tex",
  height=tikzheight )
par( cex=fntsize ) 

Put these lines before your actual R script and the output will be written into figures/outfile.tex - which you can change in the call to tikz(...) above.

png version tikz version

Neat, isn’t it?