I'm really new to TikZ (but not to LaTeX) because currently I write an important work in which I want to use some nice scalable graphics.
My current state is that I can define node types with \tikzset
. I would like to do the same for edges, so I need edge types, in graphs because I need different kinds of edges.
I'd like to start with a minimal example of what I have done so far:
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{shapes}
\usetikzlibrary{graphs}
\usetikzlibrary{quotes}
\begin{document}
\tikzset{
role/.style = {rounded rectangle, minimum size=6mm, very thick, draw=blue!50,top color=white,bottom color=blue!20, font=\ttfamily\scriptsize,align=center,text=black},
hv coll/.style = {to path={-| (\tikztotarget)}},
vh coll/.style = {to path={|- (\tikztotarget)}},
}
\begin{tikzpicture}[node distance=5mm and 5mm,thick,black!50,graphs/every graph/.style={edges=rounded corners}]
% nodes
\node[role] (n1) {Node 1};
\node[role] (n2) [below left=of n1] {Node 2};
\node[role] (n3) [below right=of n1] {Node 3};
\node[role] (n4) [below right=of n2] {Node 4};
% connections
\graph [use existing nodes] {
n1 ->[hv coll] n3 ->[vh coll] n4;
n1 ->[hv coll] n2 -> ["label" near end,font=\tiny] n3;
n2 ->[vh coll] n4;
};
\end{tikzpicture}
\end{document}
Here the edges are all equal. I'd like to have edges similar to the UML edges: composition, association, inheritance and own other edges.
What I tried between nodes n1
and n3
is the following: n1 {diamond}->[hv coll] n3
But here I get this error: No shape named n1 {diamond} is known
Furthermore I'd like to know how to define such edges in \tikzset
so that I could use a defined edge name (such as e.g. comp
for the composition edge). Then I need to define the font for edge labels, too, because it must be avoided to repeat the specification near end,font=\tiny
(seen for the label between nodes n2
and n3
) for each and every edge.
That was the first part of my question regarding edge types.
Then I'd like to know if TikZ has an inheritance concept. For example: the style hv
is meant to draw edges first horizontal and then vertical. Assume a new edge type comp
, how can I define something like hv comp
without copying/merging the separate definitions (hv
and comp
)?
This question is interesting, too, in general. So, e.g., can node types inherit from other node types.
hv
and then definehv comp/.style={hv,<other options>}
. However, keys are set in the order they are parsed and also set keys need to be unset if change is needed. So typical inheritance is not available. However if you are interested you can read more the OO Programming section in TikZ manual.