0

I have the following family of options

\pgfkeys{
    /opts/.is family,
    /opts,
    name/.store in = \@@Style@Name,
    property/.code = {
        \expandafter\gdef\expandafter\csname @@Style@ \@@StyleName @Poperty \endcsname#1},
}

I want to create a global command \@@Style@<value of the "name">@Property and assign it the value of "property" if it is passed.

For example, the code \pgfkeys{/opts, name = A, property = B} should create a command \@@Style@A@Propety with the value B. How can I do that.

Minimal non-working example:

\documentclass{article}

\usepackage{pgfkeys}

\makeatletter

\pgfkeys{
    /opts/.is family,
    /opts,
    name/.store in = \@@Style@Name,
    property/.code = {
        \expandafter\gdef\expandafter\csname @@Style@ \@@StyleName @Poperty \endcsname#1},
}

\begin{document}

    \pgfkeys{/opts, name = A, property = B}
    
    \@@Style@A@Propety

\end{document}

1 Answer 1

1

The exact syntax from the question is implemented below (see With the syntax from the question), but my suggestion would be to use a slightly different one, namely:

\pgfkeys{/myopts={A=foo, B={bar baz\\quux}}}

in order to set \@@Style@A@Property to foo and \@@Style@B@Property to bar baz\\quux (thus two assignments in this example, but you can perform as many as you want with my code). Indeed, the syntax you asked for requires stateful code:

  • one needs to remember that the property name has been set until we see the value, or that the value has been set until we see the name;

  • one needs to specify what happens with dubious input like \pgfkeys{/opts, name=foo, name=bar, property=whatever} or \pgfkeys{/opts, property=whatever}, etc.

The syntax I propose can be implemented by defining the /some dir/.unknown handler, which is invoked by pgfkeys whenever an unknown key in /some dir is used.

\documentclass{article}
\usepackage{pgfkeys}
\usepackage{etoolbox}  % for \csgdef (syntactic sugar)

\makeatletter
\pgfkeys{
  /myopts/dir/.unknown/.code={%
    \csgdef{@@Style@\pgfkeyscurrentname @Property}{#1}%
  },
  /myopts/.code={\pgfkeys{/myopts/dir/.cd, #1}},
}
\makeatother

\begin{document}

\pgfkeys{/myopts={A=foo, B={bar baz\\quux}}}

\makeatletter
\show\@@Style@A@Property % \@@Style@A@Property=macro:->foo.
\show\@@Style@B@Property % \@@Style@B@Property=macro:->bar baz\\quux.
\makeatother

\end{document}

Regarding the code you posted, please note that

\expandafter\gdef\expandafter\csname @@Style@ \@@StyleName @Property \endcsname#1}

should rather be something like this:

\expandafter\gdef\csname @@Style@\@@StyleName @Property\endcsname{#1}

(no need for the second \expandafter because it is \csname that needs to be expanded early here; beware of unwanted spaces; wrap the macro replacement text within curly braces).

With the syntax from the question

Since the OP insists on having exactly the syntax from the question, namely \pgfkeys{/opts, name = A, property = B}, here it is. I don't recommend it, though (there might be good reasons but they haven't been revealed so far, AFAICT). The name must be given before the property (as in the question).

\documentclass{article}
\usepackage{pgfkeys}
\usepackage{etoolbox}  % for \csgdef (syntactic sugar)

\makeatletter
\pgfkeys{
  /opts/.is family,
  /opts/name/.store in=\@@My@PropName,
  /opts/property/.code={%
    \csgdef{@@Style@\@@My@PropName @Property}{#1}%
  },
}
\makeatother

\begin{document}

\pgfkeys{/opts, name = A, property = B}

\makeatletter
\show\@@Style@A@Property % \@@Style@A@Property=macro:->B.
\makeatother

\end{document}
8
  • I am a little confused, I don't think that the code does what I asked for. The name of the created command should be \@@Style@<value>@Prorerty, not \@@Style@<key>@Property. Commented Jan 22, 2021 at 11:17
  • I simply did what I understood from the question. You said you wanted \pgfkeys{/opts, name = A, property = B} to store B in macro \@@Style@A@Propety (sic), and that is what I did with what I think is a better syntax. If you want a <key>=<value> assignment to define macro \@@Style@<value>@Prorerty, what is going to be stored inside this macro? The <key>? This is a simple matter of swapping things but would be unintuitive, IMHO.
    – frougon
    Commented Jan 22, 2021 at 12:20
  • I want to store value of property in the \@@Style@<value of name>@Property. Commented Jan 22, 2021 at 12:33
  • What is property? There is no such thing in \pgfkeys{/myopts={key1=value1, key2=value2}} .
    – frougon
    Commented Jan 22, 2021 at 13:01
  • name and property are the names of the keys. A and B are their values respectively. Commented Jan 22, 2021 at 13:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .