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}
3
  • Welcome to TeX.SE¨! What you try?
    – Zarko
    Dec 2, 2023 at 8:50
  • @Zarko Thanks. couldn't write code here so I edit the question. Dec 2, 2023 at 11:08
  • That is right that you edit your question. Now all can see your effort and will see, where you stuck. I will try to help you latter afternoon ...
    – Zarko
    Dec 2, 2023 at 12:36

1 Answer 1

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

9
  • Thanks, that was great but is there any way to use tree easier? Dec 2, 2023 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 2, 2023 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 3, 2023 at 1:22
  • See 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 3, 2023 at 1: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? Dec 3, 2023 at 6:05

You must log in to answer this question.

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