Question

I want a character variable in R taking the value from, lets say "a", and adding " \%", to create a %-sign later in LaTeX.

Usually I'd do something like:

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

but this fails.

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

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

Was it helpful?

Solution

As many other languages, certain characters in strings have a different meaning when they're escaped. One example for that is \n, which means newline instead of n. When you write \%, R tries to interpret % as a special character and fails doing so. You might want to try to escape the backslash, so that it is just a backslash:

paste(a, "\\%")

You can read on escape sequences here.

OTHER TIPS

You can also look at the latexTranslate function from the Hmisc package, which will escape 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\\% !"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top