3

I have a macro of the following form:

\tikzset{
    foo/.style 2 args={
        ...
    },
    foo/.default={2pt}{}
}

If I do not want to specify any argument, I can use foo. If I want to specify the first argument, I can use foo={3pt} (EDIT: in this case, the second argument is empty). If I want to specify both arguments, I can write foo={3pt}{green}.

Now I want to call foo, use the default value for the first argument and specify a value for the second argument. Is this possible ?

Something like foo={default}{green}.

eponier
  • 133
  • 4
  • 1
    foo={3pt} will not behave the way you suggested -- it always makes the second argument empty no matter what the second argument default is. – Hood Chatham Feb 01 '18 at 19:33

1 Answers1

4

This implements a handler called /.default 2 args that has the requested behavior.

\documentclass{article}
\usepackage{pgfkeys}

\pgfkeys{/handlers/.default 2 args/.code 2 args={%
    \pgfkeyssetvalue{\pgfkeyscurrentpath/.@def}{{#1}{#2}}
    \pgfkeyssetvalue{\pgfkeyscurrentpath/.@def1}{{#1}}
    \pgfkeyssetvalue{\pgfkeyscurrentpath/.@def2}{{#2}}
    \pgfkeysgetvalue{\pgfkeyscurrentpath/.@cmd}{\temp}
    \pgfkeyslet{\pgfkeyscurrentpath/.@cmd@orig}{\temp}
    \pgfkeysdefargs{\pgfkeyscurrentpath}{##1##2}{
        \def\tempa{##1}\def\tempb{##2}\def\tempc{default}%
        \ifx\tempa\tempc
            \pgfkeysgetvalue{\pgfkeyscurrentpath/.@def1}{\temparga}%
        \else
            \def\temparga{{##1}}%
        \fi
        \ifx\tempb\tempc
            \pgfkeysgetvalue{\pgfkeyscurrentpath/.@def2}{\tempargb}%
        \else
            \def\tempargb{{##2}}%
        \fi
        \csname pgfk@\pgfkeyscurrentpath/.@cmd@orig\expandafter\expandafter\expandafter\endcsname\expandafter\temparga\tempargb\pgfeov
    }
}}

\begin{document}


\pgfkeys{
    foo/.code 2 args={(#1)(#2)},
    foo/.default 2 args={2pt}{1pt},
    foo={a}{b},
    foo={a}{default},
    foo={default}{b},
    foo
}
\end{document}
Hood Chatham
  • 5,467
  • Nice, although the use of multi-argument keys is generally against the spirit of the interface, is it not? ;) – cfr Feb 01 '18 at 23:12
  • Thanks for your solution! I believe there is nothing simpler or more built-in? I was looking more for a keyword I did not know, but this does not seem as simple as I thought. – eponier Feb 08 '18 at 15:38
  • 2
    Yeah I've read the section of the pgf manual on pgfkeys quite a few times. There is nothing built-in to handle default arguments for multi-argument keys. – Hood Chatham Feb 08 '18 at 15:53