Insert "25 \%" into R for further processing in LaTeX

I want the character variable in R to take the value from, give the word "a" and add "\%" to create a% -sign later in LaTeX.

Usually I would do something like:

a <- 5
paste(a,"\%")

      

but it fails.

Error: '\%' is an unrecognized escape in character string starting "\%"

      

Any ideas? A workaround would be to define another command giving% -sign in LaTeX, but I would prefer a solution in R.

+3


source to share


2 answers


Like many other languages, some characters in strings have different meanings when hidden. One example of this is \n

what does newline mean instead of n

. When you write \%

, R tries to interpret it %

as a special character and doesn't do it. You can try to escape the backslash, so it's just a backslash:

paste(a, "\\%")

      



You can read up on escape sequences here .

+5


source


You can also look at a function latexTranslate

from the package Hmisc

, which will strip out special characters from strings to make them LaTeX compatible:



R> latexTranslate("You want to give me 100$ ? I agree 100% !")
[1] "You want to give me 100\\$ ? I agree 100\\% !"

      

+5


source







All Articles