1

I have a problem with a decision tree, two of the three edges are detached from the parent node and i can't understand why. I have a lot of trees on my thesis and this tree is the only one who gives me problems. (I mean it's probably not a package error)

Thanks in advance!

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{figure}[htbp]
\centering  
\begin{tikzpicture}[
sibling distance=4cm,
level distance=2.5cm,
every node/.style = { 
    align=center,
    top color=white
}
]
\node[ellipse][draw](Tipo di macchina){Tipo di macchina}
child { 
    node[draw, align=left] {C0:1\\C1:3}         
    edge from parent node(Tipo di macchina)[above left,pos=.6] {Monovolume}}
child {
    node[draw, align=left] {C0:8\\C1:0}
    edge from parent node(Tipo di macchina)[above right,pos=.6]{Sportiva}}
child {
    node[draw, align=left] {C0:1\\C1:7}
    edge from parent node(Tipo di macchina)[above right,pos=.2]{Lussuosa}}; 
\end{tikzpicture}
\vspace{0.2cm}
\caption{Esempio B}
\end{figure}
\end{document}

Here the output of the tree

enter image description here

4
  • 1
    Welcome to TeX.se. Please don't post code fragments, but instead put the fragment into a complete, compilable document that people can play with.
    – Alan Munn
    Jan 2, 2018 at 18:36
  • Ok sorry! I will change it immediately!
    – IvanComp
    Jan 2, 2018 at 18:39
  • But please make it minimal. Only the packages needed to make the fragment compile (in this case tikz and the shapes.geometric library).
    – Alan Munn
    Jan 2, 2018 at 18:44
  • 1
    Ok now it should be ok, sorry I was not sure about what packages are used
    – IvanComp
    Jan 2, 2018 at 18:50

2 Answers 2

2

The problem is caused by the fact that you have given identical node labels to each of the edge from parent nodes. If you make these labels distinct, the tree comes out as you wish:

In fact, unless you are further referencing these nodes in the picture, you really don't need to give them labels at all.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
    \begin{tikzpicture}[
    sibling distance=4cm,
    level distance=2.5cm,
    every node/.style = { 
        align=center,
    }
    ]
    \node[ellipse,draw](Tipo di macchina){Tipo di macchina}
    child { 
        node[draw, align=left] {C0:1\\C1:3}         
        edge from parent 
            node(Mono)[left,pos=.6] {Monovolume}
          }
    child {
        node[draw, align=left] {C0:8\\C1:0}
        edge from parent node(Sport)[above right,pos=.6]{Sportiva}
        }
    child {
        node[draw, align=left] {C0:1\\C1:7}
        edge from parent node(Lusso)[above right,pos=.2]{Lussuosa}
        }; 
    \end{tikzpicture}

\end{document}

output of code

0
1

Another solution to use the istgame package:

enter image description here

\documentclass[border=2pt]{standalone}

\usepackage{istgame}
\usepackage{makecell}

\begin{document}

\begin{istgame}
\xtdistance{25mm}{40mm}
\istrooto(0){Tipo di macchina}
  \istb{\mbox{Monovolume}}[above left,near end]
  \istb{\mbox{Sportiva}}[right]
  \istb{\mbox{Lussuosa}}[above right,near start]
  \endist
\xtNode(0-1)[box node]{\makecell{C0:1\\C1:3}}
\xtNode(0-2)[box node]{\makecell{C0:8\\C1:0}}
\xtNode(0-3)[box node]{\makecell{C0:1\\C1:7}}
\end{istgame}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .