12
votes

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.

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.

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

\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}

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.

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.

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

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.

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


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:

\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}

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

Some string \& some other string

4

4 Answers

8
votes

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}
\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}
7
votes

The Hmisc package has a latexTranslate function designed to prepare strings for Latex. From its helpfile:

latexTranslate translates particular items in character strings to LaTeX format, e.g., makes a^2 = a\$^2\$ for superscript within variable labels. LaTeX names of greek letters (e.g., "alpha") will have backslashes added if greek==TRUE. Math mode is inserted as needed. latexTranslate assumes that input text always has matches, e.g. [) [] (] (), and that surrounding by \$\$ is OK.

1
votes
0
votes

Or you could also use Sweave to make the table; when using tex output instead of Sexpr only a double backslash is needed, as my sanitize1 does here, or stresc does more completely.

You'd fill d in with your values (myReport$report_name instead of "My_Report_Name", for example) instead of the ones I made up.

<<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")
@