TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to R, Sweave, and LaTeX. I am running StatET with Eclipse 3.7 on a Windows 7 x64 OS. StatET also includes the Sweave add-on, which I use to try and compile a PDF document.

I also installed MiKTeX 2.9, because, previously, R could not execute texi2dvi (presumably, because there was no TeX runner). Basically, no .tex file could be compiled into PDF file. After I installed MiKTeX, I was successful at compiling a .pdf file from .tex file.

However, the moment I introduce pdf() function into the .Rnw file, I get a "fatal" error.

When trying to compile this simple .Rnw file:




<<echo=F, results=hide>>=


I get the following error message:

Sweave(file = "C:/Users/Egor/Desktop/R/Head-tracking/pdftest.Rnw") Writing to file pdftest.tex Processing code chunks with options ... 1 : term hide

You can now run (pdf)latex on 'pdftest.tex'


texi2dvi(file = "C:/Users/Egor/Desktop/R/Head-tracking/Data/pdftest.tex", pdf = TRUE)

Error: running 'texi2dvi' on 'C:/Users/Egor/Desktop/R/Head-tracking/Data/pdftest.tex' failed

LaTeX errors: C:/Users/Egor/Desktop/R/Head-tracking/Data/pdftest.tex:13: ==> Fatal error occurred, no output PDF file produced!

In addition: Warning message: running command '"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\texi2dvi.exe" --quiet --pdf "C:/Users/Egor/Desktop/R/Head-tracking/Data/pdftest.tex" -I "C:/Program Files/R/R-2.13.1/share/texmf/tex/latex" -I "C:/Program Files/R/R-2.13.1/share/texmf/bibtex/bst"' had status 1

I'm following the method described by Ista Zahn in his guide Learning to Sweave in APA Style: http://www.tug.org/pracjourn/2008-1/zahn/zahn.pdf.

The method I'm going to present here is specifically chosen because it makes it easier to produce figures that are sized appropriately, with appropriately sized axis labels and legends (something that can be tricky with other methods). Specifically, I recommend creating your graphic and saving it to a .pdf file, and then inserting the image into a figure environment.

Unfortunately, I cannot include the image file of the code, because I am a new user. It is on page 14 of Ista's guide, but here's an ugly version of it:

<<echo=f a l s e , r e s u l t s=hide>>=
pdf ( f i l e=" spl o t 1 2 . pdf " , width=6, he i ght=5)
qplo t ( educat ion , income , shape=type , s i z e=women , c o l our=p r e s t i g e ,
xlab="Education " , ylab="Income ")
dev . of f ( )
nbeginf f i g u r e g
n i n c l u d e g r a p h i c s [ width=6in , he i ght=5in ] f spl o t 1 2 . pdf g
ncaptionfA s c a t t e r p l o t d i s p l a y i n g the r e l a t i o n s h i p s
among Income , Education , Pr e s t i g e , Type o f occupat ion ,
and pe r c entage o f Women in the f i e l d . g
n label f f i g : f i g 1 2 g
nendf f i g u r e g

I did try to just plot without an intermediate .pdf file, but using only height and width options of \includegraphics gave me distorted figures. Sorry, it is not clear from the first code I provided that I am later planning on recalling this figure with LaTeX code, because I just wanted to concentrate on getting a pdf() function to work inside the <<>>=...@ thing. It clearly doesn't for me, whereas it does for Ista.

I decided to create the .pdf image files in a separate .R file, and then compile the comprehensive .pdf file with Sweave, but that's just an intermediate solution.

Thanks! --Egor

share|improve this question

You seem to be confusing two ways of producing a pdf.

Sweave is a part of base R, and lets you include R code in an .Rnw file that will be 'sweaved' into a tex file with the results of the R code. You can then use a tex implementation to render that tex file into a PDF file.

You can also use R to produce plots and other graphic objects straight into a pdf file using the pdf() function.

To include your plot into a sweave document, replace your plot chunk with

<<name="Figure", echo=F, figure=True>>= 

Then save your file and sweave it. sweave should produce a pdf and eps version fo your graph along with a tex document. Then run texi2dvi to produce a pdf.

share|improve this answer
Hi Paul, thank you for your response. I was aware that this is an option, but it does not provide the kind of control over the size of the image that I want (see the second part of the updated question above). Isn't it possible to call on pdf() function within the <<>>=...@? – egor.ananyev Oct 25 '11 at 22:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.