Read the manual carefully - it says that applying the handler
/.code
(or one of its variants) yields defining a sub-key .@cmd
.
/.default
yields defining a sub-key .@def
.
I.e., /MyKey/.code=Whatsoever code
yields defining a sub-key /MyKey/.@cmd
.
I.e., /MyKey/.default=Whatsoever default value
yields defining a sub-key /MyKey/.@def
.
Thus you may wish to test for three keys:
/MyKey
/MyKey/.@def
/MyKey/.@cmd
You can do:
\documentclass{article}
\usepackage{pgfkeys}
\makeatletter
\newcommand\CheckWhetherFullKeyDefined[1]{%
\pgfkeysifdefined{#1}{\expandafter\@firstoftwo}{\expandafter\@secondoftwo}%
{\@firstoftwo}{%
\pgfkeysifdefined{#1/.@def}{\expandafter\@firstoftwo}{\expandafter\@secondoftwo}%
{\@firstoftwo}{%
\pgfkeysifdefined{#1/.@cmd}{\expandafter\@firstoftwo}{\expandafter\@secondoftwo}%
{\@firstoftwo}{\@secondoftwo}%
}%
}%
}%
\makeatother
\begin{document}
\begingroup
\pgfkeys{/WeirdKey/.code=Code of WeirdKey with argument #1}%
\CheckWhetherFullKeyDefined{/WeirdKey}%
{\message{^^J^^J!!!/WeirdKey DEFINED!!!^^J^^J}}%
{\message{^^J^^J!!!/WeirdKey NOT DEFINED!!!^^J^^J}}%
\endgroup
\begingroup
\pgfkeys{/WeirdKey/.initial=Initial value of WeirdKey}%
\CheckWhetherFullKeyDefined{/WeirdKey}%
{\message{^^J^^J!!!/WeirdKey DEFINED!!!^^J^^J}}%
{\message{^^J^^J!!!/WeirdKey NOT DEFINED!!!^^J^^J}}%
\endgroup
\begingroup
\CheckWhetherFullKeyDefined{/WeirdKey}%
{\message{^^J^^J!!!/WeirdKey DEFINED!!!^^J^^J}}%
{\message{^^J^^J!!!/WeirdKey NOT DEFINED!!!^^J^^J}}%
\endgroup
\end{document}
Alternatively you can use the .try
-handler for .cd
-ing to the key in question only if it exists.
I use the .cd
-handler because the manual says that \pgfkeys
in any case will reset the default-path to what it was before executing \pgfkeys
.
(I hope \pgfkeys
will never be changed to using the .cd
-handler for resetting - if it does, then resetting to a default-path which does not exist as a key as well might be a problem.)
\documentclass{article}
\usepackage{pgfkeys}
\makeatletter
\newcommand\CheckWhetherFullKeyDefined[1]{%
\begingroup
\pgfkeys{%
% Apply handlers not in the exception-list only to existing keys:
/handler config=only existing,
% Remove the /.cd-handler from the exception-list:
/utils/exec={\expandafter\let\csname pgfk@excpt@.cd\endcsname=\UndEFiNeD},
% Try the /.cd-handler - the /.try-handler will set \ifpgfkeyssuccess:
#1/.cd/.try,
}%
\expandafter\endgroup\ifpgfkeyssuccess\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}%
\makeatother
\begin{document}
\begingroup
\pgfkeys{/WeirdKey/.code=Code of WeirdKey with argument #1}%
\CheckWhetherFullKeyDefined{/WeirdKey}
{\message{^^J^^J!!!/WeirdKey DEFINED!!!^^J^^J}}%
{\message{^^J^^J!!!/WeirdKey NOT DEFINED!!!^^J^^J}}%
\endgroup
\begingroup
\pgfkeys{/WeirdKey/.initial=Initial value of WeirdKey}%
\CheckWhetherFullKeyDefined{/WeirdKey}
{\message{^^J^^J!!!/WeirdKey DEFINED!!!^^J^^J}}%
{\message{^^J^^J!!!/WeirdKey NOT DEFINED!!!^^J^^J}}%
\endgroup
\begingroup
\CheckWhetherFullKeyDefined{/WeirdKey}
{\message{^^J^^J!!!/WeirdKey DEFINED!!!^^J^^J}}%
{\message{^^J^^J!!!/WeirdKey NOT DEFINED!!!^^J^^J}}%
\endgroup
\end{document}
Be aware that you can check for an enitre key (path and name of key) only, but not for the "existence" of some "directory"/"folder" of the path of a key:
Management of keys internally is done by defining macros whose names contain the entire key.
Thus the "directory" /MyStuff
can "exist" due to the key /MyStuff/SubDirectory/SubSubDirectory/MyKey
being defined, implying that underlying macros are defined whose names contain the phrase pgfk@/MyStuff/SubDirectory/SubSubDirectory/MyKey
, e.g., \pgfk@/MyStuff/SubDirectory/SubSubDirectory/MyKey
and probably \pgfk@/MyStuff/SubDirectory/SubSubDirectory/MyKey/.@def
or \pgfk@/MyStuff/SubDirectory/SubSubDirectory/MyKey/.@cmd
.
For checking whether the "directory" /MyStuff
exists, you'd need to know if some macro is defined whose name is pgfk@/MyStuff
or whose name's first letters form the phrase pgfk@/MyStuff/
while there might be an arbitrary sequence of letters in that macro-name trailing that phrase. Afaik such a test is not feasible in TeX.
/marsupial/.cd
./marsupial
does not prevent you from probably overriding things that are already stored in that path.