我是 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}