![tikz 树节点下方的重叠标签](https://linux22.com/image/411138/tikz%20%E6%A0%91%E8%8A%82%E7%82%B9%E4%B8%8B%E6%96%B9%E7%9A%84%E9%87%8D%E5%8F%A0%E6%A0%87%E7%AD%BE.png)
我是 Latex 的新手,我正在使用 tikz 包绘制一个简单的二叉树。我想将一个长问题放在一个节点下,但它与箭头重叠。这是我的代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\tikzset {
my_circle/.style = {circle, draw= black}
}
\begin{tikzpicture}[->, sibling distance=4cm, level distance = 4cm]
\node [label=below:{I need to put here a long question!}, my_circle]{1}
child{node[my_circle] {2} edge from parent node[left] {yes}
}
child{node [my_circle]{3}
child{node [my_circle] {6}
edge from parent node [left] {yes}}
child {node [my_circle]{7}
edge from parent node[right] {no}}
edge from parent node [right] {no}}
;
\end{tikzpicture}
\end{document}
多谢!
答案1
欢迎光临!只是shorten
各自的边缘。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\tikzset {
my_circle/.style = {circle, draw= black}
}
\begin{tikzpicture}[->, sibling distance=4cm, level distance = 4cm]
\node [label=below:{I need to put here a long question!}, my_circle]{1}
child{node[my_circle] {2} edge from parent[shorten <=0.8cm] node[left] {yes}
}
child{node [my_circle]{3}
child{node [my_circle] {6}
edge from parent node [left] {yes}}
child {node [my_circle]{7}
edge from parent node[right] {no}}
edge from parent[shorten <=0.8cm] node [right] {no}}
;
\end{tikzpicture}
\end{document}
我建议改用forest
。有了这个包,你可以说类似
where level=1{edge={shorten <=6mm}}{},
以便缩短该层级上所有边的边数。还有更多功能。而且语法更简单。
\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
yes/.style={edge label={node[auto,pos=0.7,swap]{yes}}},
no/.style={edge label={node[auto,pos=0.7]{no}}},
where level=1{edge={shorten <=6mm}}{},
for tree={circle,draw,s sep+=2em,l sep+=3em}
[1,label=below:{I need to put here a long question!}
[2,yes]
[3,no
[6,yes]
[7,no]
]
]
\end{forest}
\end{document}