5

Having learnt a lot about the pgfkeys package as a result of my last question, I'm now trying to do some more adventurous things.

Let's say I have a command called \blob, whose value is the following list of pairs:

foo/{left=2,right=23,label=horse},% 
bar/{left=25,right=29,label=sheep},% 
baz/{left=31,label=zebra}

The first item of each pair is a simple string, while the second item is a key-value list. I run a \foreach command over \blob, to process each pair in turn. Suppose the current pair is \x/\xvalue. I want to execute the command

\pgfkeys{/wickerson/.cd,left=0,right=0,label={},\xvalue}

where left=0, right=0 and label={} are the default values, to be overwritten by any values that the contents of \xvalue provides. My intention is that the first pair, for example, will expand to

\pgfkeys{/wickerson/.cd,left=0,right=0,label={},left=2,right=23,label=horse}

Unfortunately, this doesn't work. Here is a snippet of my output:

x = bar, xvalue = left=25,right=29,label=sheep

! Package pgfkeys Error: I do not know the key '/wickerson/left=25,right=29,label=sheep' and I am going to ignore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.

For some reason, it's treating left=2,right=23,label=horse as a single key, when I mean it to be treated as three keys. Could somebody kindly explain what's going on?

\documentclass{article}

\usepackage{tikz}

\pgfkeys{/wickerson/.cd,
  left/.code={\gdef\wickerson@left{#1}},
  right/.code={\gdef\wickerson@right{#1}},
  label/.code={\gdef\wickerson@label{#1}}
}

\begin{document}

\newcommand*\blob{%
  foo/{left=2,right=23,label=horse},%
  bar/{left=25,right=29,label=sheep},%
  baz/{left=31,label=zebra}%
}

\foreach \x/\xvalue in \blob {
  \typeout{x = \x, xvalue = \xvalue};
  \pgfkeys{/wickerson/.cd,left=0,right=0,label={},\xvalue}
} 

\end{document}
1

1 Answer 1

3

Thanks to Andrew Stacey's comment above, I managed to fix my code by changing three lines. The result is:

\documentclass{article}

\usepackage{tikz}

\pgfkeys{/wickerson/.cd,
  execute style/.style = {#1},                            % line added
  execute macro/.style = {execute style/.expand once=#1}, % line added
  left/.code={\gdef\wickerson@left{#1}},
  right/.code={\gdef\wickerson@right{#1}},
  label/.code={\gdef\wickerson@label{#1}}
}

\begin{document}

\newcommand*\blob{%
  foo/{left=2,right=23,label=horse},%
  bar/{left=25,right=29,label=sheep},%
  baz/{left=31,label=zebra}%
}

\foreach \x/\xvalue in \blob {
  \typeout{x = \x, xvalue = \xvalue};
  \pgfkeys{/wickerson/.cd,left=0,right=0,label={},execute macro=\xvalue} % line modified
} 

\end{document}

You must log in to answer this question.

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