Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
1  
The .default value is only used if you call the prenom key without a value. If you don't call the prenom 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 say prenom/.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. –  Jake Dec 6 '12 at 10:27
    
Quick, short and efficient ! Nice comment. –  Ger Dec 6 '12 at 13:08

1 Answer 1

up vote 5 down vote accepted

The problem with your first example is that, despite the fact that key nom actually has a value, that value is never stored in macro \nom, so that macro remains undefined. pgfkeys only will create \nom and store in it the value of the key nom when the key nom is executed. Only then the value of nom is retrieved (and if none was assigned the default one is used) and stored in \nom.

So your problem is that you have to "execute" nom and prenom keys. You can do it a three different places:

  • When calling command \qui, as for example: \qui[nom, prenom]. Since you are executing those keys but not assigning them a value, defaults are used.

  • Inside \qui macro definition:

    \newcommand{\qui}[1][]{
        \pgfkeys{/test/pgf/.cd, nom, prenom, #1}
        I am \prenom{} \nom{} !
    }
    

    This way you can use simply \qui{} in main program. If you use instead \qui[nom=foo], this value will override the default one.

  • Inside the initial \pgfkeys definition:

    \pgfkeys{
        /test/pgf/.cd,
        nom/.default = toto,
        nom/.store in = \nom,
        prenom/.store in = \prenom,
        prenom/.default = titi,
        nom, prenom
    }
    

    This has the same effect than the second option, and it is more general, so it should be the preferred one.

share|improve this answer
    
Thanks a lot. This kind of short and simple example strongly lack in the pgfmanual. But maybe pgfkeys was not created to do so simple things. –  Ger Dec 6 '12 at 13:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.