The behavior of the code below for each variant of baz is as I'd expect. The one that is `not OK', however, expresses what I'm aiming for: \foo is accesible within the definition of \baz after \endgroup, but is otherwise `hidden' (in particular, it does not clash with \def\foo{Oops} defined after \endgroup).
I could remove \begingroup ... \endgroup altogether and replace \def\foo by \gdef\SCOPENAMEfoo and use this in the definition of \baz. I'm hoping there is a better/neater suggestion. How are packages written in this respect?
I'm not clear on what you want to achieve... you want \baz have the definition of \foo that it has inside the group but want \foo be undefined outside the group? One of your versions works:
The solution with \@temp seems to work, but I have to wrap my mind around it. The one with \global\let has not worked. The one with multiple expandafter seems to emulate \xdef, doesn't it?
Meanwhile let me rephrase my goal.
Let's say that \gdef\baz (not \xdef\baz) is the public interface of a package called baz. \foo is part of the overhead: it is meant to be used within \gdef\baz, not the user of the package.
Here's the dilemma: if I define \foo using \gdef, the package works, but \foo becomes part of the public interface, which is unwanted. If I define it using \def the package does not work.
I still don't understand. Why do you want that \baz contains unexpanded content that is not defined? You either have to expand \foo in the process or define both macros either globally or not inside the group.
The \expandafter method does not exactly emulate the \xdef version. In the first case \foo is expanded once, in the second the contents of \xdef's second argument are expanded until there are only unexpandable tokens left.
cgnieder wrote:I still don't understand. Why do you want that \baz contains unexpanded contents that is not defined?
I don't see how saying that \foo is part of the overhead of \baz conveys that. In any case, I didn't mean it, and I have acknowledged this constraint:
cgnieder wrote:You either have to expand \foo in the process or define both macros either globally or not inside the group.
The solution with \@temp is not going to work if \foo takes an argument. Thus far, I only see one solution, and that's to do \gdef\PACKAGEOVERHEADfoo where PACKAGEOVERHEAD is chosen to minize the chance that \PACKAGEOVERHEADfoo clashes with a macro defined outside the package.