TikZ:如何对齐具有不同兄弟距离的节点兄弟?

TikZ:如何对齐具有不同兄弟距离的节点兄弟?

我正在尝试使用 tikz 生成一个简单的图表。我无法弄清楚如何设置兄弟节点之间的相等距离​​以停止重叠。到目前为止,我尝试过的代码:

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

我得到的结果是:

在此处输入图片描述

答案1

您可以将其用于forest树。

此外,正如 cfr 在评论中指出的那样,使用h浮点说明符通常不是很有用。例如,它通常会导致这种情况:当不尝试指定浮点数时,浮点说明符‘h’更改为‘ht’警告

在此处输入图片描述

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

或者如果手动调整可以的话,保留您的代码并添加一些xshift

在此处输入图片描述

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

相关内容