如何为与 tikz-uml 的关联命名?

如何为与 tikz-uml 的关联命名?

考虑以下一段代码:

\begin{tikzpicture} 
\umlsimpleclass[x=0,y=0]{Human}
\umlsimpleclass[x=10,y=0]{Pet}
\umlassoc[mult1=1, mult2=1..*, pos1=0.1, pos2=0.9]{Human}{Pet}
\end{tikzpicture}

我想在 Human 和 Pet 的关系线上方添加单词“owns”。我希望该词位于关系线的中间。问题是,如果我使用 \umlassoc 的“arg1”、“arg2”或“arg”属性中的任何一个,它会将该词放在多重性上方(在关系线的右侧或左侧,但不在中间)。

有人知道我怎样才能做我想做的事吗?

答案1

  1. 使用选项为关联命名name;例如owns

    \umlassoc[..., name=owns]{Human}{Pet}
    

    owns-1然后,将沿着名为、 、 ...的关系路径定义多个位置。owns-2事实上,对于这个简单的路径,只有中间的位置称为owns-1。对于有多个角的路径,还有更多。要找出有哪些,可以通过反复试验(依次尝试owns-1owns-2等)或阅读包的文档tikz-uml

  2. 添加\node语句以在上面定义的点处添加标签。在您的示例中使用例如

    \node[above] at (owns-1) {owns};
    

enter image description here

\documentclass[border=2mm]{standalone}
\usepackage{tikz-uml}
\begin{document}
\begin{tikzpicture} 
\umlsimpleclass[x=0,y=0]{Human}
\umlsimpleclass[x=10,y=0]{Pet}
\umlassoc[mult1=1, mult2=1..*, pos1=0.1, pos2=0.9, name=owns]{Human}{Pet}
\node[above] at (owns-1) {owns};
\end{tikzpicture}
\end{document}

相关内容