Sign up ×

I want to find a programmatic way of converting a .tex latex document to pdf.

What I usually do, and this works in most cases, is to use:

tools::texi2dvi(latexfile, pdf = TRUE, clean = TRUE)

However, I now need to work with documents that contain Asian fonts (Chinese, Japanese and Korean). Searching on leads to advice to incude the following lines in the tex file:

\setCJKmainfont{MS Mincho} % for \rmfamily

This means I need to use xelatex to build the pdf. I have tried variations on the following theme, none of which work.

shell(cmd="xelatex latexfile", mustWork=TRUE, translate=TRUE)

Question: How can I call xelatex programmatically?

Additional information:

  • I am on Windows 7, R2.13-1
  • xelatex is installed on my machine and works fine if I call it any other way (including via the command line)
  • I am using StatET in Eclipse
share|improve this question

migrated from Sep 27 '11 at 11:30

This question came from our site for professional and enthusiast programmers.

but what happens when you run that shell() expression? what is the error? –  mdsumner Sep 27 '11 at 8:46
If you are using MiKTeX, its texi2dvi executable has a --engine=ENGINE option. So you could call it directly through shell –  James Sep 27 '11 at 11:24
I notice that this question was migrated from StackOverflow to tex.stackexchange. This makes no sense at all - my question is not a latex question, but an R programming question. –  Andrie Sep 27 '11 at 12:30
I agree, but you should have asked it accurately in the first place . . . it's about control over spawning commands to the system from R (possibly via a 3rd party) on windows :) –  mdsumner Sep 27 '11 at 12:37
@mdsumner Yes, it could have been more accurate. And I would have made it more accurate if I understood more about the problem. Thanks very much for your help! –  Andrie Sep 27 '11 at 12:46

2 Answers 2

up vote 3 down vote accepted

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)
share|improve this answer

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")
share|improve this answer
+1 Neat. I'll try this. –  Andrie Sep 27 '11 at 12:54

Your Answer


