I work with Emacs 24 on Mac OS X 10.7.3 with R 2.14.0. I have a file foo.Rnw
containing
\documentclass[
paper=a4,% 210mm × 297mm
pagesize% write page size to dvi
]{article}
\usepackage[american]{babel}
\usepackage{fancyvrb}
\usepackage[utf8]{inputenc}
\SweaveOpts{engine=R}
\SweaveOpts{pdf=TRUE}
\begin{document}
Some text
<<>>=
sessionInfo()
@
\end{document}
My .emacs
for working with Sweave looks like this:
(add-hook 'Rnw-mode-hook
(lambda ()
(add-to-list 'TeX-command-list
'("Sweave" "R CMD Sweave %s"
TeX-run-command nil t :help "Run Sweave") t)
(add-to-list 'TeX-command-list
'("Stangle" "R CMD Stangle %s"
TeX-run-command nil t :help "Run Stangle") t)
(setq TeX-command-default "Sweave")))
If I use C-c C-c
and then choose Sweave
, the .tex
file looks like this:
\documentclass[
NA
pagesize% write page size to dvi
]{article}
\usepackage[american]{babel}
\usepackage{fancyvrb}
\usepackage[utf8]{inputenc}
\usepackage{Sweave}
\begin{document}
Some text
\begin{Schunk}
\begin{Sinput}
> sessionInfo()
\end{Sinput}
\begin{Soutput}
R version 2.14.0 (2011-10-31)
Platform: x86_64-apple-darwin11.2.0/x86_64 (64-bit)
locale:
[1] C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_2.14.0
\end{Soutput}
\end{Schunk}
\end{document}
As you can see, the second line of foo.Rnw containing the non-ASCII character ×
is replaced by NA
. I could figure out that this has to do with the localization in which R
runs (see also the output of locale: [1] C
. I could include statements of the form export LC_MESSAGES="en_US.UTF-8"
in my .bashrc
so that R
gets the correct locale
. I can also perfectly use R CMD Sweave foo.Rnw
from the terminal (which of course loads .bashrc
and thus sets locale
correctly). The question is: How can I make it work through Emacs, so that I can run Sweave via C-c C-c Sweave
? I know that Emacs is not aware of variables set in .bashrc
, but how can I guarantee that R CMD Sweave
issued from Emacs runs with the same localization as when I execute it from the terminal?
I found several posts to this problem, but no solution for Macs and from within Emacs. Again, note that I can perfectly run R CMD Sweave foo.Rnw
from the terminal (due to my settings in .bashrc
) -- most of the posts address this issue -- it's just not working from within Emacs (which is quite annoying).
UPDATE:
My ~/.bashrc
contains:
export LC_COLLATE="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
export LC_MONETARY="en_US.UTF-8"
export LC_TIME="en_US.UTF-8"
export LC_MESSAGES="en_US.UTF-8"
Following the advice of Yihui, I put in the following in my ~/.Rprofile
:
invisible(Sys.setlocale("LC_COLLATE", "en_US.UTF-8"))
invisible(Sys.setlocale("LC_CTYPE", "en_US.UTF-8"))
invisible(Sys.setlocale("LC_MONETARY", "en_US.UTF-8"))
invisible(Sys.setlocale("LC_TIME", "en_US.UTF-8"))
invisible(Sys.setlocale("LC_MESSAGES", "en_US.UTF-8"))
This solved the problem.
The final answer can be found under UPDATE.
@cbeleites: I do not run Mac OS X anymore (neither R 2-14) so I can't tell.