Adding a single backslash (\) to a string in R

Viewed 5171

I am working on knitr right now, One of the string that I was trying to print has the % symbol. I read up on the TeX manual that to a print the % symbol. TeX should have the string as \% to display the code.

Using the command gsub I tried to do the following:

try = "0% success"
try2 = gsub("%","\%",try,fixed = TRUE)

gsub throws an invalid escape character error

If I do this:

try = "0% success
try2 = gsub("%","\\\\%",try,fixed = TRUE)

knitr will still not be able to print the string

Please suggest me with a way that I can solve my problem

--------EDIT------------------

I want the string in try to displayed as it is in my output PDF I am using this in knitr as \textit{\Sexpr{try}} When the tex file gets created, it believes that the text after percentage is a comment. I am creating an rnw file first and then creating the tex file, after which using texi2pdf to get the pdf file

----EDIT 2 -------------

I am not very good with either knitr or LaTeX

2 Answers
Related