6

GNU R 3.02

> bib <- "\cite"
Error: '\c' is an unrecognized escape in character string starting ""\c"
> bib <- "\\cite"
> print(bib)
[1] "\\cite"
> sprintf(bib)
[1] "\\cite"
> 

how can I print out the string variable bib with just one "\"?

(I've tried everything conceivable, and discover that R treats the "\\" as one character.)

I see that in many cases this is not a problem, since this is usually handled internally by R, say, if the string were to be used as text for a plot.

But I need to send it to LaTeX. So I really have to remove it.

I see cat does the trick. If cat could only be made to send its result to a string.

0

3 Answers 3

8

You should use cat.

bib <- "\\cite"
cat(bib)
# \cite

You can remove the ## and [1] by setting a few options in knitr. Here is an example chunk:

<<newChunk,echo=FALSE,comment=NA,background=NA>>=
bib <- "\\cite"
cat(bib)
@

which gets you \cite. Note as well that you can set these options globally.

Sign up to request clarification or add additional context in comments.

4 Comments

The author did use cat. Read to the end of the OP! :-)
@SimonO101 I missed that.
Thanks for the information about the options! Much appreciated!
Is there a way of doing this in an inline code section?
6

There is no backslash in the character element "\cite". The backslash is being interpreted as an escape and the two character "\c" is being interpreted as a cntrl-c. Except that is not a recognized character. See ?Quotes. The second version has only one backslash followed by 4 alpha characters. Count the characters to see this:

nchar("\\cite")
[1] 5

1 Comment

I see now that there's a typo in my message and have edited it. Apparenyly, stackoverflow treats "\\" as "\" when it creates output. Which is, in my experience, expected behavior.
3

OK,

<<echo=FALSE,result='asis'>>
result <- cat(rbib)
@

does the trick (without the result <- bit, [1] is added). It just feels kludgy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.