1

I'm a complete newbie to Tikz and I want to create a decision tree similar to the image below (which I did in Paint). Does anybody know how I can do this in Latex?enter image description here

My attempt still is far away from what I want:

    \documentclass[tikz,border=5pt]{standalone}
    \usepackage{tikz}
    \usetikzlibrary{positioning}

    \begin{document}

    \begin{tikzpicture}[grow=right]
    \node(A){A}
child[sibling distance=25mm]{node{1}
child{node{} edge from parent[] node[right,xshift=10]{}
child[sibling distance=10mm]{node{5}
child{node{} edge from parent[] node[right,xshift=10]{}}
}
child[sibling distance=10mm]{node{6}
child{node{} edge from parent[] node[right,xshift=10]{}}
}
}
}
child[sibling distance=25mm]{node{2}
child{node{} edge from parent[] node[right,xshift=10]{}
child[sibling distance=10mm]{node{3}
child{node{} edge from parent[] node[right,xshift=10]{}}
}
child[sibling distance=10mm]{node{4}
child{node{} edge from parent[] node[right,xshift=10]{}}
}
}
};
    \node[left=of A](B){B};
    \draw[](B)--(A);
    \end{tikzpicture}
    \end{document}
0

2 Answers 2

4

A quick forest proposal.

\documentclass[border=3.14mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={grow'=east,child anchor=south east,parent anchor=south east,edge=thick}
[[Label1[
 [[Label2
   [[Label 4
     [[Label 6
       [[Label 10]]
     ]]
     [[Label 7
       [[Label 11]]
     ]]
   ]]
 ]]
 [[Label3
   [[Label 5
     [[Label 8
       [[Label 11]]
     ]]
     [[Label 9
       [[Label 12]]
     ]]
   ]]
 ]]
]]]
\end{forest}
\end{document}

enter image description here

2

Less quick forest proposal. Since your MWE (which determine nodes in tree) and sketch (which is not clear if it determine only edge labels or nodes too) differ in number of tree's levels, question is unclear to me. As compromise between both is:

\documentclass[margin=3mm]{standalone}
\usepackage{forest}

\begin{document}
\forestset{
    anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
    decision edge label/.style n args=2{%
            edge label/.expanded={node[pos=0.48, inner sep=1pt, anchor=#1]{#2}}
                                       },
  decision/.style={if n=1{decision edge label={south}{#1}}
                         {decision edge label={north}{#1}}
                   },
  decision tree/.style={
    for tree={
    /tikz/every node/.append style={font=\footnotesize},
        font=\normalsize, inner xsep=4pt,
        grow' = east,
    if n children=1{anchors=east}{anchors=west},
    if level = 1{l sep=24mm,s sep=4mm}{l sep=8mm,s sep=3mm},
    edge path'={
    (!u.parent anchor) -- ([xshift=2mm]!u.parent anchor |- .child anchor) -- (.child anchor)
                },
                    },
    delay={for descendants={split option={content}{;}{content,decision}}},
                        }% end of decision tree
        }% end of forestset
%---------------------------------------------------------------%
\begin{forest} decision tree
[
    [1; in
        [1;option 11\quad text
            [;aaa]
            [;bbb]
        ]
        [2;option 12\quad text
            [;ccc]
            [;ddd]
        ]
    ]
]
\end{forest}
\end{document}

enter image description here

2
  • Seems like you copied split option={content}{;}{content,decision} from here without acknowledging the source. :-(
    – user121799
    May 20, 2019 at 16:28
  • Almost every answer here use some source, for example books about (La)TeX, documentations of TikZ˙, forest, etc. In my answer I use solution described in forest documentation, page 83-84 (5.1 Decision tree). I also see, that this solution uses others too, for example cfr . So, to whom I (we) should gives acknowledgements? To DEK, Laslie Lamport, David Carlisle, Till Tauntau ... ? So far, I have been of the opinion that this site is intended to help people (with care about duplicate questions) and not to publish original scientific works, where citing sources is of key importance.
    – Zarko
    May 20, 2019 at 17:47

You must log in to answer this question.

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