3

I want to create this tree that each node have its own shape. for example each nodes be circle and leaves be square. enter image description here

this code perfectly work but I want to use tree in my code.

\begin{tikzpicture}
    \matrix [column sep=1cm, row sep=1cm] {
        & & \node (in) [draw, shape=circle] {Income}; \\
        \node (lo) [draw, shape=rectangle, minimum size=1cm]    {Loan}; & &
        \node (ms) [draw, shape=circle] {MS}; & & &
        \node (cr) [draw, shape=circle] {CR}; \\
        & \node (lo2) [draw, shape=rectangle, minimum size=1cm] {Loan}; & &
        \node (noLoan) [draw, shape=rectangle, minimum size=1cm] {No loan}; &
        \node (lo3) [draw, shape=rectangle, minimum size=1cm] {Loan}; & &
        \node (noLoan2) [draw, shape=rectangle, minimum size=1cm] {No loan};\\
    };
    \draw[->] (in) -- (lo);
    \draw[->] (in) -- (ms);
    \draw[->] (in) -- (cr);
    \draw[->] (ms) -- (lo2);
    \draw[->] (ms) -- (noLoan);
    \draw[->] (cr) -- (lo3);
    \draw[->] (cr) -- (noLoan2);
\end{tikzpicture}

1 Answers1

5

Drawing as tree by use of the forest package. A wee bit sophisticated but short code:

\documentclass[margin=3mm]{standalone}
\usepackage{forest} % it load tikz too
\usetikzlibrary{arrows.meta}

\begin{document} \begin{forest} for tree = { % nodes styles draw, minimum size=3em, inner sep=2pt, where level = 2{}{circle, draw}, align = center, anchor = north, %child anchor=north, % tree style edge = {-Straight Barb, semithick}, s sep = 12 mm, l sep = 16 mm, where level = 1{s sep=2mm}{}, % edge labels EL/.style 2 args = {edge label={% node[midway, font=\footnotesize, align=center, inner sep=2pt, anchor=south #1]{#2}}, }, }
[Income, [loan, rectangle, EL={east}{high}] [MS, EL={east}{medium}, before computing xy={s/.average={s}{siblings}} [loan, EL={east}{maried}] [no\ loan, EL={west}{no\ married}]
] [CR, EL={west}{low}, [loan, EL={east}{good}] [no\ loan, EL={west}{poor}]
] ] \end{forest} \end{document}

enter image description here

Zarko
  • 296,517
  • Thanks, that was great but is there any way to use tree easier? – Erfan Riahi Dec 02 '23 at 16:14
  • @ErfanRiahi, what you mean with "easier"? Code is simple, concise and relative easy to understand (regardless that I said that it is sophisticated ;-), this I mean in comparison to your codein question. – Zarko Dec 02 '23 at 16:54
  • You don't need where inside for tree - you just add additional processing. It's more efficient to use if or to take the where outside the for tree. The same goes for the style definition. You don't need to define for each node individually ;). – cfr Dec 03 '23 at 01:22
  • See https://chat.stackexchange.com/transcript/message/64801439#64801439. @ErfanRiahi You asked for a tree. forest provides one fairly straightforward way to create a tree. If this isn't what you wanted, can you clarify what you do want? – cfr Dec 03 '23 at 01:37
  • @cfr No, it's ok. thanks. another question: how can I use this tree in beamer? and is it possible to use pause for each node? – Erfan Riahi Dec 03 '23 at 06:05
  • @ErfanRiahi To use it in Beamer, you just load forest and put the tree inside a frame. I doubt you can use \pause, but you can specify the slides on which each node of the tree appears using TikZ styles. See e.g. http://tex.stackexchange.com/a/55849/, http://tex.stackexchange.com/a/6155/ and http://tex.stackexchange.com/a/112471/ and http://tex.stackexchange.com/a/112895/. I also have some posts specific to forest e.g. https://tex.stackexchange.com/a/417110/, https://tex.stackexchange.com/a/392608/, https://tex.stackexchange.com/a/393437/ and https://tex.stackexchange.com/a/417110/. – cfr Dec 03 '23 at 06:17
  • @ErfanRiahi, if you have new problems, please ask new question that other people perhaps can help you. My answer (perfectly) solve your question: show how to draw your image as tree. Regarding to your recent comments, it becoming unclear, what is your problem. – Zarko Dec 03 '23 at 06:28
  • @Zarko Yes your answer was really helpful and I'm thankful about that but after that I had a new problem. if I couldn't solve it, I'll ask a new question with more clear deatail. – Erfan Riahi Dec 03 '23 at 06:41
  • @ErfanRiahi, good to hear this. So, now you may consider to accept answer (by clicking on the check mark at top left side of answer) and by this inform audience here, that your question, as it is, is solved. – Zarko Dec 03 '23 at 06:59