Compiling latex directly from R

With some help from the helpful people at StackOverflow, two things emerged:

  1. The correct way to call xelatex using a shell script from R is to remove the path from the full filename.

  2. The reason that xelatex doesn't return control is because it enters interactive mode when detecting an error. To fix this, use the argument --mode=batchmode:

    shell(cmd=paste("xelatex --mode=batchmode", basename(latexfile)), 
         mustWork=TRUE, intern=TRUE, translate=TRUE)

You could also try texi2dvi with an environment variableLATEX=xelatex (not tested).

See the source code of tools::texi2dvi (lines 145-147):

...
latex <- if (pdf) 
    Sys.getenv("PDFLATEX", "pdflatex")
else Sys.getenv("LATEX", "latex")
...