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
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!
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.