我有一个关于 tikz 树的简单问题,其中我想要一个没有圆形框的边缘标签...我知道我有every node=circle
,但我不知道如何为该特定元素更改它。请帮忙 ;)
我的代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm}]
\tikzstyle{every node}=[circle,draw]
\node (Root) [red] {3}
child {
node {1}
child { node {4} edge from parent node[left] {help!} }
child { node {1} }
child { node {3} }
}
child {
node {2}
child { node {3} }
child { node {2} }
child { node {5} }
};
\end{tikzpicture}
\end{document}
答案1
更新
可以使用强大的forest
包裹:
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={circle,draw, l sep=20pt}
[3,red
[1
[4,edge label={node[midway,left] {Help!}} ]
[1]
[3]
]
[2
[3]
[2]
[5]
]
]
\end{forest}
\end{document}
初始版本
作为艾伦·芒恩在他的回答中说,使用tikz-qtree
可能是更好的选择。但是,如果出于某种原因,您想坚持使用您的代码,则可以使用以下draw=none
选项:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm}]
\tikzstyle{every node}=[circle,draw]
\node (Root) [red] {3}
child {
node {1}
child { node {4} edge from parent node[left,draw=none] {help!} }
child { node {1} }
child { node {3} }
}
child {
node {2}
child { node {3} }
child { node {2} }
child { node {5} }
};
\end{tikzpicture}
\end{document}
答案2
与所有像这样的简单树一样,使用tikz-qtree
包裹:
\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}[every tree node/.style={draw,circle},
level distance=1.25cm,sibling distance=.5cm,
edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node[red] {3};
[.1
\edge node[auto=right] {Help!};
[.4 ] [.1 ] [.3 ]
]
[.2
[.3 ] [.2 ] [.5 ]
] ]
\end{tikzpicture}
\end{document}
答案3
还有一种样式叫做every child node
,它应用于孩子其余部分保持不变。这基本上可以满足您的要求,但您必须手动将样式应用于根节点。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm},
tree node/.style={circle,draw},
every child node/.style={tree node}]
\node[tree node] (Root) [red] {3}
child {
node {1}
child { node {4} edge from parent node[left] {help!} }
child { node {1} }
child { node {3} }
}
child {
node {2}
child { node {3} }
child { node {2} }
child { node {5} }
};
\end{tikzpicture}
\end{document}