12

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

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

在 Rnw 中,存在如下代码:

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

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

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

我还尝试将 stresc 直接放在 \Sexpr 中(而不是打印),没有区别。

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

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


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

# 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);
}

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

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

因此,带有 & 的字符串现在正确显示在生成的 LaTeX 文件中:

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

4

4 回答 4

8

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

\documentclass{article}
\begin{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}
于 2011-03-23T15:08:17.190 回答
7

Hmisc包具有latexTranslate旨在为 Latex 准备字符串的功能。从它的帮助文件中:

latexTranslate 将字符串中的特定项目转换为 LaTeX 格式,例如,将 a^2 = a\$^2\$ 用于变量标签内的上标。如果 greek==TRUE,希腊字母的 LaTeX 名称(例如,“alpha”)将添加反斜杠。根据需要插入数学模式。latexTranslate 假设输入文本总是有匹配的,例如 [) [] (] (),并且由 \$\$ 包围是可以的。

于 2011-03-23T16:45:02.690 回答
1

\verb命令可以帮助您吗?

于 2011-03-23T13:58:24.593 回答
0

或者你也可以使用 Sweave 来制作表格;当使用tex输出而不是Sexpr只需要一个双反斜杠时,就像我sanitize1在这里所做的那样,或者stresc更完整地做。

您将填写d您的值(例如,而不是 ),而不是我编造的myReport$report_name值。"My_Report_Name"

<<echo=FALSE, results=tex>>=
sanitize1 <- function(str) {
  result <- str
  result <- gsub("&", "\\&", result, fixed = TRUE)
  result <- gsub("_", "\\_", result, fixed = TRUE)
  result
}
d <- rbind(c("Report name:","My_Report_Name"),
           c("Report date:","\\today"),
           c("Course name:","STAT101"),
           c("Start date:", "23 Mar 2011"),
           c("Instructor:", "Aaron & Jon L."))
cat("\\begin{tabular}{lll}\n")
for(i in 1:nrow(d)) {
  cat("& {\\textbf{",d[i,1],"}} &", sanitize1(d[i,2]), "\\\\\n", sep="")
}
cat("\\end{tabular}\n")
@ 
于 2011-03-23T15:53:41.690 回答