问题:我在绘制树时卡住了。实际问题是我希望在节点 F 正下方绘制另外两个节点(名称分别为 H 和 I)。但我无法做到,因为节点重叠。
梅威瑟:
\documentclass[10pt]{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\node [circle,draw]{A} [level distance=10mm,sibling distance=25mm]
child { node [circle,draw]{B} [level distance=10mm ,sibling distance=15mm]
child {node [circle,draw] {D}
child[missing]
child {node [circle,draw]{G}}}
child {node [circle,draw]{F}}
}
child {node [circle,draw] {C} [level distance=10mm ,sibling distance=15mm]
child {node [circle,draw] {E}
child[missing]
child {node [circle, draw] {J}}
}
child {node [circle,draw] {F}
child[missing]
child {node [circle, draw] {K}}
}
};
\end{tikzpicture}
\end{center}
\end{document}
答案1
您可以将D
节点向左移动一点以腾出空间,例如
node [circle,draw,xshift=-5mm] {D}
也可以用之前的方法减少H
和之间的距离,I
node [circle,draw]{F} [sibling distance=7mm] child {node[circle,draw]{H}} child {node[circle,draw]{I}}
如果您不需要强调missing
子节点,则无需进行任何移动,但我不知道这样是否可以。无论如何,请参见下面代码中的第二个示例。在下面的前两个示例中,我使用了\path[every node/.style={circle,draw}]
树,那么您不必[circle, draw]
为每个节点指定。
另一种更自动化的绘制树的方法是forest
使用包。我还添加了一个示例。
\documentclass{article}
\usepackage{forest} % loads tikz
\begin{document}
\begin{center}
Small adjustments:
\begin{tikzpicture}
\path[every node/.style={circle,draw}]
node {A} [level distance=10mm,sibling distance=25mm]
child { node {B} [level distance=10mm ,sibling distance=15mm]
child {node [xshift=-5mm] {D}
child[missing]
child {node {G}}}
child {node {F} [sibling distance=7mm]
child {node{H}}
child {node{I}}}
}
child {node {C} [level distance=10mm ,sibling distance=15mm]
child {node {E}
child[missing]
child {node {J}}
}
child {node {F}
child[missing]
child {node {K}}
}
};
\end{tikzpicture}
Larger adjustments:
\begin{tikzpicture}
\path[every node/.style={circle,draw}]
node {A} [level distance=10mm,sibling distance=25mm]
child { node {B} [sibling distance=13mm]
child {node {D}
child {node {G}}}
child {node {F} [sibling distance=10mm]
child {node{H}}
child {node{I}}}
}
child {node {C} [sibling distance=10mm]
child {node {E}
child {node {J}}
}
child {node {F}
child {node {K}}
}
};
\end{tikzpicture}
Forest:
\begin{forest}
for tree={draw,circle}
[A
[B
[D [G] ]
[F [H] [I] ]
]
[C
[E [J]]
[F [K]]
]
]
\end{forest}
\end{center}
\end{document}