4

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 (hvand comp)? This question is interesting, too, in general. So, e.g., can node types inherit from other node types.

2
  • you can define your hv and then define hv 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.
    – percusse
    Commented May 22, 2014 at 16:12
  • Thanks for the hint of the quasi-inheritance. I think that's enough for my purpose. I'll check the OO chapter in the manual, too. I haven't dug that far yet. Any hints for the first part of my question regarding the edge style for graphs?
    – ErnstZ
    Commented May 23, 2014 at 7:34

1 Answer 1

1

I found out that the graph concept is not suited best for my purpose. I could solve my problems with a path definition instead of a graph.
Here is the fully working corrected example from above:

\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/.style = {to path={-| (\tikztotarget)}},
    vh/.style = {to path={|- (\tikztotarget)}},
    comp/.style = {Diamond-},
    ass/.style = {-{>[width=2pt 2]}},
    imp/.style = {-{Latex[open]}},
    proh/.style = {{Bar[sep=1pt]}-{Bar[sep=1pt]}}
}

\begin{tikzpicture}[node distance=5mm and 5mm,thick,
    every edge/.style={rounded corners,draw=black!50,font=\tiny,near end}]
    % 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
    \path
        (n1) edge[comp,hv] (n2)
        (n1) edge[comp,hv] (n3)
        (n2) edge[ass,"label"] (n3)
        (n4) edge[imp,hv] (n2)
        (n3) edge[proh,vh] (n4);
\end{tikzpicture}

\end{document}

The edge kinds are defined in \tikzset: comp,ass,inh,proh. Here, ass stands for association. Furthermore, the defaults for the edge labels could be defined in the configuration of the tikzpicture itself via every edge/.style.

That's an acceptable solution for me.

You must log in to answer this question.

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