R,Sweave,LaTeX - 要在 LaTeX 中打印的转义变量? [英] R, Sweave, LaTeX - escape variables to be printed in LaTeX?

查看:21
本文介绍了R,Sweave,LaTeX - 要在 LaTeX 中打印的转义变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去 2 天我一直在搜索,虽然我在 Stack Overflow 和 Google 上的其他讨论中发现了类似的问题,但没有找到符合我要求的内容.

I've been searching for the past 2 days, and while I've found similar questions on Stack Overflow and other discussions on Google, I've found nothing matching my request.

我有一个我支持的预先存在的应用程序,它是围绕 R 构建的.Sweave Rnw 模板文件用于生成 .tex 文件,这些文件用于生成 .pdf 文件.

I have a pre-existing application that I'm supporting, which is built around R. Sweave Rnw template files are used to generate .tex files, which are used to generate .pdf files.

在 Rnw 中,存在如下代码:

Within the Rnw, there is existing code such as this:

egin{tabular}{lll}
& {f 	extcolor{TitlesColor}{Report name:}} & Sexpr{print(myReport$report_name)}\
& {f 	extcolor{TitlesColor}{Report date:}} & 	oday \
& {f 	extcolor{TitlesColor}{Course name:}} & Sexpr{print(myReport$courseInfo$shortname)}\
& {f 	extcolor{TitlesColor}{Start date:}}  & Sexpr{print(myReport$courseInfo$startdate_readable)}\
& {f 	extcolor{TitlesColor}{Instructor:}} & Sexpr{print(paste(myReport$instructor$lastname, collapse="| "))}\
end{tabular}

问题是,myReport$courseInfo$shortname 具有需要为 LaTeX 转义的值,因为它包含诸如 & 之类的字符.(这迫使 LaTeX 抛出关于表格列的错误).我尝试包含 seqinr 库,并在整个数据对象上使用 stresc,但生成的 .tex 文件仍然显示不带斜线的 &来自短名称.

The issue is, myReport$courseInfo$shortname has values that need to be escaped for LaTeX, as it includes characters such as & (which forces LaTeX to toss an error about the table columns). I've attempted including the seqinr library, and using stresc on the entire data object, but still the generated .tex file shows an unslashed & from shortname.

我还不是完全熟悉 R,但是在使用模板时,我发现甚至不需要上面对print()"的调用,因为只需在 Sexpr 结果中直接指定变量在打印值中,但在 .tex 中记录时,我的转义值仍然未转义.

I'm not wholly familiar with R yet, but in playing around with the template, I've found that the calls to "print()" above are not even needed, as just specifying the variables directly within Sexpr results in printed values, but still my escaped values are unescaped when recorded in the .tex.

我还尝试将 stresc 直接放在 Sexpr 中(而不是 print),没有区别.

I've also attempted to place stresc directly within the Sexpr (instead of print), with no difference.

所以看起来 R/Sweave 自己的过程正在去除斜线,这意味着我可能需要双斜线值,但我对 R 不够熟悉,不知道如何做到这一点.

So it seems that R/Sweave's own process is stripping the slashes, which means I likely need to double-slash values, but I'm not familiar enough with R to know how to do that.

将动态数据打印到 .tex 文件中的正确方法是什么?

What is the proper way to print dynamic data into a .tex file?

更新:根据@Aaron 的回复,这是我创建的函数:

UPDATE: Based on @Aaron's response, here is the function I created:

# Sanitizes variables for displaying within LaTeX via Sexpr
# Adds slashes to LaTeX special characters, which results in single-slash in tex output
sanitizeLatexS <- function(str) {
    gsub('([#$%&~_\^\\{}])', '\\\\\1', str, perl = TRUE);
}

我的更新模板(如我上面的原始帖子中所引用的)现在看起来像这样:

My updated template (as referenced in my original post above) now looks like this:

egin{tabular}{lll}
& {f 	extcolor{TitlesColor}{Report name:}} & Sexpr{sanitizeLatexS(myReport$report_name)}\
& {f 	extcolor{TitlesColor}{Report date:}} & 	oday \
& {f 	extcolor{TitlesColor}{Course name:}} & Sexpr{sanitizeLatexS(myReport$courseInfo$shortname)}\
& {f 	extcolor{TitlesColor}{Start date:}}  & Sexpr{sanitizeLatexS(myReport$courseInfo$startdate_readable)}\
& {f 	extcolor{TitlesColor}{Instructor(s):}} & Sexpr{sanitizeLatexS(paste(myReport$instructorList$lastname, collapse="| "))}\
end{tabular}

所以一个带有 & 的字符串现在在生成的 LaTeX 文件中正确显示为:

So a string with an & now properly shows up in the generated LaTeX file as:

一些字符串&其他一些字符串

Some string & some other string

推荐答案

你可以使用 verb 或者你可以插入一个四重反斜杠,这是在最终的 tex 文件中获取一个反斜杠所必需的,因为它经过两次打印操作,将双"转换为单".

You can either use verb or you can insert a quadruple backslash, which is needed to get one in the final tex file because it goes through two printing operations which convert a double "" to a single "".

documentclass{article}
egin{document}
<<echo=FALSE, results=hide>>=
sanitize <- function(str) {
  result <- str
  result <- gsub("&", "\\&", result, fixed = TRUE)
  result <- gsub("_", "\\_", result, fixed = TRUE)
  result
}
@ 

<<>>=
(foo <- "test & _")
sanitize(foo)
@ 

My string is ``verb+Sexpr{foo}+''.  When sanitized, it's ``Sexpr{sanitize(foo)}''.

end{document}

这篇关于R,Sweave,LaTeX - 要在 LaTeX 中打印的转义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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