7

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

6
  • Could you please elaborate your workflow? Are we to understand that these commands are to be included in a knitr chunk? What is the desired output in LaTeX? Commented Dec 27, 2013 at 12:27
  • I want the string to be displayed as it is, my output pdf that I am generating should have the string "0% success" printed the way it is. But when knitr encounters the % it believes the string to be a comment, I am using this in knitr as \textit{\Sexpr{try}} Commented Dec 27, 2013 at 12:30
  • You still haven't clarified. There are many paths from Knitr to LaTeX. Is it via Rmd? Or Rnw? Are you having problems echoing R commands in the PDF? Or are you trying to create LaTeX code with an R function? When I plug your code in knitr, I have no issues displaying % in PDF. I knit an MD file, and the resulting .tex gives \begin{verbatim} try = "0% success" try \end{verbatim} \begin{verbatim} [1] "0% success" \end{verbatim} Commented Dec 27, 2013 at 12:35
  • Sorry, I did not understand initially what you asked for.. I am creating an rnw file, then creating the tex. The texi2pdf operation is not able to print it. Commented Dec 27, 2013 at 12:39
  • So basically you want to create a variable in R (containing "0% success") and then render it in the string \textit{\Sexpr{try}}, where try is that variable in R? Commented Dec 27, 2013 at 12:43

3 Answers 3

11

why are you jumping fomr one "\" to 4 "\"?

> try2 = gsub("%","\\%",try,fixed = TRUE)
> try2
[1] "0\\% success"

This should be read as "\\", which (as far as i know) the regular expression will understand as an "\".

I hope i understood you right- and this is a possible sulution for you!

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

3 Comments

No, texi2pdf operation still thinks of the string after the % operation as a comment and gives me an error in that process because of that.
oh, sorry! Now i can see your problem- but i can´t reproduce it;
You were right, so If I put two // and output from R. It does come out perfectly, sorry my bad
5

You need two backslashes to escape:

<<>>=
try = "0\\% success"
@

Some text in LaTex \Sexpr{try}. 

After knitting the document one obtains the following LaTeX code:

\begin{document}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{try} \hlkwb{=} \hlstr{"0\textbackslash{}\textbackslash{}% success"}
\end{alltt}
\end{kframe}
\end{knitrout}

Some text 0\% success

\end{document}

This, of course, assuming echo=TRUE.

3 Comments

This still causes the problem? You can see its being knitd to \textbackslash{}\textbackslash{}% instead of the desired \%
I can see that, but that is contained within the knitrout environment. I have used the PDF compiler which is provided in R studio internally, as well as TeX Studio to confirm the generalizability. Both conversions gave me the required PDF. For your convenience I've included all three files in a zip dl.dropboxusercontent.com/u/18527706/stack.zip
Ah I'm not familiar with the knitrout environment. I didn't see that it had compiled the Some text 0\% success as well!
0

Other way to solve this issue is to use raw strings:

try = "0% success"
try2 = gsub(r"(%)", r"(\%)", try, fixed = TRUE)
try2
#[1] "0\\% success" 

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.