[R] Solved: Input encoding problem when using sweave with xetex

Erich Studerus erich.studerus at bli.uzh.ch
Thu May 13 14:03:41 CEST 2010


With the kind help of Duncan Murdoch, I finally managed to correctly compile 
LyX-Sweave documents with XeTeX under Windows. In case someone else is 
struggling with a similar problem in the future, here is a small summary of 
what worked for me. I assume that you have already set up Lyx to work with 
Sweave and XeTeX according to the following instructions:
http://cran.r-project.org/contrib/extra/lyx/INSTALL
http://wiki.lyx.org/LyX/XeTeX

My original problem was that accented letters that were read from 
Excel-files or other external sources from within R during the LyX-Sweave 
document compilation were not correctly encoded in UTF-8. However, UTF-8 is 
needed for typsetting the final document with XeTeX.

Because R code chunks contained in the Lyx-Sweave document were executed by 
running R from the command line, it was necessary to start R with UTF-8 
encoding from the command line. I managed to do this by editing the 
Sweave.sh file which can be found in rhome/bin/ directory. Within sweave.sh, 
I changed the command

echo "library(\"utils\"); Sweave(\"file.Rnw\")" | R --no-restore --slave

into

echo "library(\"utils\"); options(encoding=\"UTF-8\"); Sweave(\"file.Rnw\")" 
| R --no-restore --slave

Because I wanted to run R with UTF-8 encoding only when I compile my 
documents with XeTeX and not with LaTeX, I saved the Sweave.sh file with the 
new name UTF8Sweave.sh in the same directory and changed the definition for 
the conversion of Lyx-Sweave documents to PDF (xelatex). I did this by 
defining a new file format in LyX under
Tools->Preferences->File handling->File formats. The file format that I 
defined had the same settings as LaTeX (pdflatex), but I saved it with the 
new name LaTeX (pdflatexUTF8). I then defined a new converter under 
Tools->Preferences->File handling->File formats with the following settings:
 From format: Sweave
To format: LaTex (pdflatexUTF8)
Converter: R CMD /path/to/UTF8Sweave.sh $$i

Because the full path to UTF8Sweave.sh file contained blanks, I had to use 
the 8.3 filename which I determined by using the MSDOS cmd window. See this 
post for more information:
http://article.gmane.org/gmane.comp.lang.r.general/190040

Finally, I changed the converter LaTeX (pdflatex) -> PDF(xelatex) which I 
had already defined for the original LyX-XeTeX installation(see 
http://wiki.lyx.org/LyX/XeTeX) to LaTeX (pdflatexUTF8) -> PDF(xelatex).

I hope this helps.

Regards,
Erich



More information about the R-help mailing list