0

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?

  • 2
    Why do you append the pdf suffix in line 5 again? – user1146332 Jan 22 '13 at 10:02
1

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).

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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