[R] how to get paste() to output \% so LaTeX will interpret it as a percent sign rather than a comment symbol

Christopher Ryan cry@n @end|ng |rom b|ngh@mton@edu
Fri Sep 8 16:23:08 CEST 2023


I'm using a .Rnw --> Sweave --> pdflatex workflow on Windows 10, with emacs
26.3 and ESS (not sure which version.)

I have this code:

n.tests.7 <- 3 ## for example
trailing.7.pos.percent <- 12  ## for example
trailing.7.message <-
    ifelse( n.tests.7 > 0,
            paste("In the past seven days,", n.tests.7, "test results have
been reported, of which", trailing.7.pos.percent, "\\% were positive"),
            paste("No test results have been reported from these labs in
the past seven days.") )

Desired outcome in the resulting .tex file is "In the past seven days, 3
test results have been reported, of which 12\% were positive."

But I get "In the past seven days, 3 test results have been reported, of
which 12\\% were positive." Which, when compiled in pdflatex,
unsurprisingly cuts off the sentence at 12

Replacing, in the .Rnw code, the \\% with \% , yields an R error message:

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

Grateful for any help.

--Chris Ryan

	[[alternative HTML version deleted]]



More information about the R-help mailing list