[R] switching off commands within character vector

Duncan Murdoch murdoch.duncan at gmail.com
Mon Sep 19 13:37:32 CEST 2011

On 11-09-19 7:30 AM, Marion Wenty wrote:
> Hello,
> could someone help me with this problem?:
> I would like to create a latex-script inside of a character vector in order
> to being able to compilate it within latex in the end.
> if i try the following commands:
> l1<- "Hello world"
> latexscript<- paste("\c",l1,"\c")
> ... I get an error message. I think the problem is that r sees \c as a
> command.
> How can I get
> "\c Hello world \c"
> as a result without getting the error message?

Here's the long answer:

R uses \ as an "escape character", which is different from the way LaTeX 
uses it.  The string "\n" contains a single character (a newline 
character.  When you say "\c", the parser thinks you are asking for a 
single character which is written as \c, but there isn't one, so you get 
the error message.

To put the characters \c into a string, you need to escape the 
backslash, i.e. use "\\c".  That's a two character string.  It will 
print as "\\c" using print(), but if you use cat() you will see the 
actual characters \c.

The short answer is:

latexscript <- paste("\\c", l1, "\\c")
cat(latexscript, "\n")

Duncan Murdoch

More information about the R-help mailing list