1

I'm new to latex and I'm using the tikz package to draw a simple binary tree. I want to put a long question under a node but it's overlapping with the arrows. Here's my code:

\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}

By doing this I have this result: Overlapping arrows

What I'm trying to do is to start the arrows after the question: Desired result

Thanks a lot!

1

Welcome! Just shorten the respective edges.

\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}

enter image description here

I'd recommend switching to forest. With this package you can say things like

where level=1{edge={shorten <=6mm}}{},

in order to shorten the edges in all edges at that level. And much more. And the syntax is simpler.

\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}

enter image description here

| improve this answer | |
  • Thank you a lot for the fast answer! I'd like also to ask you what are the advantages of the forest? – Max Apr 7 at 10:21
  • @Max It is just so much more powerful. I added an example, but in this one you cannot really see the advantages. However, if you go to more complicated trees, you will. – user194703 Apr 7 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.