Search code examples
rsweave

Paste "25 \%" in R for further processing in LaTeX


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.


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.