问题描述
我在Mac OS X 10.7.3中使用了R 2.14.0的Emacs 24。我有一个文件 foo.Rnw
包含 \documentclass [
paper = a4,%210mm×297mm
pagesize%将页面大小写入dvi
] {article}
\usepackage [美式] {babel}
\usepackage {fancyvrb}
\usepackage [utf8] {inputenc}
\SweaveOpts {engine = R}
\SweaveOpts {pdf = TRUE}
\begin {document}
一些文本
<>> =
sessionInfo()
@
\end {document}
我的 .emacs
与Sweave一起使用如下:
(add-hook'Rnw-mode-hook
()
(add-to-list'TeX-command-list
'(SweaveR CMD Sweave%s
TeX-run-command nil t:helpRun Sweave )t
(add-to-list'TeX-command-list
'(StangleR CMD Stangle%s
TeX-run-command nil t:help Run Stangle)t)
(setq TeX-command-defaultSw e)))
如果我使用 Cc Cc
然后选择 Sweave
, .tex
文件如下所示:
\documentclass [
NA
pagesize%将页面大小写入dvi
] {article}
\usepackage [美] {babel}
\usepackage {fancyvrb}
\usepackage [utf8] {inputenc}
\usepackage {Sweave}
\begin {document}
一些文本
\begin {Schunk}
\begin {Sinput}
> sessionInfo()
\end {Sinput}
\begin {Soutput}
R版本2.14.0(2011-10-31)
平台:x86_64-apple-darwin11 .2.0 / x86_64(64位)
locale:
[1] C
附加的基本包:
[1] stats graphics grDevices utils数据集方法base
通过命名空间加载(而不附加):
[1] tools_2.14.0
\end {Soutput}
\end {Schunk
\end {document}
如你所见,foo的第二行。包含非ASCII字符×
的.nnw替换为 NA
。我可以弄清楚,这与本地化有关,其中 R
运行(另见语言环境的输出:[1] C
我可以在我的 .bashrc $ c $中包含表单
export LC_MESSAGES =en_US.UTF-8
c>,以便 R
获得正确的 locale
,我也可以完美使用 R CMD Sweave来自终端的foo.Rnw
(当然这些加载 .bashrc
,从而设置 locale
正确)问题是:如何通过Emacs进行工作,以便我可以通过 Cc Cc Sweave
运行Sweave?我知道Emacs不知道在 .bashrc
中设置的变量,但是如何保证从Emacs运行发出的 R CMD Sweave
与我从终端执行相同的本地化?
我发现了这个问题的几个帖子,但是没有Mac和Emacs中的解决方案。再次注意到我可以完美地运行 R CMD Sweave foo.Rnw
f (由于我的设置在 .bashrc
) - 大多数帖子解决这个问题 - 它只是不工作在Emacs(这是非常讨厌)。
更新:
我的〜/ .bashrc
包含:
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
按照Yihui的意见,我在〜/ Rprofile
:
隐形(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))
这解决了这个问题。
最后的答案可以在 UPDATE 下找到。
@cbeleites:我不再运行Mac OS X(R 2-14),所以我不能告诉。
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.
这篇关于Emacs,R,Sweave:当通过Emacs启动Sweave时,无法识别UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!