[R] Backslash in paste() function

Stefan Petersson stefan.petersson at inizio.se
Wed Jun 16 15:23:50 CEST 2010


> 
> Just double all the backslashes and you are fine.
> In order to see the outcome, use cat() (not print).
> 
> Uwe Ligges
> 
> On 16.06.2010 09:49, Stefan Petersson wrote:
> >
> > Hi,
> >
> > I'm trying to build a vector of latex commands. However, I need the command
strings to begin with a
> backslash "\". I have:
> >
> > test<- c('foo','bar')
> >
> > and I need to rebuild the array, encapsulating the text items with latex
stuff, like this:
> >
> > paste("\parbox[b]{3cm}{", test, "}", fill=TRUE)
> >
> > Actually, cat() prints the string fine if one uses double backslashes.
However, I can't save the result
> from cat() back to a vector. And when I use cat() inside the latex function
from the Hmisc package, I only get
> errors... The (not so) funny thing is that I can use paste() on a few latex
commands. So if I needed to make the
> strings bold, the following works:
> >
> > paste("\bfseries{", test, "}")
> >
> > I've read the R FAQ 7.37, but it only deals with cat(). Not paste. Or I just
didn't get it :o/
> >
> > Is there a way to paste() the backslash character?
> >
> >> sessionInfo()
> > R version 2.11.1 (2010-05-31)
> > i486-pc-linux-gnu
> >
> > locale:
> >   [1] LC_CTYPE=en_US.utf8       LC_NUMERIC=C
> >   [3] LC_TIME=en_US.utf8        LC_COLLATE=en_US.utf8
> >   [5] LC_MONETARY=C             LC_MESSAGES=en_US.utf8
> >   [7] LC_PAPER=en_US.utf8       LC_NAME=C
> >   [9] LC_ADDRESS=C              LC_TELEPHONE=C
> > [11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C
> >
> > attached base packages:
> > [1] stats     graphics  grDevices utils     datasets  methods   base
> >>
> >
> > ______________________________________________
> > 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.
> 
> 

Sorry... no love. Double backslashes doesn't cut it. Double backslashes prints
nothing. Single backslashes removes the first character of my command. But no
single backslash!

  paste("\parbox[b]{3cm}{", test, "}", fill=TRUE)

Gives me:

  [1] arbox[b]{3cm}{ foo }
  [2] arbox[b]{3cm}{ bar }


and 
    paste("\\parbox[b]{3cm}{", test, "}", fill=TRUE)

Gives me:

  [1] parbox[b]{3cm}{ foo }
  [2] parbox[b]{3cm}{ bar }

No backslash.



More information about the R-help mailing list