# How to make a pgf default action create a different key

I know very little pgf, but I'm trying to learn...so bear with me (please).

I would like to have a default action on a pgf path so that if I try to access any unknown key, without giving it a value, the result is to define a different key (say id), with the value being the name of the original key.

so that

\pgfkeys{/YF/hello}


would actually do

\pgfkeys{/YF/id=hello}


If hello is unknown.

-

I think that you are looking for the .unknown key handler. It is described in Section 55.3.5 of the PGF manual (2.10 version) and there are some examples scattered throughout the TikZ and PGF code (and PGFPlots, if I remember right). Also, most of my packages in the TeX-SX launchpad directory (and subsequently on CTAN) have examples.

If I understand your situation correctly then the following will do it:

\documentclass{article}
%\url{http://tex.stackexchange.com/q/34376/86}
\usepackage{tikz}
\pgfkeys{/YF/.unknown/.code={%
\pgfkeys{/YF/id=\pgfkeyscurrentname}
},
/YF/id/.initial=hello,
}

\begin{document}
\pgfkeysvalueof{/YF/id}
\pgfkeys{/YF/world}
\pgfkeysvalueof{/YF/id}
\end{document}


This produces hello world.

-
looks great! thanks. – Yossi Farjoun Nov 10 '11 at 20:53
Two comments: first, I like using the .style handler for this, since you're just calling another key: YF/.unknown/.style = {/YF/id = \pgfkeyscurrentname}. (since .style is a kind of .code, it's perfectly valid as a thing to define for .unknown). Second, I am always unsure when that macro changes (it seems not to in a .unknown handler, but definitely does with other keys) so I usually expand first: YF/.unknown/.style/.expanded = {YF/id = \pgfkeyscurrentname}. It seems to work for you without this, though. – Ryan Reich Nov 11 '11 at 0:27
@RyanReich: Good point about the expansion. That's something to watch for with more complicated set-ups. I hadn't thought of .style; my usual cases are a little more complicated as I usually want to do something a bit like the TikZ version where it tests to see if the unknown key is a colour or an arrow or a shape before passing it on to the PGF subsystem. – Loop Space Nov 11 '11 at 7:59
@Andrew: If you're talking about the example in the 2.10 manual, section 55.3.5, I think it can be rewritten using .style; the only thing they do besides testing keys is expand \pgfkeyscurrentname, and you can do that either with .expanded on each .try or by defining a key, say, key name/.estore in = \thiskeyname and then calling key name = \pgfkeyscurrentname. I have yet to find a situation where .code is actually necessary, given the way handlers work. Maybe if you want to do something complex outside the key system. – Ryan Reich Nov 11 '11 at 15:17
@RyanReich: No, I'm talking about the one in tikz.code.tex, starting with line 1252. If you look at that you'll see that it's quite complicated as it tests for lots of things before passing it on to the PGF system. – Loop Space Nov 11 '11 at 21:45