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

Hadley Wickham h@w|ckh@m @end|ng |rom gm@||@com
Fri Sep 8 18:03:03 CEST 2023


You're getting confused between the contents of the string and the
printed representation of the string. There's a little bit about this
in R4DS: https://r4ds.hadley.nz/strings#escapes

(or in brief, do writeLines(trailing.7.message))

Hadley

On Fri, Sep 8, 2023 at 7:23 AM Christopher Ryan via R-help
<r-help using r-project.org> wrote:
>
> 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]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
http://hadley.nz



More information about the R-help mailing list