Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am including an image in R and found the name of the image file appearing with the image:

  name<-c("A.pdf","B.pdf","C.pdf","D.pdf")
  <<echo=FALSE,results=tex>>=
  for (i in 1:4)
  {      
     cat(paste("\\includegraphics[trim=0cm 10cm 0cm 5cm, clip=true,%
                          width=.7\\linewidth]{pdf_repository/",name[i],".pdf}",sep=""),"\n")
  }
  @

I found the names of image files: A.pdf, B.pdf, C.pdf, D.pdf also appear with the images. How can I remove them?

share|improve this question
2  
Why do you append the pdf suffix in line 5 again? –  user1146332 Jan 22 '13 at 10:02
add comment

2 Answers

up vote 1 down vote accepted

Your names is outside a code block and so is being included as text to be formatted by LaTeX. Is that what you are seeing? Moving it inside the R code block, I don't see file names.

\documentclass{article}
\usepackage[american]{babel}
\begin{document}

<<echo=FALSE,results=tex>>=
name<-c("A.pdf","B.pdf","C.pdf","D.pdf")
for (i in 1:4) {
  cat(paste("\\includegraphics[trim=0cm 10cm 0cm 5cm, clip=true, width=.7\\linewidth]{pdf_repository/",name[i],"}",sep=""),"\n")
}
@

\end{document}

gives the .tex file

\documentclass{article}
\usepackage[american]{babel}
\usepackage{Sweave}
\begin{document}

\includegraphics[trim=0cm 10cm 0cm 5cm, clip=true, width=.7\linewidth]{pdf_repository/A.pdf} 
\includegraphics[trim=0cm 10cm 0cm 5cm, clip=true, width=.7\linewidth]{pdf_repository/B.pdf} 
\includegraphics[trim=0cm 10cm 0cm 5cm, clip=true, width=.7\linewidth]{pdf_repository/C.pdf} 
\includegraphics[trim=0cm 10cm 0cm 5cm, clip=true, width=.7\linewidth]{pdf_repository/D.pdf} 
\end{document}

which compiles to a PDF with just the 4 graphics (assuming there are the 4 PDFs to include in the given directory).

share|improve this answer
add comment

As the first comment said, you wrote .pdf twice (e.g. in your tex output, you will see A.pdf.pdf).

As much as I hate the cat('\\includegraphics') trick, here is the knitr solution:

\documentclass{article}
<<setup, include=FALSE>>=
opts_chunk$set(fig.path = 'pdf_repository/')
@

\begin{document}
<<test, out.width='.7\\linewidth', out.extra='trim = 0cm 10cm 0cm 5cm, clip = true'>>=
plot(rnorm(30))
plot(rnorm(30))
plot(rnorm(30))
plot(rnorm(30))
@
\end{document}

Just like you call Sweave() on the Rnw file, you can install the knitr package and call library(knitr); knit(). You will never worry about multiple plots per chunk again.

multiple plots from knitr

share|improve this answer
add comment

Your Answer

 
discard

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.