[Rd] Backslashes, braces, \Sexpr and user macros

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Fri Mar 25 16:32:23 CET 2022


Hello R-devel,

I had been using an Rd trick that runs afoul of the changes in
r81965[*]. After the change, there seems to be a problem with any
user-defined macro that takes R code, forwards it to \Sexpr and gets
passed a string literal containing '{':

\newcommand{\RRd}{\Sexpr[stage=build,results=rd]{#1}}
\RRd{x <- 'a string with { inside it'; 'okay'}

R-4.1.3: okay
R-devel: Error: x.Rd: '\{' is an unrecognized escape in character string
starting "'a string with \{"
Execution halted

My actual use case is a macro that forwards its argument to
\Sexpr{tools::toRd(bibentry(...))}. I've been trying to pass \enc{}{}
as an argument to bibentry; previously, I had to use a workaround to
create the backslash, but now any braces (in addition to backslashes)
inside string literals also result in errors:

\newcommand{\bib}{\Sexpr[stage=build,results=rd]{bibentry(#1)}}
\bib{'Misc',
  author = person('J.', 'Doe'), year = 2022,
  title = paste0(
    'A ', rawToChar(as.raw(0x5c)),
    'enc{møøse}{moose} bit my sister once'
#       ^-----^^-----^--- in these four places
  )
}

R-4.1.3: Doe J (2022). “A møøse bit my sister once.”

R-devel: Error: x.Rd: '\{' is an unrecognized escape in character string
starting "'enc\{"
Execution halted

Should I have relied upon \Sexpr{#1} working inside user-defined
macros? Georgi N. Boshnakov's Rdpack does similar things (and has been
an inspiration for my tricks). I'll be glad to re-write some of my Rd
to achieve the same result using less crazy tricks, assuming there is a
right way to do that.

-- 
Best regards,
Ivan

[*]
https://github.com/r-devel/r-svn/commit/7ccd166735e6b978023b85fc9aa4845af8c0c241



More information about the R-devel mailing list