3

这个问题源于 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 字符的行已被转换NASweave. 似乎语言环境也已更改:

结果.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。)

4

1 回答 1

2

潜在修复:

试着放

export LANG=en_US.UTF-8

在您的 TeXShop 脚本中。

(最初的想法在~/.bashrc文件中,但显然 TeXShop 没有加载它。)

早期:

当您放入sessionInfo()Rnw 文件时会发生什么?

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
Some non-ascii text: éüáî
<<>>=
sessionInfo()
@ 
\end{document}
于 2011-09-22T14:38:17.117 回答