Sweave似乎没有得到.Rnw文件的编码权 [英] Sweave doesn't seem to get .Rnw file encoding right

查看:145
本文介绍了Sweave似乎没有得到.Rnw文件的编码权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题出自tex.sx上的以下问题: Sweave生成无效的LaTeX 。问题似乎是 Sweave 无法识别文件的编码,尽管区域设置为UTF-8,而 .Rnw 文件保存为UTF-8。最终结果是任何包含非ASCII字符的 .Rnw 文件最终在结果中生成NA .tex 文件。正如你可以在对该问题的评论中看到的,另一个用户不显示该问题,什么是显然是相同的设置。 (在Mac上为R 2.13.1)以下是失败的最小文档。



更新



根据Aaron的建议,我已将 sessionInfo 添加到 .Rnw 文件,现在是真实的问题揭示了自身。 Sweave 处理文件时,似乎更改区域设置。



.Rnw 文件

  \documentclass {article} 
\ usepackage [utf8] {inputenc}
\begin {document}
一些非ascii文本:éüáî
<>> =
sessionInfo $ b @
\end {document}

通过 Sweave ,生成以下 .tex 文件。包含非ASCII字符的行已通过 Sweave 转换为 NA 。貌似地区已更改:



结果 .tex 文件 / p>

  \documentclass {article} 
\usepackage [utf8] {inputenc}
\usepackage { Sweave}
\begin {document}
NA
\begin {Schunk}
\begin {Sinput}
> sessionInfo()
\end {Sinput}
\begin {Soutput}
R版本2.13.1(2011-07-08)
平台:x86_64-apple-darwin9 .8.0 / x86_64(64位)

locale:
[1] C

附加的基本软件包:
[1] stats graphics grDevices utils数据集方法base

通过命名空间加载(并未附加):
[1] tools_2.13.1
\end {Soutput}
\end {Schunk }
\end {document}

sessionInfo / code>返回:



> sessionInfo()
R版本2.13.1(2011-07-08)
平台:x86_64-apple-darwin9.8.0 / x86_64(64位)

locale:
[1] en_US.UTF-8 / en_US.UTF-8 / C / C / en_US.UTF-8 / en_US.UTF-8

更新(回应Aaron)

  ; text<  -  readLines(sweave-enc-test.Rnw,warn = FALSE)
> enc< - tools :::。getVignetteEncoding(text,convert = TRUE)
>
>文本
[1]\\ doc_name {article}\\usepackage [utf8] {inputenc}\\begin {document}
[4] non-ascii text:éüáî\\end {document}
> enc
[1]UTF-8
> iconv(text,enc,)
[1]\\documentclass {article}\\usepackage [utf8] {inputenc}\\begin {document}
[4]一些非ASCII文本:éüáî\\end {document}


$ b b

(这是 R.app 中R控制台内的输出。)

解决方案

潜在修正:



尝试



<$ p $ Te> 在您的TeXShop脚本中导出LANG = en_US.UTF-8





(原始想法在〜/ .bashrc 文件中,但显然TeXShop不会加载。 )



EARLIER:



> sessionInfo()在Rnw文件中?

  \documentclass {article} 
\usepackage [utf8] {inputenc}
\begin {document}
一些非ASCII文本:éüáî
<>> =
sessionInfo
@
\end {document}


This question arose out of the following question on tex.sx: Sweave generating invalid LaTeX. The problem seems to be that Sweave is not recognizing the encoding of the file, despite the locale being set to UTF-8, and the .Rnw file being saved as UTF-8. The end result is that any .Rnw file that contains non-ASCII characters ends up producing NA in the resultant .tex file. As you can read in the comments to that question, another user doesn't show the problem, with what is apparently an identical setup. (R 2.13.1 on a Mac) Here's a minimal document that fails.

Update

Based on Aaron's suggestions, I've added sessionInfo to the .Rnw file, and now the real problem reveals itself. When Sweave processes the file, it seems to change the locale.

.Rnw file

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
Some non-ascii text: éüáî
<<>>=
sessionInfo()
@ 
\end{document}

Running this through Sweave, produces the following .tex file. The line containing the non-ASCII characters has been converted into NA by Sweave. It seems also that the locale has been changed:

Resultant .tex file

\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() from within R.app returns:

> 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

Update (Response to Aaron)

> 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}"      

(This is the output from within the R console in R.app.)

解决方案

Potential fix:

Try putting

export LANG=en_US.UTF-8

in your TeXShop script.

(Original idea was in the ~/.bashrc file, but apparently TeXShop doesn't load that.)

EARLIER:

What happens when you put sessionInfo() in the Rnw file?

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
Some non-ascii text: éüáî
<<>>=
sessionInfo()
@ 
\end{document}

这篇关于Sweave似乎没有得到.Rnw文件的编码权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆