2

I would like to be able to reuse components in a state diagram using TikZ-UML. I created a savebox called \giraffe with two components inside and I would like to place these above the initial node and below the final node. The code doesn't seem to produce the required result. Any idea why?

\documentclass{report}

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

\begin{tikzpicture}[auto]

\newsavebox{\giraffe}
\savebox{\giraffe}{
  \umlbasicstate[x=0, y=0, fill=pink, width=15ex]{test1}
  \umlbasicstate[x=3, y=0, fill=yellow, width=15ex]{test2}
}

\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]

\node [below of=final] {\usebox{\giraffe}};          % below final
\node [above of=initial] {\usebox{\giraffe}};        % above initial

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

\end{tikzpicture}
\end{document}

Note: the code above is compiling but tikz-uml is a non-standard package; it can be downloaded from here.

5
  • Using \def instead of a box approach wouldn't save computer cycles, but would save typing. Which savings is your goal? Commented Mar 24, 2014 at 14:58
  • Saving typing. Will \def still allow me to choose the location of the inner nodes? [ Note: I was able to successfully do this using simple rectangles, but for some reason similar code does not work with \umlbasicstate. ] Commented Mar 24, 2014 at 15:12
  • Could you also post a code version that compiles, but contains all the extra typing that you would like to eliminate? Commented Mar 24, 2014 at 15:16
  • This one compiles on my machine. If it is not compiling it may be because the package tikz-uml does not come by default. I downloaded the .sty from perso.ensta-paristech.fr/~kielbasi/tikzuml. Commented Mar 24, 2014 at 15:23
  • Ok, I see it compiles. I don't have the answer, but I suspect the problem has to do with the fact that you are trying to specify x and y coordinates in something that you are hoping will be able to float around. Commented Mar 24, 2014 at 15:26

2 Answers 2

2

Hopefully, this can be adapted to your need. The key here is using a separate tikzpicture to store the box in which you are placing the components.

\documentclass{report}

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

\newsavebox{\giraffe}
\savebox{\giraffe}{%
\begin{tikzpicture}[auto]
  \umlbasicstate[x=0, y=0, fill=pink, width=15ex]{test1}
  \umlbasicstate[x=3, y=0, fill=yellow, width=15ex]{test2}
\end{tikzpicture}%
}

\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]

\node [below of=final] {\usebox{\giraffe}};          % below final
\node [above of=initial] {\usebox{\giraffe}};        % above initial

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

\end{tikzpicture}
\end{document}

enter image description here

2

With TiKZ 3.0 you can declare and use pics. A pic is a short picture that you can reuse several times in your diagram. Next code shows how to use giraffe as a pic.

\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=pink, width=15ex]{test1}
        \umlbasicstate[x=1.5, y=0, fill=yellow, 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};          % below final
\draw pic[above of =  initial] {giraffe};        % above initial

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

\end{tikzpicture}

\end{document}

enter image description here

As you can see, the result is different from Steven's solution but I don't know tikz-uml positioning system to do it. I've tried to replace below of= syntax (I think it's deprecated) with below = of but it also modified internal composition of umlbasicstate components.

2
  • Is it possible to provide both colors as argument? I checked Section 18.3 Defining New Pic Types of the manual and can do it for one color but not both. The idea would be to use it as \draw pic[below of = final] {giraffe={colorA=pink, colorB=yellow}}; (syntax doesn't matter as long as both arguments can be provided). [ Note: I don't know if a new question is required for this. ] Commented Mar 25, 2014 at 10:01
  • I've added it as a new question at tex.stackexchange.com/q/167556/47170. Commented Mar 25, 2014 at 10:10

You must log in to answer this question.

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