but that's not very transparent since to get at the "value", I have to use the macro \csname applicant@whatever the key name was\endcsname. (Also, I'm not very au fait with pgfkeys, this being my first foray, so I should probably have used something like /.expand once or /.store in, but that's by the by.)

Is there a better way to circumvent this restriction? I'd be happy with a new macro for defining the key, so \pgfglobalkeys would be okay (though in the spirit of \pgfkeys it feels more like it ought to be \pgfkeys{key name/.global=...} but let's not quibble), but when using the key's value then it ought be be accessible via the traditional means, say pgfkeysgetvalue.

4 Answers
4

Edit: I just got my copy of TeX by Topic from Lulu, which of course means that my work day ended early :-). I stumpled upon \globaldefs, which allows one to answer the question actually asked with a yes:

Here I implicitly use that \globaldefs is 0 (or non-positive), so that the assignment \globaldefs=1 is local. If \globaldefs is already positive in the current scope, we don't need to set it; and in fact it would be wrong to do so (since its value might be local to some surrounding group; assigning it a positive value again would set it globally). Correcting this is left as an exercise.

Original answer Andrew, your comment about using \aftergroup led me to investigate. It turns out that the body of \foreach is actually performed two levels down. Assuming that what you really want is a way for \foreach to be able to set keys at the current scope (as opposed to actually setting them globally), this seems to work:

That is fantastic. I think that both your solutions are useful, so I'm glad you left the earlier one in there. And with the added bonus that I now understand why one might want to mess with \globaldefs! (And as if that weren't enough, I'm left with the image of Lulu sending you a book. If you're confused, take a look at en.wikipedia.org/wiki/Lulu_%28singer%29 )
– Loop SpaceApr 8 '11 at 20:09

It is probably safer to have the \globaldefs inside the loop, so that the the looping code is not affected. Or even better make a wrapper of \pgfkeys that locally sets \globaldefs.
– CaramdirApr 8 '11 at 20:58

@Caramdir: That's right. Would something as simple as \def\pgfglobalkeys#1{\begingroup \ifnum\the\globaldefs>0\relax \else \globaldefs=1\fi \pgfkeys{#1}\endgroup} work? (Provided of course that none of the key-setting business messes with \globaldefs.)
– VillemoesApr 8 '11 at 21:52

I don't think pgfkeys allows for global assignments out-of-the-box.
However, you can define your own handler to define global values. The problem is that it wont work the same for all macros like \pgfkeyssetvalue keys like .code, .store in, .style, etc. You would have to look up their definitions and define global versions of these handlers, like \pgfkeysgsetvalue.gcode, .gstore in, .gstyle.

First, you want to use \pgfkeyslet instead of \pgfkeyssetvalue, since you want to use the key outside, where \myvalue is no longer defined, so you have to set the key to the contents of \myvalue instead of to \myvalue itself.
Second, the definition of \pgfkeyslet starts with \expandafter\let. As it happens, prefixing \pgfkeyslet with \global still affects the \let after the expandafter, so that

does what you want. This solution is a bit dirty since it depends on the internal definition of \pgfkeyslet.

Another solution is to forget about \foreach if it doesn't fit the bill and to use another method for iterating about a comma separated list. If you are free to choose the representation of the list that you want to iterate over, the simplest approach is the one proposed by Knuth in Appendix D of The TeXbook. Define the list as

\newcommand\mylist{\li{long}{list}\li{of}{keys}}

Now you can process the list items repeatedly by \leting \li to a command that implements the loop body. In your example this would just be

\let\li\pgfkeyssetvalue

after which

\mylist

processes the key-value pairs. (Knuth proposed to use \\ instead of \li, which might be dangerous in LaTeX.)