28

In a command like:

\tikset{qar/.style={draw,rectangle,fill=#1}}

is it possible to add a default key value for the option fill so that if you have:

\node [qar] {};

the node will be filled with the default color (for example, red) while if you have:

\node [qar={blue}] {};

the node will be filled with the selected color?

And what if instead of one there are two arguments like:

\tikset{qar/.style 2 args={draw=#2,rectangle,fill=#1}}

?

f.boc
  • 912

1 Answers1

36

You can assign a default value for a key by using the .default handler. By setting

qar/.style={
    draw,
    rectangle,
    fill=#1
},
qar/.default=red

the value red will be used whenever qar is called without an argument.

The same thing works for keys that take two or more arguments:

qar2/.style 2 args={
    rectangle,
    draw=#1, thick,
    fill=#2
},
qar2/.default={blue}{yellow}
Jake
  • 232,450
  • 7
    How about qar2/.style args= {#1and#2also#3}{ rectangle, draw=#1, thick, fill=#2, text=#3 }, - how do you set default values for that? – Make42 Dec 10 '15 at 15:42
  • @Make42, it seens that qar2/.default={1 and 2 also 3} works... – Rmano May 28 '19 at 08:18
  • Can you also just set defaults partially? So something like: qar2/.style 2 args={ rectangle, draw=#1, thick, fill=#2 }, qar2/.default={blue}{} – BadAtLaTeX Jun 17 '19 at 22:02
  • With two arguments, is it possible to only specify the first and let the second #2 be the default value yellow? I can't get it to work. – O'Neil Nov 05 '23 at 21:50