\usebox 与 tikz-uml

\usebox 与 tikz-uml

我希望能够使用 TikZ-UML 在状态图中重用组件。我创建了一个包含两个组件的savebox调用\giraffe,我想将它们放置在初始节点上方和最终节点下方。代码似乎没有产生所需的结果。知道为什么吗?

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

注意:上面的代码可以编译,但是tikz-uml是非标准包;可以从以下位置下载这里

答案1

希望这可以满足您的需求。这里的关键是使用分离 tikzpicture存储放置组件的盒子。

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

在此处输入图片描述

答案2

TiKZ 3.0您可以使用 声明并使用pics。是pic您可以在图表中多次重复使用的简短图片。 下面的这段代码显示了如何giraffe使用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}

在此处输入图片描述

如您所见,结果与 Steven 的解决方案不同,但我不知道tikz-uml定位系统如何做到这一点。我尝试用 替换below of=语法(我认为它已被弃用),但它也修改了组件below = of的内部组成。umlbasicstate

相关内容