Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Sweave or knitr put graphics suffix in `\includegraphics{}`

I just run into the (curious) problem that when submitting a (pdf)LaTeX manuscript to some Elsevier journal the filenames of figures needed to be complete in order to found by their pdf building and checking syste, i.e.:

\includegraphics{picture.pdf}

Is there any easy and convenient way to tell Sweave or knitr to do that?

Edit:

  • I'm familiar with sweave's include=FALSE option
  • I also feel quite capable to patch utils:::RweaveLatexRuncode

However, for the moment I'm hoping that there's something more convenient and elegant.

It's also about handing out the .Rnw files as supplementary material or vignettes. From a didactic point of view I don't like these tweaks that make the source code much more complicated for the new users of whom I hope they read it.

(Which is also why I really appreciate the recently introduced print=TRUE in Sweave)

like image 582
cbeleites unhappy with SX Avatar asked Aug 11 '12 14:08

cbeleites unhappy with SX


2 Answers

You can modify the plot hook a little bit in knitr to add the file extension:

<<>>=
knit_hooks$set(plot = function(x, options) {
  x = paste(x, collapse = '.') # x is file.ext now instead of c(file, ext)
  paste0('\\end{kframe}', hook_plot_tex(x, options), '\\begin{kframe}')
})
@

See 033-file-extension.Rnw for a complete example. To understand what is going on behind the scene, see the source code of the default LaTeX hooks in knitr.

like image 164
Yihui Xie Avatar answered Nov 15 '22 16:11

Yihui Xie


A brute force solution is to explicitly create the files yourself in the R snippet. Set the option for graphics etc to false but have the code evaluated so that the file is created, and then have latex call them with the very \includegraphics{} call you show.

I used similar schemes for simple caching: if the target file exists, skip the code creating.

like image 36
Dirk Eddelbuettel Avatar answered Nov 15 '22 17:11

Dirk Eddelbuettel