[R] Sweave: printing an underscore in the output from an R command

Yihui Xie xie at yihui.name
Mon Sep 2 22:11:48 CEST 2013


I think Thierry meant gsub("_", "\\\\_", version$platform); he just
typed too quickly. The point is to escape _ using \, but then people
are often trapped in the dreams of dreams of dreams of backslashes
like the movie Inception. And then due to a long-standing bug in
Sweave for \Sexpr{} (sorry I forgot to report to R core), you will be
so confused that you can never wake up and come back to the reality.

Dream level 1: when you need a backslash in a character string, you
need "\\", which really means \; you think "\\_" should be good, but
no --

Dream level 2: when you need one literal \ in a regular expression as
the replacement expression, you need \\

Combine the two levels of dreams, you end up with "\\\\_". \\\\ in R
really means \\, which really means \ in regular expressions.

Now you are good at the regular expression level, but Sweave comes and
bites you, and that is due to this bug in the regular expression in
Sweave Noweb syntax:

> SweaveSyntaxNoweb$docexpr
[1] "\\\\Sexpr\\{([^\\}]*)\\}"

It should have been "\\\\Sexpr\\{([^}]*)\\}", i.e. } does not need to
be escaped inside [], and \\ will be interpreted literally inside [].
In your case, Sweave sees \ in \Sexpr{}, and the regular expression
stops matching there, and is unable to see } after \, so it believes
there is no inline R expressions in your document.

BTW, knitr does not have this bug and works well in your case:

\documentclass{article}
\begin{document}
\Sexpr{sub("_", "\\\\_", version$platform)}
\end{document}

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Mon, Sep 2, 2013 at 2:18 PM, David Epstein
<David.Epstein at warwick.ac.uk> wrote:
> Dear Thierry,
>
> Your suggestion doesn't work on my version of R. Here's what I get
>> gsub("_", "\_", print(version$platform)
> Error: '\_' is an unrecognized escape in character string starting ""\_"
>> print(gsub("_", "\_", version$platform))
> Error: '\_' is an unrecognized escape in character string starting ""\_"
>
>> sub("_", "\\_", version$platform)
> [1] "x86_64-apple-darwin10.8.0"
> Sweave does not evaluate this expression when \Sexpr is applied and a tex error results
>
>> sub("_", "\\\_", version$platform)
> Error: '\_' is an unrecognized escape in character string starting ""\\\_"
> Error message from R
>
>> sub("_", "\\\\_", version$platform)
> [1] "x86\\_64-apple-darwin10.8.0"
> R evaluates this. However, the above examples indicate a deficiency/possible bug in the command sub, because sub does not seem to be able to output an expression with a single backslash.
>
> I tried the previous version as follows in my .Rnw document
> \Sexpr{print(sub("_", "\\\\_", version$platform))}
> When Sweave is run, this expression is evaluated to illegal LaTeX
>
> David.
>
>
>
>
> On 2 Sep 2013, at 16:47, ONKELINX, Thierry wrote:
>
>> You have to escape the underscore
>>
>> \Sexpr{gsub("_", "\_", print(version$platform))}
>>
>> Best regards,
>>
>> Thierry
>>
>> ________________________________________
>> Van: r-help-bounces at r-project.org [r-help-bounces at r-project.org] namens David Epstein [David.Epstein at warwick.ac.uk]
>> Verzonden: maandag 2 september 2013 17:38
>> Aan: r-help at r-project.org
>> Onderwerp: [R] Sweave: printing an underscore in the output from an R command
>>
>> I am working with Sweave and would like to print out into my latex document the result of the R command
>> version$platform
>> So what I first tried in my .Rnw document was \Sexpr{print(version$platform)}.
>>
>> However, the output from this command is the string "x86_64-apple-darwin10.8.0" (without the quotes). This contains an underscore, which is a special character in tex and so I get an error message from latex.
>>
>> I can get round this by using sub to replace underscore with a space, but I would like to know how to print the underscore if I really wanted to do so.



More information about the R-help mailing list