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

From: Duncan Murdoch <>
Date: Mon, 14 Jul 2008 14:53:57 -0400

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 mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 14 Jul 2008 - 18:57:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 Jul 2008 - 19:31:29 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive