3

I am trying to generate a simple diagram by using tikz. I couldn't figure it out how to set equal distance between sibling nodes to stop overlapping. The code that I have tried so far:

\documentclass[]{article}

\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{er,positioning,shadows,trees}


\begin{document}

\begin{figure}[h] 
    \caption{Diagram}
    \begin{tikzpicture}[auto,node distance=0.5cm]
    \node[entity] (node1) {Engine}
    [grow=down,sibling distance=2.5cm, align=center]
    child {node[attribute] {A}}
    child {node[attribute] {B}}
    child {node[attribute] {C}}
    child {node[attribute] {Long label for node}}
    child {node[attribute] {D}};

    \node[relationship] (rel1) [text width=1.6cm, align=center, above = of node1] {Protocol};

    \node[entity] (node2) [above left = of rel1] {Player 1};
    \node[entity] (node3) [above right = of rel1] {Player 2};

    \path (node1) edge node {} (rel1) edge node {} (node1);
    \path (node2) edge node {} (rel1) edge node {} (node2);
    \path (node3) edge node {} (rel1) edge node {} (node3);
    \end{tikzpicture}
\end{figure}

\end{document}

The result that I get is:

enter image description here

tdgunes
  • 151

1 Answers1

3

You could use forest for the tree.

Addtionally, as cfr notes in a comment using just h as the float specifier is often not very useful. For example, it will often lead to this: `h' float specifier changed to `ht' warning when not attempting to specify a float

enter image description here

\documentclass{article}   
\usepackage{forest}
\usetikzlibrary{er,positioning}
\begin{document}
\begin{figure}
    \centering
    \caption{Diagram}
    \begin{forest}
    for tree={
     if n children=0{attribute}{entity},
     l'=2cm,
     parent anchor=children,child anchor=parent
    }
    [Engine,name=node1
    [A] [B] [C] [Long label for node] [D]
    ]
    % empty lines not allowed
    \node[relationship] (rel1) [text width=1.6cm, align=center, above = of node1] {Protocol};
    \node[entity] (node2) [above left = of rel1] {Player 1};
    \node[entity] (node3) [above right = of rel1] {Player 2};
    \draw (node1) -- (rel1) -- (node2) 
                     (rel1) -- (node3);
    \end{forest}
\end{figure}
\end{document}

Or if manual adjustments are OK, keep your code and add some xshift.

enter image description here

\documentclass[]{article}

\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{er,positioning,shadows,trees}


\begin{document}

\begin{figure}[h] 
    \caption{Diagram}
    \begin{tikzpicture}[auto,node distance=0.5cm]
    \node[entity] (node1) {Engine}
    [grow=down,sibling distance=2.5cm, align=center]
    child {node[attribute] {A}}
    child {node[attribute,xshift=-4mm] {B}}
    child {node[attribute,xshift=-10mm] {C}}
    child {node[attribute,xshift=-4mm] {Long label for node}}
    child {node[attribute] {D}};

    \node[relationship] (rel1) [text width=1.6cm, align=center, above = of node1] {Protocol};

    \node[entity] (node2) [above left = of rel1] {Player 1};
    \node[entity] (node3) [above right = of rel1] {Player 2};

    \draw (node1) -- (rel1) -- (node2) 
                     (rel1) -- (node3);

    \end{tikzpicture}
\end{figure}

\end{document}
Torbjørn T.
  • 206,688
  • 1
    (+1) parent anchor=children, child anchor=parent is more flexible (but does not work with v1 of Forest, although that's quite old by now). l'=2cm is faster than l=2cm. – cfr Jun 14 '17 at 23:10
  • 1
    Maybe worth pointing out that specifying h as the only acceptable location for a float is pointless. – cfr Jun 14 '17 at 23:12