2

Currently I am trying to create a tree diagram. I need to fit this into a beamer slide. This is what I did so far:

\fbox{
\begin{tikzpicture}[level/.style={sibling distance=60mm/#1}]
\node[circle,draw] (0){$0$}
  child {node[circle,draw] (A) {$A$}
    child {node[circle,draw] (B) {$B$}
      child {node[circle,draw](C) {C}
        child {node[circle,draw] (D) {$D$}}
        child {node[circle,draw] (E) {$E$}}
            } 
      child {node[circle,draw](F){$F$}}
          }
    child {node[circle,draw](G) {$G$}
      child {node[circle,draw](H) {$H$}}
      child {node[circle,draw](I) {$I$}}
          }
        }
  child {node[circle,draw](J) {$J$}
    child {node[circle,draw](K) {$K$}
      child {node[circle,draw](L) {$L$}}
      child {node[circle,draw](M){$M$}}
          }
  child {node [circle,draw] (N) {$N$}
   % child {node[circle,draw](O) {$O$}}
    child {node[circle,draw](P){$P$}
      child {node [circle,draw](Q) {$Q$}}
      child {node [circle,draw](R) {$R$}
% ----------------------------------------
%text on the right -----------------------
       child [grow=right] {node (q) {\void} edge from parent[draw=none]
          child [grow=right] {node (q) {4. Stufe} edge from parent[draw=none]
            child [grow=up] {node (r) {3. Stufe} edge from parent[draw=none]
              child [grow=up] {node (s) {2. Stufe} edge from parent[draw=none]
                child [grow=up] {node (t) {1. Stufe} edge from parent[draw=none]
                  child [grow=up] {node (u) {Start} edge from parent[draw=none]}
% ----------------------------------------
                }
              }
            }
          }
        }
      }
    }
  }
};

% Wahrscheinlichkeiten der Pfade
\path (0) -- (A) node [midway, left]  {\; $\frac{o}{a}$ \;};
\path (0) -- (J) node [midway, right] {\; $\frac{o}{j}$ \;};

\path (A) -- (B) node [midway, left] {\; $\frac{a}{b}$ \;};

\path (B) -- (C) node [midway, left] {\; $\frac{b}{c}$ \;};
\path (B) -- (F) node [midway, right] {\; $\frac{b}{f}$ \;};
 \path (C) -- (D) node [midway, left] {\; $\frac{c}{d}$ \;};
 \path (C) -- (E) node [midway, right] {\; $\frac{c}{e}$ \;};

\path (A) -- (G) node [midway, right] {\; $\frac{a}{g}$ \;};
 \path (G) -- (H) node [midway, left] {\; $\frac{g}{h}$ \;};
 \path (G) -- (I) node [midway, right] {\; $\frac{g}{i}$ \;};

\path (J) -- (K) node [midway, left] {\; $\frac{j}{k}$ \;};
 \path (K) -- (L) node [midway, left] {\; $\frac{k}{l}$ \;};
 \path (K) -- (M) node [midway, right] {\; $\frac{k}{m}$ \;};

\path (J) -- (N) node [midway, right] {\; $\frac{j}{n}$ \;};
 \path (N) -- (P) node [midway, right] {\; $\frac{n}{p}$ \;};
   \path (P) -- (Q) node [midway, left] {\; $\frac{p}{q}$ \;};
   \path (P) -- (R) node [midway, right] {\; $\frac{p}{r}$ \;};    

\end{tikzpicture}
}

Now i got two questions: How can I add a fifth level to my diagram and how does it fit to a beamer slide? And the second: How can I highlight certain branches of the diagram in a different color?

  • 1
    Please complete your example. If it needs to fit a beamer slide, it needs the beamer class etc. and we need the theme you are using etc. The code does not compile even when I add the obvious class and package to the file. – cfr Jun 21 '15 at 14:28
  • What exactly do you mean by "highlight certain branches"? Do you mean how to colorize some edges of the tree? – Gonzalo Medina Jun 21 '15 at 15:54
7

You add a fifth level just the way you added the other levels. Of course, it has to fit on the slide!

forest is especially good at drawing compact trees. Here's a version using this package:

compact forest

This actually uses a forest style which I wrote as an experiment. The code for the example is

\documentclass{beamer}

\usepackage{etex,prooftrees}% remove etex if unneeded

\begin{document}
\begin{frame}[fragile]
  \fbox{\begin{forest}
    proof tree,
    no line numbering,
    single branches,
    for tree={
      circle,
      draw,
      math content,
      minimum size=2em,
      inner sep=1.5pt,
      before drawing tree={
        if level=0{}{
          if n'=1{
            edge label/.wrap 2 pgfmath args={node [midway, right=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          }{
            edge label/.wrap 2 pgfmath args={node [midway, left=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          },
        }{}
      },
      l sep+=3mm,
    },
    [, phantom
      [0, name=0, just=Start
        [A, name=A, just={1. Stufe}
          [B, name=B, just={2. Stufe}
            [C, name=C, just={3. Stufe}
              [D, name=D, just={4. Stufe}]
              [E, name=E]
            ]
            [F, name=F]
          ]
          [G, name=G
            [H, name=H]
            [I, name=I]
          ]
        ]
        [J, name=J
          [K, name=K
            [L, name=L]
            [M, name=M]
          ]
          [N, name=N
            [P, name=P
              [Q, name=Q
                [S, name=S, just={5. Stufe}]
                [T, name=T]
              ]
              [R, name=R]
            ]
          ]
        ]
      ]
    ]
  \end{forest}}
\end{frame}

\end{document}

You do not need etex unless your TeX installation is very up-to-date, and you will not need it in a few days when the bug fix propagates through CTAN. If you have an updated TeX Live 2015, you will need it temporarily.

The code for the prooftrees package is here. Save as prooftrees.sty in the same directory as your .tex file.

To colour branches, you can use for tree={<colour>, edge={<colour>}}. For example, if I add to the node A as follows

, for tree={green!50!black, edge={green!50!black}}

and to K as follows

, for tree={blue, edge={blue}}

reverting the G branch to black with

, for tree={black, edge={black}}

then I get

colouring branches

Complete code (excluding prooftrees.sty):

\documentclass{beamer}

\usepackage{etex,prooftrees}% remove etex if unneeded

\begin{document}
\begin{frame}[fragile]
  \fbox{\begin{forest}
    proof tree,
    no line numbering,
    single branches,
    for tree={
      circle,
      draw,
      math content,
      minimum size=2em,
      inner sep=1.5pt,
      before drawing tree={
        if level=0{}{
          if n'=1{
            edge label/.wrap 2 pgfmath args={node [midway, right=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          }{
            edge label/.wrap 2 pgfmath args={node [midway, left=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          },
        }{}
      },
      l sep+=3mm,
    },
    [, phantom
      [0, name=0, just=Start
        [A, name=A, just={1. Stufe}, for tree={green!50!black, edge={green!50!black}}
          [B, name=B, just={2. Stufe}
            [C, name=C, just={3. Stufe}
              [D, name=D, just={4. Stufe}]
              [E, name=E]
            ]
            [F, name=F]
          ]
          [G, name=G, for tree={black, edge={black}}
            [H, name=H]
            [I, name=I]
          ]
        ]
        [J, name=J
          [K, name=K, for tree={blue, edge={blue}}
            [L, name=L]
            [M, name=M]
          ]
          [N, name=N
            [P, name=P
              [Q, name=Q
                [S, name=S, just={5. Stufe}]
                [T, name=T]
              ]
              [R, name=R]
            ]
          ]
        ]
      ]
    ]
  \end{forest}}
\end{frame}

\end{document}

EDIT

If you don't want the edges to originate from a single point on the parent, you can add parent anchor=, to the for tree={...} invocation:

...
for tree={
  circle,
  draw,
  parent anchor=,
  math content,
  minimum size=2em,
  inner sep=1.5pt,
  ...

to get

edges from different points on parent

A similar approach would use justtrees which is a simplified version of prooftrees. The code is here. Save it as justtrees.sty in the same directory as your .tex file.

The output is essentially the same but the package is using somewhat simpler code and might, possibly, compile a bit faster (I'm not sure).

\documentclass{beamer}

\usepackage{etex,justtrees}% remove etex if unneeded

\begin{document}
\begin{frame}[fragile]
  \fbox{\begin{forest}
    just tree,
    for tree={
      circle,
      draw,
      parent anchor=,
      math content,
      minimum size=2em,
      inner sep=1.5pt,
      before drawing tree={
        if level=0{}{
          if n'=1{
            edge label/.wrap 2 pgfmath args={node [midway, right=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          }{
            edge label/.wrap 2 pgfmath args={node [midway, left=.5em] {\ensuremath{\frac{#2}{#1}}}}{content()}{content("!u")},
          },
        }{}
      },
      l sep+=3mm,
    },
    [, phantom
      [0, name=0, right just=Start
        [A, name=A, right just={1. Stufe}, for tree={green!50!black, edge={green!50!black}}
          [B, name=B, right just={2. Stufe}
            [C, name=C, right just={3. Stufe}
              [D, name=D, right just={4. Stufe}]
              [E, name=E]
            ]
            [F, name=F]
          ]
          [G, name=G, for tree={black, edge={black}}
            [H, name=H]
            [I, name=I]
          ]
        ]
        [J, name=J
          [K, name=K, for tree={blue, edge={blue}}
            [L, name=L]
            [M, name=M]
          ]
          [N, name=N
            [P, name=P
              [Q, name=Q
                [S, name=S, right just={5. Stufe}]
                [T, name=T]
              ]
              [R, name=R]
            ]
          ]
        ]
      ]
    ]
  \end{forest}}
\end{frame}

\end{document}

just tree with right justifications

EDIT

In response to the discussion in comment, \scalebox{<scale>}{\begin{forest}...\end{forest}} can be used to scale the tree. However, making it half the size will make it difficult or impossible for the audince to read. Removing the fractions on the edges will help a little bit. If some of these fractions are really essential, they can be added directly using edge node or a style my label can be created to create the fractions based on node content when required.

For example:

\documentclass{beamer}

\usepackage{etex,justtrees}% remove etex if unneeded

\begin{document}
\begin{frame}[fragile]
  \fbox{\scalebox{.5}{\begin{forest}
    my label/.style={
      if n'=1{
        edge label/.wrap 2 pgfmath args={node [midway, right=.5em] {\ensuremath{\frac{##2}{##1}}}}{content()}{content("!u")},
      }{
        edge label/.wrap 2 pgfmath args={node [midway, left=.5em] {\ensuremath{\frac{##2}{##1}}}}{content()}{content("!u")},
      },
    },
    just tree,
    for tree={
      circle,
      draw,
      parent anchor=,
      math content,
      minimum size=2em,
      inner sep=1.5pt,
      l sep+=3mm,
    },
    [, phantom
      [0, name=0, right just=Start
        [A, name=A, right just={1. Stufe}, for tree={green!50!black, edge={green!50!black}}
          [B, name=B, right just={2. Stufe}, my label
            [C, name=C, right just={3. Stufe}
              [D, name=D, right just={4. Stufe}]
              [E, name=E]
            ]
            [F, name=F]
          ]
          [G, name=G, for tree={black, edge={black}}
            [H, name=H]
            [I, name=I]
          ]
        ]
        [J, name=J
          [K, name=K, for tree={blue, edge={blue}}, my label
            [L, name=L]
            [M, name=M]
          ]
          [N, name=N
            [P, name=P
              [Q, name=Q
                [S, name=S, my label, right just={5. Stufe}]
                [T, name=T, my label]
              ]
              [R, name=R]
            ]
          ]
        ]
      ]
    ]
  \end{forest}}}
\end{frame}

mini-tree

Note that I really do not recommend making the tree this small unless either you wish to annoy your audience or you know that your audience will consist entirely of bright-eyed eagles.

  • +1. Nice. (and some more dummy text so it lets me post my comment). – Gonzalo Medina Jun 21 '15 at 17:05
  • Any plans on making prooftrees available from CTAN? – Gonzalo Medina Jun 21 '15 at 17:13
  • @GonzaloMedina I'm not sure whether I should. I would at least like somebody to test it before I do that. Right now, I'm the only person who has used it as far as I know. There's actually a second version, justtrees which I probably should have used for this answer and which is very similar. (It doesn't do automated line numbering as prooftrees can.) Also, I wasn't sure whether the promised beta of the new forest might break things. I really would like a way to automatically add the phantom root node, but I've not yet figured out a way to do it. – cfr Jun 21 '15 at 18:28
  • If you want to, I would volunteer for testing the package. Depending on the actual scope of the package you could then decide if it is worth to upload it to CTAN, but at first sight, I'd say it is worth it. – Gonzalo Medina Jun 22 '15 at 0:34
  • @GonzaloMedina Update code is here. Almost certainly an abuse of the SE site, but whatever. I don't have any documentation at the moment, though. I'll see if I can find time to write some. It is not really intended to be all that customisable. However, you can specify explicit single branches (default is not), no line numbering (default is numbered) and perhaps one or two other things I've forgotten about. It is rather messy. – cfr Jun 22 '15 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.