Christopher Ryan
2023-Sep-08 14:23 UTC
[R] how to get paste() to output \% so LaTeX will interpret it as a percent sign rather than a comment symbol
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]]
Hadley Wickham
2023-Sep-08 16:03 UTC
[R] how to get paste() to output \% so LaTeX will interpret it as a percent sign rather than a comment symbol
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 at 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 at 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