R-bloggers

2012-09-15

A Useful Function for Sweave Users: Sweatex()

This function is courtesy of AC Davison at EPFL.

Here you go:
options(width=65, digits=7)
options(show.signif.stars=FALSE)
# ps.options(horizontal=FALSE)
set.seed(1234)

Sweatex <- function(filename,extension='Rnw',command='latex',silent=FALSE,preview=TRUE,bibtex=FALSE)
{  
        #check if latex path is present, otherwise adds it to current path for the GUI
        latex.path <- dirname(options('latexcmd')[[1]])
        path <- as.character(Sys.getenv("PATH"))
        if (regexpr(latex.path,path)==-1) {Sys.putenv("PATH"= paste(path,":",latex.path, sep=""))}

        if (command=='latex') command='simpdftex latex --maxpfb'
    extension<-paste('.',extension,sep='')
        Sweave(paste(filename,extension,sep=''))
    if (bibtex)
    {  # this does latex+bibtex+latex (if you need latex one more time, uncomment last row)
        system(paste(command,' ',filename,sep=''),intern=silent)
        system(paste('bibtex',' ',filename,sep=''),intern=silent)
        system(paste(command,' ',filename,sep=''),intern=silent)
        system(paste(command,' ',filename,sep=''),intern=silent)
    }
    else system(paste(command,' ',filename,sep=''),intern=silent)
        if (preview) 
        {
                system(paste(options('pdfviewer')[[1]],' ',filename,'.pdf',sep=''))
        }
            
}

No comments:

Post a Comment