4

As stated in the Intro to R manual,

paste("\\")

prints

[1] "\\"

Is it possible for paste to print out

[1] "\"

?

update: I didn't want Gavin's this nice answer to get stuck in the comments below, so I'll paste it here:

print(xtable(as.matrix("\\citep{citation}")), sanitize.text.function = function(x) {x}) 
David LeBauer
  • 31,011
  • 31
  • 115
  • 189
  • 1
    What is your real question? Would `file.path()` help? – Dirk Eddelbuettel Dec 07 '10 at 17:40
  • Do you want `print()` -ed output or would something like `cat()` or `writeLines()` do? – Gavin Simpson Dec 07 '10 at 17:47
  • @Dirk I am trying to create a vector from a vector of bibtex citations that returns "\citep{citation}" from "citation", like paste("\citep{", citation, "}", sep='') @Gavin, presumably cat() would work, but I was using paste() since it returns a vector instead of a string from a vector; I could use an apply statement though – David LeBauer Dec 07 '10 at 17:50
  • @David: no, what I mean't was, does it actually matter if the printed output contains `"\\"`. If you cat this out, it will contain only a single `\`. Are you doing this in any particular context? A Sweave file?? – Gavin Simpson Dec 07 '10 at 18:12
  • @Gavin - yes, I am using this to add citatons to a data frame that will become a table column via xtable in a Sweave file. – David LeBauer Dec 07 '10 at 18:20
  • 1
    @David, then see my answer. IIRC you'll need to have ` \\ ` in there for xtable. Just ignore how R *prints* the vector in the console, xtable will spit it out via `cat()` or similar and the second ` \ ` will miraculously disappear. – Gavin Simpson Dec 07 '10 at 18:24
  • @Gavin xtable(as.matrix("\\citep{citation}")) returns $\backslash$citep\{citation\} in the cell where I want \citep{citation} – David LeBauer Dec 07 '10 at 18:38
  • 1
    @David: read the help for `print.xtable`, you are getting hit by the print method sanitizing the text to remove important LaTeX characters. The Xtable Gallery vignette has examples of how to handle sanitization, such as this: `print(xtable(as.matrix("\\citep{citation}")), sanitize.text.function = function(x) {x})` – Gavin Simpson Dec 07 '10 at 18:46
  • @Gavin thanks for pointing that out - I would have been completely lost. – David LeBauer Dec 07 '10 at 19:45

3 Answers3

6

You are confusing how something is stored and how it "prints".

You can use paste to combine a \ with something else, but if you print it then the printed representation will have \ to escape the \, but if you output it to a file or the screen using cat instead, then you get the single \, for example:

> tmp <- paste( "\\", "cite{", sep="" )
> print(tmp)
[1] "\\cite{"
> cat(tmp, "\n")
\cite{ 
zwol
  • 135,547
  • 38
  • 252
  • 361
Greg Snow
  • 48,497
  • 6
  • 83
  • 110
5

That is the printed representation of a single "\" in R. Clearly the right answer will depend on your end usage, but will something like this do:

> citations <- paste("title", 1:3, sep = "")
> cites <- paste("\\citep{", citations, "}", sep = "")
> writeLines(cites)
\citep{title1}
\citep{title2}
\citep{title3}

Using writeLines() you can output that to a file using something like:

> writeLines(cites, con = file("cites.txt"))

Resulting in the following file:

$ cat cites.txt 
\citep{title1}
\citep{title2}
\citep{title3}
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
1

One way to do is is to use the write command, e.g.

> write("\\", file="")
\

Write is usually used to write to files, so you need to set file="" to get it to print to STDOUT.

The \ is repeated in the write command so that it doesn't escape the closing quotation mark.

I'm not sure if this is the correct way to do it, but it works for me.

Edit: Realised slightly too late that you were using the paste() command. Hopefully my answer still bears some relevance to your plight. Apologies.

matt
  • 163
  • 8