Re: [R] Backslash in paste() function

From: Nikhil Kaza <nikhil.list_at_gmail.com>
Date: Wed, 16 Jun 2010 23:56:25 -0400

On Jun 16, 2010, at 9:23 AM, Stefan Petersson wrote:

>>
>> 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.

I suppose what Uwe was saying is that you should use

> a <- paste("\\parbox[b]{3cm}{", test, "}", fill=TRUE)   cat(a, file="out.tex")

>
> ______________________________________________
> 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.

______________________________________________
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. Received on Thu 17 Jun 2010 - 03:58:59 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 Thu 17 Jun 2010 - 04:20:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive