我正在努力解决一个问题。为了更清楚地显示树的不同层级,我想在树的每个层级之间画一条线(即一条直线,将层级 i 到层级 j 的所有边都切断)。你能帮我吗?
\begin{document}
\begin{frame}
\begin{tikzpicture}[level distance=6mm]
\tikzstyle{every node}=[draw,circle,minimum size=0.5em, inner sep=1pt]
\node {r}
child {node {\tiny{1}}}
child {node {\tiny{1}}
child {node {\tiny{2}}
child {node {\tiny{3}}}
child {node {\tiny{3}}}
child {node {\tiny{3}}}}};
\end{tikzpicture}
\end{frame}
\end{document}
答案1
您的问题不太清楚。此外,您仅提供了使用过时语法来定义节点样式的代码片段。因此,以下 MWE 是基于我的猜测:
\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}
\begin{tikzpicture}[
every node/.style = {circle, draw, minimum size=0.75em,
inner sep=1pt, font=\tiny},
level distance = 6mm,
level 2/.style = {sibling distance = 6mm},
]
\node {r}
child {node (n1) {1}}
child {node {2}
child {node (n2) {2}}
child {node {3}}
child {node {3}}
child {node (n5) {5}}
};
\draw[dashed, shorten <=-5mm, shorten >=-5mm]
([yshift=1.5mm] n1.north) -- ([yshift=1.5mm] n1.north -| n5.east);
\draw[dashed, shorten <=-5mm, shorten >=-5mm]
([yshift=1.5mm] n2.north -| n1) -- ([yshift=1.5mm] n2.north -| n5.east);
\end{tikzpicture}
\end{frame}
\end{document}
结果图像非常小: