11

Thanks for helping me out.

How can I make the following tree-diagrams?

http://www.vias.org/tmdatanaleng/cc_prob_cond_independ.html

10

There is a nice example of a probability tree on TeXample.net. That might help you get started.

  • Isn't this more of a comment than an answer? – Svend Tveskæg Jun 7 '15 at 8:48
  • @Svend Well, I though it was an answer (albeit short) but I see no problem if a mod convert it to a comment. – chl Jun 8 '15 at 11:21
  • I'm not saying that is a comment; I'm just asking. :) – Svend Tveskæg Jun 8 '15 at 12:17
  • @SvendTveskæg Is it, however, objectionable on the grounds that it is a link-only answer? – cfr Apr 29 '18 at 1:16
8

A forest solution though there are better versions around if you search, I think. This is not exactly like the image you linked to because that one seems rather ugly to me. Obviously, you can adjust this if you prefer greater ugliness.

Update for Forest 2.1.5 (2017-07-14)

Surely there's a simpler way, Sašo?

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest,nicefrac}
\usetikzlibrary{positioning}
\begin{document}
\newcounter{forestalias}
\forestset{
  left marble colour/.store in=\leftmarblecolour,
  right marble colour/.store in=\rightmarblecolour,
  left marble colour=black,
  right marble colour=red,
  declare count register={marble count},
  marble count'=0,
  declare toks={marble}{black},
  declare keylist={marbles}{},
  define long step={more marbles}{}{%
    filter={root,tree}{>O+tt=!{marbles}{}}},
  declare keylist={make more marbles}{},
  probability tree/.style={
    for tree={
      grow=0,
      s sep'+=2.5mm,
      delay={
        edge label/.process={
          OO_=?_ w2 {content} {n}{1}{below}{above} {node [midway, ##2, font=\scriptsize, text=black] {##1}}
        },
        content=,
      },
    },
    for descendants={
      if n=1{marble=\leftmarblecolour}{marble=\rightmarblecolour},
    },
    before typesetting nodes={
      for tree={
        edge+/.option=marble,
        make marble=black,
      },
      where n children=0{
        tempkeylista'=,
        for nodewalk={
          c,
          while={>O_> {level}{1}}{u}
        }{
          spare marble/.option=marble,
          tempkeylista/.process={Rw{marble count}{m##1}},
        },
        marbles/.register=tempkeylista,
      }{},
    },
    make more marbles={
      for more marbles={
        tempcountc'=0,
        split option={marbles}{,}{make descendant marble},
      }
    },
    before drawing tree={
      where n children=0{
        TeX={\stepcounter{forestalias}},
        alias={n\theforestalias}
      }{}
    }
  },
  make descendant marble/.style={
    tempcountc'+=1,
    for nodewalk={L}{
      if={>O+tt={name}{#1}}{}{
        append/.expanded=#1, do dynamics
      }
    }
  },
  make marble/.style={circle, minimum width=2.5pt, fill=#1, draw=#1, inner sep=0pt},
  spare marble/.style={
    marble count'+=1,
    append/.expanded={[, make marble=#1, name=m\foresteregister{marble count}, grow=0, no edge, l'+=-10mm]},
  },
}
\begin{forest}(%
    stages={% customised definition of stages
      for root'={% nothing is removed from the standard forest definition - we only change it by adding to it
        process keylist register=default preamble,
        process keylist register=preamble,
      },
      process keylist=given options,
      process keylist=before typesetting nodes,
      % addition here
      process keylist=make more marbles,
      typeset nodes stage,
      process keylist=before packing,
      pack stage,
      process keylist=before computing xy,
      compute xy stage,
      process keylist=before drawing tree,
      draw tree stage,
    },
  )%
  probability tree,
  left marble colour=blue,
  [, name=t0 [\nicefrac{3}{5}[\nicefrac{5}{9}][\nicefrac{4}{9}]][\nicefrac{2}{5}, name=t1 [\nicefrac{2}{3}][\nicefrac{1}{3}, name=t2]]]
  \foreach \i/\j/\k/\l [count=\n] in {3/5/5/9,3/5/4/9,2/5/2/3,2/5/1/3}
  \node (l\n) at (n\n) [label={[label distance=2.5mm, inner sep=0pt, , font=\scriptsize]right:$\frac{\i}{\j}.\frac{\k}{\l}$}] {};
  \coordinate [above=7.5mm of t2] (m1);
  \path [draw, fill, font=\scriptsize\itshape] (m1 -| t0) circle (1pt) -- (m1 -| t1) node [midway, below] {first}  circle (1pt) -- (m1 -| t2) node [midway, below] {second}  circle (1pt) -- ([xshift=2.5mm]m1 -| n3) node [midway, below] {result} circle (1pt) -- ++(7.5mm,0) node [midway, below] {\phantom{l}p\phantom{l}} circle (1pt);
\end{forest}
\end{document}

Note that this could be done without redefining stages, I assume. I just found it easier to use that to experiment with the ordering. Unlike the original code, this relies on do dynamics, which is experimental. It does, however, produce a similar result:

v. 2.1.5 output

Original answer for earlier Forest (v. 1.something?)

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest,nicefrac}
\usetikzlibrary{positioning}
\begin{document}
\newcounter{forestalias}
\forestset{
  left marble colour/.store in=\leftmarblecolour,
  right marble colour/.store in=\rightmarblecolour,
  left marble colour=black,
  right marble colour=red,
  probability tree/.style={
    for tree={
      grow=0,
      marble=black,
      s sep+=2.5mm,
      if n=1{
        before typesetting nodes={
          edge label/.wrap pgfmath arg={node [midway, below, font=\scriptsize, text=black] {##1}}{content()},
        },
      }{
        before typesetting nodes={
          edge label/.wrap pgfmath arg={node [midway, above, font=\scriptsize, text=black] {##1}}{content()},
        },
      },
      before packing={
        content={},
        typeset node,
      },
    },
    for descendants={
      if n=1{make descendant marble=\leftmarblecolour}{make descendant marble=\rightmarblecolour},
    },
    before drawing tree={
      for tree={
        if n children=0{
          TeX={\stepcounter{forestalias}},
          alias={n\theforestalias}
        }{}
      }
    }
  },
  marble/.style={circle, minimum width=2.5pt, fill=#1, draw=#1, inner sep=0pt},
  make descendant marble/.style={
    edge=#1,
    delay n={int(level())}{
      for descendants={
        if n children=0{
          append={[, grow=0, marble=#1, no edge, l+=-10mm]}
        }{},
      },
    },
  },
}
\begin{forest}
  probability tree,
  left marble colour=blue
  [, name=t0 [\nicefrac{3}{5}[\nicefrac{5}{9}][\nicefrac{4}{9}]][\nicefrac{2}{5}, name=t1 [\nicefrac{2}{3}][\nicefrac{1}{3}, name=t2]]]
  \foreach \i/\j/\k/\l [count=\n] in {3/5/5/9,3/5/4/9,2/5/2/3,2/5/1/3}
    \node (l\n) at (n\n) [label={[label distance=2.5mm, inner sep=0pt, , font=\scriptsize]right:$\frac{\i}{\j}.\frac{\k}{\l}$}] {};
  \coordinate [above=7.5mm of t2] (m1);
  \path [draw, fill, font=\scriptsize\itshape] (m1 -| t0) circle (1pt) -- (m1 -| t1) node [midway, below] {first}  circle (1pt) -- (m1 -| t2) node [midway, below] {second}  circle (1pt) -- ([xshift=2.5mm]m1 -| n3) node [midway, below] {result} circle (1pt) -- ++(7.5mm,0) node [midway, below] {\phantom{l}p\phantom{l}} circle (1pt);
\end{forest}
\end{document}

probability tree

  • The code in your answer doesn't produce the same output as your image - the results column only has the first set of dots. Please could you edit it? – A. Goodier Apr 28 '18 at 18:04
  • @A.Goodier I think something has changed in Forest. I can't see how I can have got that using this method, but the code was obviously written before the current version of Forest became available. (At the least, before I got to grips with the new stuff, but I suspect from this, before it was published.) – cfr Apr 29 '18 at 0:59
  • 1
    @A.Goodier Please see edit above. – cfr May 1 '18 at 0:09

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.