12

I want to set the same default value for multiple PGF keys, like in this case where both a and b have a default of 1:

\documentclass{article}
\usepackage{pgfkeys}
\pgfkeys{
 a/.code={a:#1},
 a/.default=1,
 b/.code={b:#1},
 b/.default=1
}
% the rest is irrelevant, the question only concerns setting the keys
\begin{document}
 \pgfkeys{a,b}
\end{document}

In my real code, which contains many more than two keys, it's pretty repetitive to be writing keya/.default=1, keyb/.default=1, etc. So is it possible to set a default for a large number of keys all at once, in some way that takes less typing than one full assignment for each such key? If there's some concept of inheritance for keys, such that I could set a default on a "base key" and then "derive" other keys from it, that would work quite well for my purposes, but from a read through the manual I don't see that. Maybe I'm missing it.

Of course there's no reason I have to do this; it works fine to just write a whole bunch of key/.default=1 lines, but I'm curious about whether it could be done.

2
  • Yes, but you may need the full tikz package for \foreach \key in {a,b} Commented Jun 25, 2014 at 21:23
  • Not a problem, because I'm using tikz anyway (the loading of pgfkeys is just to keep the example minimal)
    – David Z
    Commented Jun 25, 2014 at 21:36

3 Answers 3

7

Using pgffor (@percusse beat me to it but this is still a little different in allowing you to have different defs for a and b):

\documentclass{article}
\usepackage{pgfkeys}
\usepackage{pgffor}
\pgfkeys{
 default to 1/.style={#1/.default=1},
 a/.code={a:#1},
 b/.code={b:#1},
 default to 1/.list={a,b}
}
% the rest is irrelevant, the question only concerns setting the keys
\begin{document}
 \pgfkeys{a,b}
\end{document}

Or, if you wish to be able to specify the default on the fly:

\documentclass{article}
\usepackage{pgfkeys}
\usepackage{pgffor}
\pgfkeys{
 set default/.style args={of #1 to #2}{
     default to #2/.style={##1/.default=#2},
     default to #2/.list={#1}
 },
 a/.code={a:#1},
 b/.code={b:#1},
 c/.code={c:#1},
 d/.code={d:#1},
 set default = of {a,c} to 1,
 set default = of {b,d} to 2
}
\begin{document}
 \pgfkeys{a,b,c,d}
\end{document}
13

Yes it is the /.list handler. The argument to it is passed to a \foreach array and at each spin next item is passed to the original handler.

\documentclass{article}
\usepackage{pgfkeys,pgffor}
\pgfkeys{
mykeysetter/.style={
#1/.code={#1:##1},
#1/.default=1
},
mykeysetter/.list={a,b,c}
}

\begin{document}
 \pgfkeys{a=4,b=3,c}
\end{document}

This gives

a:4b:3c:1

9

You do have a macro processor to hand so you could define a macro for it if tikz doesn't offer the functionality directly:

\documentclass{article}
\usepackage{pgfkeys}
\def\foo#1{\pgfkeys{#1/.code={#1:##1},#1/.default=1}}

\foo{a}
\foo{b}

% the rest is irrelevant, the question only concerns setting the keys
\begin{document}
 \pgfkeys{a,b}
\end{document}
1
  • That could work, though it will take some thought because the code (actually a style) for each of these keys is nontrivially different - they don't all follow a shared structure to the extent shown in the example. Still probably saves some keystrokes.
    – David Z
    Commented Jun 25, 2014 at 21:57

You must log in to answer this question.

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