Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4095918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:58:06+00:00 2026-05-20T19:58:06+00:00

I’ve been searching for the past 2 days, and while I’ve found similar questions

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:58:07+00:00Added an answer on May 20, 2026 at 7:58 pm

    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}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm making a simple page using Google Maps API 3. My first. One marker
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.