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
If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: var s = "\\Tasks"; // or var s = @"\Tasks"; Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.
In R (and elsewhere), the backslash is the “escape” symbol, which is followed by another symbol to indicate a special character. For example, "\t" represents a “tab” and "\n" is the symbol for a new line (hard return).
In R, a single backslash is an escape character, and using it for directory paths will always produce an error. To specify directory paths correctly in R, you have two options: Use two backslashes instead of a single one: > setwd("C:\\R files") Use a forward slash: > setwd("C:/R files")
The backslash is used to escape special (unprintable) characters in string literals.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With