[R] Sweave, backslash in sub pattern? was: Backslash in sub pattern?

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jul 14 20:53:57 CEST 2008


On 7/14/2008 2:39 PM, Stephan Kolassa wrote:
> Phil:
> 
> thanks a lot! However, my original problem[1] remains: in fact, I am 
> trying to \Sexpr the result for Sweave, and for some reason Sweave 
> regurgitates the \Sexpr command unchanged:
> 
> foo.Rnw contains:
> \Sexpr{"DV~IV"}
> \Sexpr{cat("DV~IV")}
> \Sexpr{sub('~','$\\\\sim$',"DV~IV")}
> \Sexpr{cat(sub('~','$\\\\sim$',"DV~IV"),"\n")}
> 

The \Sexpr{} form is limited in what it can handle; it's probably 
getting confused by all those escapes.  Here's a way to do it that works 
for me:

<<>>=
Fixtilde <- function(x) sub('~','$\\\\\\\\sim$',x)
@

Here it is:  \Sexpr{Fixtilde("DV~IV")}.


I haven't traced through the code to see why I need 8 escapes so that 
one makes it through to the .tex file, but that's what appears to work.

Duncan Murdoch



More information about the R-help mailing list