我正在尝试绘制一条边,该边从下图中的节点 A 和 B 之间的边开始,到新节点 J,如下所示。我能够从 B 和 C 之间的边到新节点 K 绘制一条边,但出于某种原因,我无法让到 J 的边看起来正确。
代码如下:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
[sibling distance=16em,level distance=8em, every node/.style={shape=rectangle,draw,align=center}]
\tikzstyle{level 3}=[sibling distance=20em]
\tikzstyle{level 4}=[sibling distance=8em]
\node (A){A}
child{node(B){B}
child{node{C}
child{node{D}
child{node{E}}
child{node{F}}
}
child{node{G}
child{node{H}}
child{node{I}}
}
}
};
\path (A) -- node[right=7em] (J){J} (A-1) (J) edge (J-|A);
\path (B) -- node[right=7em] (K){K} (B-1) (K) edge (K-|B);
\end{tikzpicture}
\end{document}
谢谢!
答案1
编辑:由于 OP 改变了问题...新的解决方案可以简单地从原始答案中得出:
尝试
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
node distance = 7em,
every node/.style={shape=rectangle,draw,align=center},
sibling distance=16em,
level distance=8em,
level 3/.style = {sibling distance=20em},
level 4/.style = {sibling distance=8em}
]
%
\node (a) {A}
child{node (b) {B} % <---
child{node (c) {C} % <---
child{node{D}
child{node{E}}
child{node{F}}
}
child{node{G}
child{node{H}}
child{node{I}}
}
}
};
\path (a) -- coordinate (j1)
node[draw, right=7em] (j2) {J} (b) (j2) edge[draw] (j1);
\path (b) -- coordinate (k1)
node[draw, right=7em] (k2) {K} (c) (k2) edge[draw] (k1);
\end{tikzpicture}
\end{document}
附录:forest
如果你使用包, 代码会更加简单:
\documentclass[margin=5mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree = {
% nodes
draw,
% tree
edge = {-, semithick},
s sep = 4em,
l sep = 4em,
}
%
[A, name=a
[B, name=b
[C, name=c
[D
[E]
[F]
]
[G
[H]
[I]
]
]
]
]
\path (a) -- coordinate (j1)
node[draw, right=7em] (j2) {J} (b) (j2) edge[draw] (j1);
\path (b) -- coordinate (k1)
node[draw, right=7em] (k2) {K} (c) (k2) edge[draw] (k1);
\end{forest}
\end{document}