> 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).
Glad to be inspiration:). There are no tricks in Rdpack and
using \Sexpr{#1} in user Rd macros is ok but care is needed. In your example:
\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
)
}
I am surprised that the above macro ever worked as intended. 'bibentry'
returns a 'bibentry' object and \Sexpr converts it with
'as.character'. There is no as.character method for bibentry, so the
result would be something like list(list(given = "J.", family = ...)).
That is what I get on Ubuntu with R-devel (r82022 2022-03-29) and non-R-devel. I
put the above in a package. If you are running a command on the console the
bibentry object will be printed ok since the default print method for bibentry
uses the text style. In fact, if the body of your macro is replaced by
capture.output(print(bibentry))
I get the desired result in the built documentation:
Doe J (2022). ?A m??se bit my sister once.?
One thing to be aware of is that the argument of \Sexpr is R-like but that of
user defined macros is not. When the parser escapes the argument to your macro,
it doesn't know that in your case it is R code (in the invocation of
'\bib' above), since it would need to 'understand' the body of
your macro and notice that you are using #1 in R code, not in LaTeX-like text.
This would explain the error message you get.
I don't know if something was fixed between the R-devel you used and
probably the most recent one I did but reproducible example and system
information would be helpful. I have some simple packages used in bug reports at
https://github.com/GeoBosh/reprexes
Georgi Boshnakov
-----Original Message-----
From: R-devel <r-devel-bounces at r-project.org> On Behalf Of
r-devel-request at r-project.org
Sent: 29 March 2022 11:01
To: r-devel at r-project.org
Subject: R-devel Digest, Vol 229, Issue 21
Send R-devel mailing list submissions to
r-devel at r-project.org
To subscribe or unsubscribe via the World Wide Web, visit
https://stat.ethz.ch/mailman/listinfo/r-devel
or, via email, send a message with subject or body 'help' to
r-devel-request at r-project.org
You can reach the person managing the list at
r-devel-owner at r-project.org
When replying, please edit your Subject line so it is more specific than
"Re: Contents of R-devel digest..."
Today's Topics:
1. Backslashes, braces, \Sexpr and user macros (Ivan Krylov)
----------------------------------------------------------------------
Message: 1
Date: Fri, 25 Mar 2022 18:32:23 +0300
From: Ivan Krylov <krylov.r00t at gmail.com>
To: r-devel at r-project.org
Subject: [Rd] Backslashes, braces, \Sexpr and user macros
Message-ID: <20220325183223.3ab20393-2358 at arachnoid>
Content-Type: text/plain; charset="utf-8"
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