[R] Sweave, backslash in sub pattern? was: Backslash in sub pattern?
Marc Schwartz
marc_schwartz at comcast.net
Mon Jul 14 21:04:50 CEST 2008
on 07/14/2008 01: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")}
>
> After Sweave(foo) the file foo.tex then contains:
> \usepackage{Sweave}
> DV~IV
>
> \Sexpr{sub('~','$\\\\sim$',"DV~IV")}
> \Sexpr{cat(sub('~','$\\\\sim$',"DV~IV"),"\n")}
>
> (yes, with the blank line resulting from \Sexpr{cat("DV~IV")}), and
> LaTeX understandably complains that \Sexpr is not a valid command.
>
> My goal: I would like to use some function bar() so that
>
> str <- "DV~IV"
> \Sexpr{bar(str)}
>
> yields after Sweaving
>
> DV$\sim$IV
>
> in the tex file.
>
> Any thoughts?
> Stephan
>
>
> [1] Sorry, I now see that my commented, minimal, self-contained,
> reproducible code was too minimal... I reduced the problem too far. My bad!
>
>
>
> Phil Spector schrieb:
>> Stephen -
>> Since the print function in R always displays two backslashes
>> for a single one, you really need to use cat or nchar to make sure
>> that things are working:
>>
>>> sub('~','$\\\\sim$',str)
>> [1] "ab$\\sim$cd"
>>> cat(sub('~','$\\\\sim$',str),"\n")
>> ab$\sim$cd
>>
>> It's the result of cat that's actually output, not the string that
>> R uses to display it.
>>
>>
>> - Phil Spector
>> Statistical Computing Facility
>> Department of Statistics
>> UC Berkeley
>> spector at stat.berkeley.edu
>>
>>
>> On Mon, 14 Jul 2008, 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
>>>
>>>
>>>> sessionInfo()
>>> R version 2.7.1 (2008-06-23)
>>> i386-pc-mingw32
>>>
>>> locale:
>>> LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252
>>>
>>>
>>> attached base packages:
>>> [1] stats graphics grDevices utils datasets tcltk
>>> methods base
>>>
>>> other attached packages:
>>> [1] svIDE_0.9-5
>>>
>>> loaded via a namespace (and not attached):
>>> [1] svMisc_0.9-5
Stephan,
Try running Sweave on the file without SciViews loaded. There have been
some posts over the past few years, with one relatively recently,
indicating that there can be package conflicts resulting in \Sexpr{}'s
not being parsed properly. The key package in question in the past has
been R2HTML, but perhaps svIDE et al are the culprits here. The conflict
is due to an alternative or supplemental Sweave driver being loaded.
See FAQ A.18 here:
http://www.statistik.lmu.de/~leisch/Sweave/FAQ.html
It may also be that your code within the \Sexpr{} is overly complicated,
in which case, move the text processing outside the \Sexpr and then just
use the result within it:
Res <- sub('~','$\\\\sim$',"DV~IV")
\Sexpr{Res}
HTH,
Marc Schwartz
More information about the R-help
mailing list