Hereafter is a simple macro with keys nom
and prenom
defined with `pgfkeys.
\documentclass[a4paper]{article}
\usepackage{tikz}
\pgfkeys{
/test/pgf/.cd,
nom/.store in = \nom,
nom/.default = toto,
prenom/.store in = \prenom,
prenom/.default = titi,
}
\newcommand{\qui}[1][]{
\pgfkeys{/test/pgf/.cd,#1}
I am \prenom{} \nom{} !
}
\begin{document}
\qui[prenom = toto, nom = titi]
\end{document}
The issue is that if you use macro \qui
without the key \nom
or \prenom
you get an error message saying ! Undefined control sequence.
I think that when I execute the macro with \qui[nom = titi]
LaTeX does not know the macro \prenom
and thus gives me an error message.
I do not understand why I got this error since I give a default value to all keys with the .default
handler. I tried also the .initial
handler but if you want the macro to work you have to replace \prenom
and \nom
by \pgfkeysvalueof{prenom}
and \pgfkeysvalueof{nom}
.
PGF Keys differences between .initial and .default
But the following code works. You can use macro \qui
without any keys. But again I do not understand why the .default
handler does not do this job ?
\documentclass[a4paper]{article}
\usepackage{tikz}
\pgfkeys{
/test/pgf/.cd,
nom/.store in = \nom,
nom = toto,
prenom/.store in = \prenom,
prenom = titi,
}
\newcommand{\qui}[1][]{
\pgfkeys{/test/pgf/.cd,#1}
I am \prenom{} \nom{} !
}
\begin{document}
\qui[prenom = toto, nom = titi]
\end{document}
.default
value is only used if you call theprenom
key without a value. If you don't call theprenom
key at all, it isn't executed, and therefore the\prenom
macro isn't defined. The usual thing to do in a case like this would be to sayprenom/.store in = \prenom, prenom/.default = titi, prenom
, i.e. define the key, set its default, and call it once (using the default value) to make sure the macro is defined.