[R] Backslash in sub pattern?

Marc Schwartz marc_schwartz at comcast.net
Mon Jul 14 20:23:34 CEST 2008


on 07/14/2008 01:09 PM Stephan Kolassa wrote:
> Dear guRus,
> 
> I am trying to replace "~" by "$\sim$" for TeX. However, I can't get the 
> backslash to work. I would like to turn "DV~IV" into "DV$\sim$IV".
> 
> sub("~","$\sim$","DV~IV") => "DV$sim$IV"
> sub("~","$\\sim$","DV~IV") => "DV$sim$IV"
> sub("~","$\\\sim$","DV~IV") => "DV$sim$IV"
> sub("~","$\\\\sim$","DV~IV") => "DV$\\sim$IV"
> 
> Alternatives 1 and 3 also yield warnings about unknown escape sequences 
> (no surprise there). I have been unsuccessfully searching the archives. 
> sessionInfo below.
> 
> Thank you all for your time!
> 
> Best regards
> Stephan

You want door number 4 because when the double escape is cat()'d to the 
.tex file, you get:

 > cat("DV$\\sim$IV", "\n")
DV$\sim$IV

which is what you want latex to process.

HTH,

Marc Schwartz



More information about the R-help mailing list