6

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.

12
  • I'm not going to do some eps-fu to get this working, if latex is this terrible at handling pdfs them I'm just going to combine these graphs manually at the back of the report :P
    – gakera
    Commented Nov 18, 2010 at 11:33
  • 1
    @gakera I've included R output PDFs in LaTeX before with text intact, so it's not that LaTeX is terrible at handling pdfs; it's something more specific...
    – Seamus
    Commented Nov 18, 2010 at 11:53
  • 3
    Probably there is a font problem. Either your R-pdfs don't include their fonts or there is some clash. But to be able to help you a small example tex-file, one of the R-pdf's, the log-file and the result of the pdflatex run of the example is needed. Commented Nov 18, 2010 at 12:09
  • I tried to look at the pdf file, but for some reason, i am unable to download it. Commented Nov 18, 2010 at 13:00
  • added mirror: scribd.com/doc/43122065/Vf
    – gakera
    Commented Nov 18, 2010 at 13:09

4 Answers 4

7

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.

3
  • Very interesting, but I get this error because of an un-escaped # sign in drawing, which is included in a string so it shouldn't be a problem: ! You can't use `macro parameter character #' in restricted horizontal mode. l.591 ...0] at (106.15,417.09) {Fishing gear 2 , #
    – gakera
    Commented Nov 18, 2010 at 21:10
  • And I get some random errors when I plot to tikz that aren't there when I just plot straight to screen or even pdf. I'm using sm.density.compare() with apparently some sketchy (according to tikz) vectors. I dunno. Very nice concept tho, I'll try it again with some other project.
    – gakera
    Commented Nov 18, 2010 at 21:16
  • @gakera I edited my post to address your first comment. For your second, if you are getting warnings saying something like: running command <blah blah blah> had status 1---this is due to a recent change in the way R handles the return value of system(). In this case, nothing is wrong and we are working on a fix that will restore sanity to the warning messages. If you are getting some other kind of error, we would very much like to hear about it so we can find a fix.
    – Sharpie
    Commented Nov 18, 2010 at 22:15
2

Option 1 (medium easy and hopefully would do it): Any of my font issues went away when I switched over to the XeTeX from pdfTeX. I use TextMate, which has a pop-down list to switch engines. Instructions on doing so in TexMaker are here. XeTeX handles fonts more easily than does pdfTeX. I have had COUNTLESS issues with imported PDF files and XeTeX eliminated them. YMMV. Remember: XeTeX reads in your LaTeX documents perfectly and outputs a PDF. There isn't any reason (that I know of) to stick with pdfTeX. (cue someone leaping at their keyboard to hammer out a reason in 5, 4, 3...)

Option 2 (easy, if you have the software and would for sure work): Use your copy of Illustrator or PS STill by Stone or (google: outline PDF fonts) and outline the fonts, which means that you turn them from text into paths (much like your graph).

Option 3 (easy but low quality): Save as a high res PNG file. Anything around 250dpi should be fine. If you think this is not acceptable, I would argue that bring external fonts from another source and that do not match your documents design already violates good design, so you're not that much worse for the wear! :)

Side Note: I don't know how long you've been using LaTeX, but I'll throw out some free advice. While LaTeX does a great job with external illustrations, it does less of a great job dealing with external text. Any time that I've styled something outside of LaTeX, I've always ended up bringing back in and doing it with PGF/TikZ. It not only is much less hassle, it also looks better and you'll find that everything is far more maintainable. For what it is worth...

0

I fixed this by manually saving the plot as displayed by R (save as in the plot window) and then importing that PDF instead of the one generated by my R code directly.

Essentially, in R:

pdf(file = ss.pdf)
plot(bla)
title("This will not show up when I include the pdf file in my Latex code")
dev.off()

where as:

plot(bla)
title("This will be fine if I "save as: ss.pdf" from the R window and then include in Latex")
2
  • I tried telling R to embed the font it uses, pdf(file = ss.pdf, fonts="Helvetica"), but still, nothing. Bah.
    – gakera
    Commented Nov 18, 2010 at 15:54
  • Try to use another font than Helvetica, e.g. NimbusSan. (Disclaimer: I have no idea what R is and what is it doing and how to use it, but a fast search on the mailing list seems to indicate that embedding won't work with the standard fonts.) Commented Nov 18, 2010 at 17:40
0

the pdfpages package may help!

\usepackage{pdfpages}

\includepdf[pages=1.4]{foo.pdf}

1
  • heh, I tried that one already and it gives me the same result. It's even included in the sample code I gave, if you read carefully ;)
    – gakera
    Commented Nov 18, 2010 at 17:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .