Search code examples
rlatexsweaveknitr

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)


Solution

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