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

David Epstein David.Epstein at warwick.ac.uk
Mon Sep 2 21:18:21 CEST 2013


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.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
> Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
> The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
> 



More information about the R-help mailing list