Making LaTeX documents with Hebrew? (in Sweave + RStudio)

I would like to write in Hebrew in an .Rnw file, and use it with RStudio.

Searching around, I found an example for using Hebrew in LaTeX in general: Numbers, punctuation and parenthesis reversed in RTL... But when I try running the example from above in an RStudio session, I get a bunch of errors. e.g:

Sweave('aaa2.Rnw') Writing to file aaa2.tex
Processing code chunks with options ...

You can now run (pdf)latex on 'aaa2.tex'

Warning messages:
1: 'aaa2.Rnw' has unknown encoding: assuming Latin-1
2: invalid char string in output conversion

Running texi2dvi...
Error: running 'texi2dvi' on 'aaa2.tex' failed

LaTeX errors:
! ! The fontspec package requires either XeTeX or LuaTeX to function. ! The fontspec package requires either XeTeX or LuaTeX to function. ! ! ! You must change your typesetting engine to, e.g., "xelatex" or "lualatex" ! You must change your typesetting engine to, e.g., "xelatex" or "lualatex" ! instead of plain "latex" or "pdflatex". ! instead of plain "latex" or "pdflatex". ! ! ! See the fontspec documentation for further information. ! See the fontspec documentation for further information. ! ! ! For immediate help type H ! For immediate help type H |''''''''''''''''''''''''''''''''''''''''''''''' | This is a fatal error: LaTeX will abort |...............................................

running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\texi2dvi.exe" --quiet --pdf "aaa2.tex" -I "d:/R/R-214~1.0/share/texmf/tex/latex" -I "d:/R/R-214~1.0/share/texmf/bibtex/bst"' had status 1


Any suggestions?

-

There's no completely simple way to do this within RStudio. The reason for this is that the compiledPdf command that RStudio provides uses texi2dvi to compile the LaTeX file generated by Sweave, and texi2dvi knows nothing about xelatex, which is what you should be using for your Hebrew documents. So the solution is to run Sweave manually from within RStudio, and then run xelatex from the console. To get you started, here's a test document that I had lying around for a separate question here on the site that I have adapted for use with Hebrew.

Update The development version of RStudio (versions > 0.96.48) now supports XeLaTeX.

Rnw file (assume name is sweave-test.Rnw)

%  This document must be compiled with Sweave and xelatex (not pdflatex)
\documentclass{article}
\usepackage{booktabs}
\usepackage{caption}
\usepackage[noae]{Sweave} % you must load Sweave with the noae option
% load polyglossia late in the package load order, since the bidi package (which is
% loaded when an RTL language is set) redefines lots of package code.
\usepackage{polyglossia}
\setmainlanguage{hebrew}
\setotherlanguage{english}
\setmainfont{David CLM}
% for R work with Hebrew, it's important to have a mono font that supports
% the Hebrew script.  Miriam Mono is one that I know of
\setmonofont[Script=Hebrew]{Miriam Mono CLM}
\title{\textenglish{Side-by-side xtables}}
\author{}
\date{}
\begin{document}
\maketitle
ראשית קצת קוד R כדי ליצור כמה נתונים.
<<>>=
myData <- matrix(c(19,89,23,23,74,44,16,39,67),ncol=3,byrow=TRUE)
colnames(myData) <- c("A","B","C")
rownames(myData) <- c("1","2","3")
myData2 <- myData * 2
@

כעת אנו שמים את הנתונים בשני צדדי על ידי צד טבלאות:

\begin{table}[htb]
\begin{minipage}{.45\textwidth}
\centering
\captionof{table}{\textenglish{First Table}}
<<echo=FALSE,results=tex>>=
library("xtable")
print(xtable(myData),
floating=FALSE,
hline.after=NULL,
command=c('\\toprule\n','\\midrule\n','\\bottomrule\n')))
@

\end{minipage}
\begin{minipage}{.45\textwidth}
\centering
\captionof{table}{\textenglish{Second Table}}
<<echo=FALSE,results=tex>>=
print(xtable(myData2),
floating=FALSE,
hline.after=NULL,
command=c('\\toprule\n','\\midrule\n','\\bottomrule\n')))
@

\end{minipage}
\end{table}
\end{document}


Open this document within RStudio, and then run Sweave on it:

Sweave("sweave-test.Rnw")


This should then exit with the following message:

Writing to file sweave-test.tex
Processing code chunks with options ...
1 : echo term verbatim
2 : term tex
3 : term tex

You can now run (pdf)latex on 'sweave-test.tex'


Now you need to run xelatex (not pdflatex) on the resultant .tex file. Assuming your system is set up to run correctly from the command line, this is probably easiest to do by opening a Shell window from within RStudio, and then running the command from there.

xelatex sweave-test.tex


Then you will need to open the resulting .pdf file with your favourite viewer. Since the latest stable release of RStudio isn't very customizable, I don't think there's a simple way around this. I don't use RStudio myself, though; on a Mac I can run Sweave files directly from within my LaTeX IDE (TeXShop).

Using XeLaTeX with later versions of RStudio

Currently version 0.96.48 of RStudio now has preferences to use XeLaTeX for Sweave compilation. In the preferences, choose XeLaTeX for compilation, and make sure the "Use texi2dvi to resolve cross references is unchecked. Here's an image of the preference panel.

Output

Here is the output. Since I don't know Hebrew, I've just used Google translate to translate the little bit of text in the document. I'm imagine it's quite amusing.

-
I would correct the automatic translation of the second sentence but that would spoil the screenshot... –  einpoklum Mar 4 '12 at 15:37
@EyalRozenberg Since it's certainly not crucial to the answer, I think we can leave it as is. (Unless Google translated it into something very rude.) –  Alan Munn Mar 4 '12 at 15:41
the development version of RStudio allows you to use xelatex: rstudio.org/download/daily –  Yihui Mar 8 '12 at 1:19
@Yihui Thanks. That's good to know. It's a shame that it's hard to keep up with progress in the TeX world and the R world simultaneously. The R world is unfortunately always a bit behind the TeX world in terms of its ability to use the latest TeX things. –  Alan Munn Mar 8 '12 at 1:26
@yihui You can wrap the text in a \begin{LTR} ... \end{LTR}` environment. –  Alan Munn May 22 '12 at 3:33