# How to check if a pgfkey has a .code defined?

I naïvely wrote this thing:

\documentclass[]{article}
\usepackage{tikz}
\pgfqkeys{/pgf}{test/.code={TEST}}

\begin{document}

\pgfkeysifdefined{/pgf/test}{TRUE}{FALSE}

\pgfkeysifdefined{/pgf/test/.code}{TRUE}{FALSE}

\pgfkeys{/pgf/test}

\end{document}


...and I supposed that at least one of the two tests would have given true, but I have:

Normally, \pgfkeys{<key path>/.code=...} internally defines two keys <full key path>/.@cmd and <full key path>/.@body, hence you can use

\documentclass{article}
\usepackage{tikz}
\pgfqkeys{/pgf}{test/.code={TEST}}

\begin{document}
\pgfkeysifdefined{/pgf/test/.@cmd}{TRUE}{FALSE}  % output "TRUE"

\pgfkeysifdefined{/pgf/test/.@body}{TRUE}{FALSE} % output "TRUE"

\pgfkeys{/pgf/test}

\end{document}


There is a short introduction to <key>/.@cmd in the pgf manual, sec. 88.3.3.

More Notes

• \pgfkeysifdefined{<key>}{<true>}{<false>} tests if an internal command \pgfk@<key> is defined, it cannot be used to test if a <key> is defined by handler .code.
• On the one hand, keys defined by some handlers are not distinguishable. For example, .code and .ecode.
• On the other hand, keys defined by some other (kinds of) handlers are distinguishable. For example, keys defined by .(e)code and .(e)code 2 args can be distinguished by the argument specification part of their corresponding \pgfk@<key>/.code commands.
• I added some limited discussions about how to determine a key is defined by some kind of handlers. – muzimuzhi Z May 30 at 18:53