16

How do I provide multiple arguments for pic?

giraffe contains two arguments for colors, #1 and #2. I would like to be able to provide these as giraffe={pink, yellow} or giraffe={colorA=pink, colorB=yellow}. The syntax does not really matter.

\documentclass[border=3mm]{standalone}

\usepackage{tikz}
\usepackage{tikz-uml}
\usetikzlibrary{arrows,positioning,shapes}
\begin{document}

\tikzset{
    giraffe/.pic={
        \umlbasicstate[x=-1.5, y=0, fill=#1, width=15ex]{test1}
        \umlbasicstate[x=1.5, y=0, fill=#2, width=15ex]{test2}
    }%
}


\begin{tikzpicture}[auto]
\umlstateinitial[x=0, y=11, name=initial]
\umlbasicstate[x=0, y=9, name=a, fill=white, width=15ex]{a}
\umlbasicstate[x=0, y=6, name=b, fill=white, width=15ex]{b}
\umlbasicstate[x=0, y=3, name=c, fill=white, width=15ex]{c}
\umlstatefinal[x=0, y=1, name=final]

\draw pic[below of = final] {giraffe={pink, yellow}};          % below final
\draw pic[above of =  initial] {giraffe={blue, green}};        % above initial

\umltrans{initial}{a}
\umltrans{a}{b}
\umltrans{b}{c}
\umltrans{c}{final}

\end{tikzpicture}

\end{document}

In the manual, pic is in Section 18.3 Defining New Pic Types. This is in connection with another question where someone suggested picas an answer.

I've also checked this question, but I'm having difficulty applying it to my problem with pic.

24

When the command is a bit more complicated then you want to use the general syntax

\tikzset{pics/giraffe/.style={code={....with #1...}}}

The /.style is part of the pgfkeys handler mechanism and it has a direct analogue with two arguments namely /.style 2 args. So you could write

\tikzset{pics/giraffe/.style 2 args={code={
  \umlbasicstate[x=-1.5,y=0,fill=#1,width=15ex]{test1}
  \umlbasicstate[x=-1.5,y=0,fill=#2,width=15ex]{test2}}}}

to be called as \draw pic {giraffe={pink}{yellow}}. Similary there is /.style n args={5}{code={...}} that will define a version with 5 arguments.

However, there are other syntaxes available. If it is just the colours you are setting then prehaps you would like to write

\draw pic {giraffe=pink/yellow};

This can be acheived with

\tikzset{
    pics/giraffe/.style args={#1/#2}{
      code = {
        \umlbasicstate[x=-1.5, y=0, fill=#1, width=15ex]{test1}
        \umlbasicstate[x=1.5, y=0, fill=#2, width=15ex]{test2}
    }}}

The first argument to /.style args provides a pattern which is used for extracting the arguments to be used in the code.

In your example this gives you:

Sample output

\documentclass[border=3mm]{standalone}

\usepackage{tikz}
\usepackage{tikz-uml}
\usetikzlibrary{arrows,positioning,shapes}
\begin{document}

\tikzset{
    pics/giraffe/.style args={#1/#2}{
      code = {
        \umlbasicstate[x=-1.5, y=0, fill=#1, width=15ex]{test1}
        \umlbasicstate[x=1.5, y=0, fill=#2, width=15ex]{test2}
    }}}

\begin{tikzpicture}[auto]
\umlstateinitial[x=0, y=11, name=initial]
\umlbasicstate[x=0, y=9, name=a, fill=white, width=15ex]{a}
\umlbasicstate[x=0, y=6, name=b, fill=white, width=15ex]{b}
\umlbasicstate[x=0, y=3, name=c, fill=white, width=15ex]{c}
\umlstatefinal[x=0, y=1, name=final]

\draw pic[below of = final] {giraffe=pink/yellow};          % below final
\draw pic[above of =  initial] {giraffe=blue/green};        % above initial

\umltrans{initial}{a}
\umltrans{a}{b}
\umltrans{b}{c}
\umltrans{c}{final}

\end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.