Search code examples
rknitrr-markdownsweavepdflatex

Remove .tex files automatically after compiling .rnw file using knitr


For rmarkdown files there is an option which allows to choose whether or not the .tex file should be deleted after compiling. So only the .rmd and the .pdf files remain.

Is there a way to achieve this for .rnw files as well?


Solution

  • I have not found any other solution than implementing a cleaner function myself. Check this:

    cleanFolder <- function() {
      answer <- NA
      while(!(answer %in% c('y', 'n'))) {
        answer <- readline(paste("Clean ", getwd(),"? (y/n)"))
      }
      if(answer == 'y') {
        rules <- c('.log', '.vrb', '.nav', '.snm', '.toc', 
                   '-tikzDictionary', '.tex', '.synctex.gz')
        file.remove(list.files(pattern = paste0('\\',rules ,'$', collapse = '|')))
      }
    }
    

    It should be self-explaining:

    1. The user is asked to confirm that the current working directory is the one to be cleaned.

    2. rules contains all the file extentions (or endings like -tikzDictionary) that should be taken care of.

    3. Finally file.remove deletes all the files selected by list.files using a regex expression.

    You could add this function to a custom package which is loaded when you start a new R session and just call it after compiling your *.Rnw document.