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.

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

share|improve this question
    
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 Dec 5 '12 at 13:51
    
Yes it was that. But strangely I thought I had already tested this ... –  Ger Dec 5 '12 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 Dec 5 '12 at 14:59
add comment

1 Answer 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.

share|improve this answer
2  
...and if you use \foreach \item in \prop, the loop will spin three times again... –  Jake Dec 5 '12 at 14:00
    
Yes saw that in your top comment after I posted, you have the advantage of having used pgfkeys I suspect:-) –  David Carlisle Dec 5 '12 at 14:32
add comment

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.