这个问题源于 tex.sx 上的以下问题:Sweave generate invalid LaTeX。问题似乎Sweave
是无法识别文件的编码,尽管语言环境设置为 UTF-8,并且.Rnw
文件被保存为 UTF-8。最终结果是任何.Rnw
包含非 ASCII 字符的文件最终都会在结果.tex
文件中产生 NA。正如您在对该问题的评论中看到的那样,另一个用户没有显示问题,显然是相同的设置。(Mac 上的 R 2.13.1)这是一个失败的最小文档。
更新
根据 Aaron 的建议,我已将其添加sessionInfo
到.Rnw
文件中,现在真正的问题暴露了。处理文件时Sweave
,似乎更改了语言环境。
.Rnw
文件
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
Some non-ascii text: éüáî
<<>>=
sessionInfo()
@
\end{document}
运行它Sweave
,生成以下.tex
文件。包含非 ASCII 字符的行已被转换NA
为Sweave
. 似乎语言环境也已更改:
结果.tex
文件
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{Sweave}
\begin{document}
NA
\begin{Schunk}
\begin{Sinput}
> sessionInfo()
\end{Sinput}
\begin{Soutput}
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin9.8.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.13.1
\end{Soutput}
\end{Schunk}
\end{document}
sessionInfo()
从内部R.app
返回:
> sessionInfo()
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
更新(对亚伦的回应)
> text <- readLines("sweave-enc-test.Rnw", warn = FALSE)
> enc <- tools:::.getVignetteEncoding(text, convert = TRUE)
>
> text
[1] "\\documentclass{article}" "\\usepackage[utf8]{inputenc}" "\\begin{document}"
[4] "Some non-ascii text: éüáî" "\\end{document}"
> enc
[1] "UTF-8"
> iconv(text, enc, "")
[1] "\\documentclass{article}" "\\usepackage[utf8]{inputenc}" "\\begin{document}"
[4] "Some non-ascii text: éüáî" "\\end{document}"
(这是 R 控制台中的输出R.app
。)