1

I try to use the pgfkeys package. Hereafter there is a short example of a simple command which use pgfkeys. I would like to know why the containts of a variable is not considered in a same way in the case of it is a mandatory argument of the command and in the case it is a option.

As you can see below, if I use #2 or \prop I do not obtain the same output.

\documentclass[a4paper]{article}
\setlength{\parindent}{0em}

\usepackage{pgfkeys}
\usepackage{pgffor}

\pgfkeys{
    /nom/.code=\def\nom{#1},
    /prenom/.code=\def\prenom{#1},
    /prop/.code=\def\prop{#1},
    /prop/.default = {age,ville,couleur}
}

\newcommand{\qui}[2][]{
    \pgfkeys{#1}
    I am \prenom{} \nom{} !

    \medskip

    \#2 contains : #2

    \medskip

    prop contains : \prop

    \medskip

    If I use \#2 I get :\\
    \foreach \item in {#2} {
        \item \\
    }

    \medskip

    If I use prop option I get :\\
    \foreach \item in {\prop} {
        \item \\
    }
}

\begin{document}
\qui[prenom = toto, nom = titi, prop = {29,perpignan,bleu}]{29,perpignan,bleu}
\end{document}

enter image description here

3
  • If you want to use \foreach to cycle over a comma separated list that's stored in a macro, you'll need to use \foreach \item in \prop (without the {...}). If it's stored in a macro argument (like #2), you do need the {...}. Is that what you're asking?
    – Jake
    Commented Dec 5, 2012 at 13:51
  • Yes it was that. But strangely I thought I had already tested this ...
    – Ger
    Commented Dec 5, 2012 at 14:11
  • You can use /nom/.store in = \nom, etc, to just save a value in a macro. This is a little less writing.
    – Ryan Reich
    Commented Dec 5, 2012 at 14:59

1 Answer 1

1

You see similar things with most loop macros. If you go

  \foreach \item in {a,b,c}

then the loop will spin three times with \item being set to a on the first time.

If you go

\def\prop{a,b,c}
\foreach \item in {\prop}

The loop will just spin once, with \item being defined to be \prop and only later expanding to a,b,c when the item is used.

2
  • 2
    ...and if you use \foreach \item in \prop, the loop will spin three times again...
    – Jake
    Commented Dec 5, 2012 at 14:00
  • Yes saw that in your top comment after I posted, you have the advantage of having used pgfkeys I suspect:-) Commented Dec 5, 2012 at 14:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .