[Tex/LaTex] \includegraphics and \includepdf both remove all copy (text) from the (R outputted) pdf graphs, using pdfTeX

graphicspdfpdftex

I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009) (format=pdflatex 2010.8.30) in TexMaker 2.1 on Mac OSX. I installed the whatever latex packages using the "easier" method at http://guides.macrumors.com/Installing_LaTeX_on_a_Mac – the MacTex batch.

I'm outputting some graphs to PDFs in R and want to include them in my PDF report. Both includegraphics and includepdf input the same, only the lines and colors, but not the text in the legend or the main title. What's going on?

Here is a link to my pdf: http://www.scribd.com/doc/43122065/Vf and some sample tex:

\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{pdfpages}
\begin{document}

Bladebla

\includepdf{Vf.pdf} 
\includegraphics{Vf.pdf}

What more bla bla
\end{document}

I tried just this snippet of code, and I get the same results, so nothing else in my preamble should be interfering with it.

Best Answer

Note: I co-authored the package I am about to recommend, so this is a shameless plug.

You can try creating your graphics using the tikzDevivce which lets TeX handle all of the typesetting. This will hopefully sidestep any font issues you are having as the text in your graphics will use the same font as the body text of your paper, which is displaying fine.

To install the package in R:

install.packages('tikzDevice')

Then make your graph as usual, except use tikz() instead of pdf() to start the plot output:

require(tikzDevice)
tikz('myAwesomeRPlot.tex')

# Your plotting code here

dev.off()

The output of the tikzDevice is a .tex file containing commands required to construct your plot using TikZ---an excellent graphics package for TeX, LaTeX and ConTeXt (however, our output is currently only compatible with LaTeX). To include your plot in a paper, use \input instead of \includegraphics:

% In your preamble:
\usepackage{tikz}

% Later on in your paper...
\begin{figure}
  \input{myAwesomeRPlot}
  \caption{Check out the spread on that distribution!}
\end{figure}

You can also pass the standAlone=TRUE option to tikz() and the resulting .tex file will be self-contained and can be typeset by LaTeX without including it in another document.

For more information and examples, check out the package vignette.

Hope this helps!

Addendum

It should be noted that the tikzDevice treats all plot text as LaTeX code. This allows you do do things like:

curve({x^2 / 2},-1,1,main="Plot of $y=\\frac{x^2}{2}$",ylab="$\\frac{x^2}{2}$")

And LaTeX will typeset the equations in your plot. This behavior has some consequences:

  • The backslash at the beginning of each LaTeX command must be escaped by another backslash as R treats backslashes as escape characters.

  • Any character that is special to LaTeX, such as #, $, %, ^, _, etc, must be escaped if you don't want it treated as a LaTeX command:

    plot(...,main="I really want the dollar sign in \\$2.99!")
    
  • If LaTeX code in your plot text requires special packages, you need to make the tikzDevice aware of this. See the documentation of tikzLatexPackages in section 3.1 of the vignette for details.