![TikZ:如何对齐具有不同兄弟距离的节点兄弟?](https://linux22.com/image/356703/TikZ%EF%BC%9A%E5%A6%82%E4%BD%95%E5%AF%B9%E9%BD%90%E5%85%B7%E6%9C%89%E4%B8%8D%E5%90%8C%E5%85%84%E5%BC%9F%E8%B7%9D%E7%A6%BB%E7%9A%84%E8%8A%82%E7%82%B9%E5%85%84%E5%BC%9F%EF%BC%9F.png)
我正在尝试使用 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}