1

I have the following tree diagram (packages specified in the heading).

\usepackage{tikz}
\usetikzlibrary{trees}
\usepackage{tikz-cd}

% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

\begin{tikzpicture}[grow=right]
\node[bag] {Start}
    child {
        node[bag] {\euro -$1$}        
            child {
                node[end, label=right:
                    {\euro -$2$}] {}
                edge from parent
                node[above] {$1$}
                node[below]  {}
            }
            child {
                node[end, label=right:
                    {\euro $0$}] {}
                edge from parent
                node[above] {$1$}
                node[below]  {}
            }
            edge from parent 
            node[above] {$\frac{1}{2}$}
            node[below]  {}
    }
    child {
        node[bag] {\euro $1$}        
        child {
                node[end, label=right:
                    {\euro $0$}] {}
                edge from parent
                node[above] {$1$}
                node[below]  {}
            }
            child {
                node[end, label=right:
                    {\euro $2$}] {}
                edge from parent
                node[above] {$1$}
                node[below]  {}
            }
        edge from parent         
            node[above] {$\frac{1}{2}$}
            node[below]  {}
    };
\end{tikzpicture}

I obtain the following layout:

enter image description here

But, what I wish is that both €0 join together. That is, just one €0 and then the middle arrows pointing at the unique €0 in the middle. In other words, I would like to have a tree with common children in the middle points. I really searched around without success and, on the other hands, it seems like this should be easily achieved by tweaking a bit the code.

Thanks for any tips or suggestions!

6
  • 3
    Welcome. This isn't really a tree anymore. Seems like something for the istgame package or just tikz-cd: Q684735 Oct 31, 2023 at 11:20
  • Maybe it receives a different name in such a case. I know little about tex diagrams in general.
    – Martingalo
    Oct 31, 2023 at 15:43
  • 1
    Please always make the code you post compilable. It makes it much easier for people to help.
    – cfr
    Oct 31, 2023 at 18:17
  • 1
    See tex.stackexchange.com/a/565550, actualy your question is duplicate to this.
    – Zarko
    Oct 31, 2023 at 18:18
  • 1
    That's right! Thank you! I really searched but did not find it. In any case, I really like the matrix solution here below, but if duplicates are not allowed, I'm okay with deleting. Thanks for the help!
    – Martingalo
    Oct 31, 2023 at 19:22

1 Answer 1

2

For a simple diagram like this, here's one way to do it with basic Tikz.

Basic ideas:

  • use \matrix, which acts like a table for Tikz to position e.g. nodes
  • this one has 3 columns ( & & \\) and 5 rows
  • some fields are empty by intention
  • put nodes with content like yours where needed
  • assign (S), (A1) etc. to reference their coordinates later, again
  • for simplicity I used "e" to keep the matrix/table-structure visible
  • use column sep and row sep for visual fine tuning

After the matrix, once having placed all nodes with all text, do some drawings, like

  • \draw (S) -- (A1); % to draw a line between nodes S and A1
  • \draw (S) -- node[above] {$\frac{1}{2}$} (A1);% same, but put some text near the line

Things you may want to look up in the pgfmanual:

  • 3rd tutorial, "5.3 Aligning the Nodes Using Matrices"
  • edges as an alternative to place labels

result

\documentclass[10pt,border=3mm,tikz]{standalone}

\begin{document}
 \begin{tikzpicture}
    \matrix[column sep=12mm,
            row sep=4mm]{
                &                       & \node(B1) {$e_2$};\\
                & \node(A1) {$e_1$};    &\\
\node(S) {Start};   &                   & \node(B2) {$e_0$};\\
                & \node(A2) {$e_{-1}$}; &\\
                &                       & \node(B3) {$e_{-2}$};\\
    };
    
    \draw (S)  -- node[above] {$\frac{1}{2}$} (A1);
    \draw (S)  -- node[above] {$\frac{1}{2}$} (A2);
    
    \draw (A1) -- node[above] {$\frac{1}{3}$} (B1);
    \draw (A1) -- node[above] {$\frac{2}{3}$} (B2);
    \draw (A2) -- node[below] {$\frac{1}{4}$} (B2);
    \draw (A2) -- node[below] {$\frac{3}{4}$} (B3);
 \end{tikzpicture}
\end{document}
1
  • 1
    Oh thanks! That helped a lot. I could even reason the code to have one more column, so it is very "user friendly". Thanks a lot!
    – Martingalo
    Oct 31, 2023 at 18:14

You must log in to answer this question.

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