我一直在搜索过去的两天,虽然我在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}
Run Code Online (Sandbox Code Playgroud)
问题是,myReport $ courseInfo $ shortname具有需要为LaTeX转义的值,因为它包含诸如&的字符(这会强制LaTeX抛出关于表列的错误).我试图包括seqinr库,并在整个数据对象上使用stresc,但生成的.tex文件仍显示unslashed&from shortname.
我还不完全熟悉R,但是在使用模板时,我发现甚至不需要上面的"print()"调用,因为只需在\ Sexpr中直接指定变量就会产生打印值,但是在.tex中记录时,我的转义值仍未转义.
我还尝试将stresc直接放在\ Sexpr(而不是print)中,没有区别.
所以似乎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) { …Run Code Online (Sandbox Code Playgroud)