2

Consider the following MWE:

\documentclass{article}

\usepackage{tikz}

\newenvironment{env}{
    \pgfkeys{/env/.initial=hello}
}{}
\newcommand{\comm}{
    \pgfkeys{/comm/.initial=hello}
}{}

\begin{document}

\begin{env}\end{env}
\comm{}

\pgfkeys{
    /env/.get=\envval,
    /comm/.get=\commval,
}

Value of key set using environment: \envval 

Value of key set using command: \commval

\end{document}

It produces

Value of key set using environment:
Value of key set using command: hello

Is this an known issue, and are there any workarounds?

gTcV
  • 1,065
  • An environment creates a TeX group. Definitions are local to that group unless you make them global. If you want values to remain accessible outside the group, you need to make them global before closing it. – cfr Feb 08 '17 at 04:27
  • @Andrew It is not really a properly of pgfkeys in particular. It is the same for any other way of writing \def. – cfr Feb 08 '17 at 04:28
  • @cfr Yes that's true , but I've always thought that this was a conscious design choice in pgfkeys (but I don't know for sure, so perhaps it isn't). –  Feb 08 '17 at 04:31
  • And how do I make this change global? – gTcV Feb 08 '17 at 04:31
  • One of the (very useful) features of pgfkeys is that any changes are local to the current group - and an environment creates a group. See Martin Scharrer's answer to http://tex.stackexchange.com/questions/15204 for one way around this. –  Feb 08 '17 at 04:33
  • 1
    One way of making the changes global is to use \pgfkeysgsetvalue{/env}{Hello}, where the macro \pgfkeysgsetvalue is defined in Martin Scharrer's answer in the post I linked to above. He also defines a .gcode handler. –  Feb 08 '17 at 04:35
  • Or use a different key-setting system, of course. L3 keys doesn't have this problem. – cfr Feb 08 '17 at 04:37
  • 1
    @cfr In what sense? Also L3 keys obey scoping rules. – egreg Feb 08 '17 at 10:08
  • @egreg But it does have things like .gset_thing in addition to .set_thing. And that seems to make it a lot easier in practice to get values assigned globally when you want them and not otherwise. I agree that the particular structure in the example would still be problematic, but something functionally equivalent is easier to create than with pgfkeys. At least, for me it is. But L3 keys makes more sense generally to me than pgfkeys ever has, so maybe I just don't see the obvious in the latter case. – cfr Feb 08 '17 at 18:19

0 Answers0