gpt4 book ai didi

r - Sweave 似乎没有正确获得 .Rnw 文件编码

转载 作者:行者123 更新时间:2023-12-04 09:42:04 25 4
gpt4 key购买 nike

这个问题源于 tex.sx 上的以下问题:Sweave generating invalid LaTeX .问题似乎是 Sweave 无法识别文件的编码,尽管语言环境设置为 UTF-8,并且 .Rnw 文件被保存为 UTF- 8.最终结果是任何包含非 ASCII 字符的 .Rnw 文件最终都会在生成的 .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 字符的行已通过 Sweave 转换为 NA。似乎语言环境也已更改:

生成的 .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}
R.app 中的

sessionInfo() 返回:

> 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.app 中 R 控制台的输出。)

最佳答案

潜在修复:

尝试放置

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}

关于r - Sweave 似乎没有正确获得 .Rnw 文件编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7509395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com