1

Using pgfkeys, I automatically build some subkeys when an unknown key is encountered. See MWE below.

I would like to avoid having to set the test key (which is what fires the .unknown handler) before accessing the subkeys, ie. make it implicit. Is this possible?

\documentclass{article}

\usepackage{pgfkeys}
\usepackage{etoolbox}

\newcommand\buildkeys[1]{%
    \pgfkeys{%
        /mypath/#1/a/.initial=N/A,
        /mypath/#1/b/.initial=N/A,
    }
}

\pgfkeys{%
    /mypath/.unknown/.code={%
        \expandafter\buildkeys\expandafter{\pgfkeyscurrentname}%
    }
}

\pgfkeys{%
    /mypath/.cd,
    test, % Needed to fire the .unknown handler. I would like to avoid this
    test/a=test a,
    test/b=test b,
}

\begin{document}

should be "test a": \pgfkeysvalueof{/mypath/test/a}

should be "test b": \pgfkeysvalueof{/mypath/test/b}

\end{document}
8
  • No suggestions?
    – meide
    Oct 12, 2020 at 8:30
  • 1
    Not possible. If you access test/a there is no check whether test exists first. That is deliberate, because for many keys in PGF the parent key doesn't exist and it would be really wasteful (or even harmful) to fire .unknown in all of these situations. Oct 12, 2020 at 8:43
  • Thanks. Hmm, then it might be cleaner to do it with a \mykeys[2] macro that builds the keys under #1, then sets them from #2.
    – meide
    Oct 12, 2020 at 9:14
  • @HenriMenke btw is the use of .initial here correct? I would prefer to have them "undefined" if nothing is set by the user
    – meide
    Oct 12, 2020 at 9:18
  • 1
    \pgfkeys{a/.initial=} (or \pgfkeys{a/.initial={}} if you wish) should define it to be empty. Oct 12, 2020 at 10:57

1 Answer 1

0

Per @HenriMenke that is not possible, so I may instead use this solution (but I'm still open to better ways!)

\documentclass{article}

\usepackage{pgfkeys}

\newcommand\mykeys[2]{%
    \pgfkeys{%
        /mypath/#1/.cd,
        a/.initial=N/A,
        b/.initial=N/A,
        #2,
    }%
}

\mykeys{test}{%
    a=test a,
    b=test b,
}

\begin{document}

should be "test a": \pgfkeysvalueof{/mypath/test/a}

should be "test b": \pgfkeysvalueof{/mypath/test/b}

\end{document}

You must log in to answer this question.

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