R代码输出中的特殊字符未打印成PDF(虽然图形内容很好)

nik*_*nen 5 pdf r rstudio knitr

最后的工作示例

我想使用knitr在LaTeX文档中包含R代码,但是当存在非ascii字符时,它无法正确打印到PDF中.情况类似于用户unikumGitHub中提出的问题,但在那里给出的解决方案仅部分地帮助了我.

理想情况下,打印的行将在普通文本中并具有合适的LaTeX格式,特别是在结果只是一行的情况下.在我的脑海中,我有一些用途,研究论文中的例句会自动从数据文件中获取.

我在RStudio(0.98.945)中编写了Rnw文件,我使用XeLaTex进行排版.我只需单击RStudio中的Compile PDF按钮即可创建PDF .我有MacBook Pro和Mavericks.如果需要一些其他信息,请告诉我.如果有帮助,我也可以切换到其他程序(如TeXShop等).

我还在学习所有这些工具,R,knitr和LaTeX,所以如果我错过任何明显的解决方案或滥用某些术语,我会道歉.我试图将类似问题的不同解决方案结合起来,比如关于波兰人或者希伯来语的问题.有很多人在撰写有关类似问题的文章,但我仍然有问题要将它们结合在一起.我猜这是编码问题.

这是我的LaTeX序言:

\documentclass{article}
\usepackage{hyperref}
\usepackage {fontspec}
\setromanfont{Charis SIL}

<<setup, include=FALSE>>=
options(device = function(file, width = 7, height = 7, ...) {
cairo_pdf(tempfile(), width = width, height = height, ...)
})
@
Run Code Online (Sandbox Code Playgroud)

在普通文本中,我可以有任何可能的unicode字符,它们在pdf中显示正确:мичаалыддьыныпозьӧ,met'šužišurse̮kmis-šodasɛ͔dvoɛ͔šent'a·b das-e̮kmi͔sɛ͔dlunɛ.

我认为这与编码有关.

在我的例子中,我扫描到包含以下10行的文本文件(test.txt),它们只是芬兰语和科米语中的随机单词形式:

??????????
?????????
??????
sivulla
???
???????
sivu-ovi
Esko
???????
akkuna
Run Code Online (Sandbox Code Playgroud)

以下代码正确打印结果:

<<test1, echo=FALSE, comment=NA>>=
test <- scan(file="test.txt", what="char", sep="\n")
wordlat <- grep("sivu", test, value=T)
print(wordlat)
@
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

[1] "sivulla"  "sivu-ovi"
Run Code Online (Sandbox Code Playgroud)

但这不是:

<<test2, echo=FALSE, comment=NA>>=
test <- scan(file="test.txt", what="char", sep="\n")
wordcyr <- grep("???????", test, value=T)
print(wordcyr)
@
Run Code Online (Sandbox Code Playgroud)

出来的是:

[1] "" ""
Run Code Online (Sandbox Code Playgroud)

我稍微修改了发布到GitHub的示例.我只是想在标签上同时使用拉丁语和西里尔语.正如人们所料,将cairo_pdf添加到开头有助于:

<<pie-plot, dev='cairo_pdf'>>=
x <- gl(3, 4, 50, labels=c("Pretty easy!", "???? ??????????!", "????? ????? :("))
pie(table(x), main="How difficult is this question")
@
Run Code Online (Sandbox Code Playgroud)

馅饼出来很完美,但显然仍有问题,因为上面的代码说:

x<-gl(3,4,50,labels=c("Pretty easy!"," !","  :("))pie(table(x),main="How difficult is this question")
Run Code Online (Sandbox Code Playgroud)

工作示例:

我现在看到我应该更加注意提供一段可以复制和运行的代码.我为此道歉.所以下面的示例与我原来的帖子相同,但它可以复制到RStudio中,它应该制作一页PDF,它有我描述的问题.很抱歉重复相同,但我可能会让我的问题更加不清楚,如果我开始编辑整个帖子.

\documentclass{article}
\usepackage {fontspec}
\setromanfont{Charis SIL}

\begin{document}

<<setup, include=FALSE>>=
options(device = function(file, width = 7, height = 7, ...) {
cairo_pdf(tempfile(), width = width, height = height, ...)
})
@

So this comes out fine.

<<test1, echo=FALSE, comment=NA>>=
test1 <- c("??????????", "?????????", "??????", "sivulla", "???", "???????", "sivu-ovi", "Esko", "???????", "akkuna")
wordlat <- grep("sivu", test1, value=T)
print(wordlat)
@

But for some reason this doesn't. How to make it show characters correctly, that's the whole question I posted about.

<<test2, echo=FALSE, comment=NA>>=
test2 <- c("??????????", "?????????", "??????", "sivulla", "???", "???????", "sivu-ovi", "Esko", "???????", "akkuna")
wordcyr <- grep("????", test2, value=T)
print(wordcyr)
@

The graphical things come out fine with cairo-package, but only within the graph itself:

<<pie-plot, dev='cairo_pdf'>>=
x <- gl(3, 4, 50, labels=c("Pretty easy!", "???? ??????????!", "????? ????? :("))
pie(table(x), main="How difficult is this question")
@

The code above the graph in the PDF has the same problem as in earlier example which contained cyrillic:

c("Pretty easy!"," !","  :("))

\end{document}
Run Code Online (Sandbox Code Playgroud)

感谢帮助!