3

I’m trying to make a horizontal probability tree as pictured below using the Forest package. enter image description here

Currently, I am able to get a regular forest tree using the following code

\usepackage{forest}
\begin{document}
\begin{forest}
    [A
        [B[D][E]]
        [C[F][G]]
    ]
\end{forest}
\end{document}

I also know that you can add edge labels manually by specifying, for instance:

[D;edge label = {node[midway,above]{$\frac{3}{5}$}}]

However, since I will be making these trees quite frequently, I was hoping to have the edge labels automatically placed either above or below. As well, there may be more than two branches per node, in which the edge label should default to the top.

I’ve seen code like:

[D;$\frac{3}{5}$]

But I’m not too sure how to achieve that.

  • Welcome. Is the related tree an option? – Bobyandbob Apr 6 '18 at 20:20
  • seems to be duplicate to tex.stackexchange.com/questions/364996/… – Zarko Apr 6 '18 at 21:14
  • Can you please provide compilable code? – cfr Apr 7 '18 at 3:07
  • Welcome! It would be easier if you gave a link to wherever you've 'seen code like' the fragment you included, too. After all, it is likely that there may be a hint there as to 'how to achieve that', don't you think? – cfr Apr 7 '18 at 3:25
7

Please provide compilable code and be sure that your example demonstrates the problem. For example, if you need cases with more than 2 branches, you need to provide an example in which some cases have more than 2 branches. It is also not obvious, because your code is incomplete, whether you have a tree growing in your desired direction or whether that is also a problem for you. Generally, it is all a bit underspecified.

Here's an example based on the code you gave, a modified version of the syntax specified in your desiderata (I don't think it is good to choose a semicolon here, as that is ubiquitous in TikZ's syntax) and your target picture. Adjust to taste.

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{
  ptree/.style={
    for tree={
      grow'=0,
      parent anchor=children,
      child anchor=parent,
    },
    before typesetting nodes={
      for tree={
        split option={content}{:}{content, my edge label},
      },
    },
  },
  my edge label/.style={
    if={
      > O_= {n'}{1}
    }{
      edge label={node [midway, below, font=\footnotesize] {$\frac#1$} }
    }{
      edge label={node [midway, above, font=\footnotesize] {$\frac#1$} }
    },
  }
}
\begin{document}
\begin{forest}
  ptree
  [A
    [B:12
    [D:{3}{5}][E:{2}{5}]
    ]
    [C:12
      [F:{1}{4}][G:{3}{4}]
    ]
  ]
\end{forest}
\end{document}

<code>ptree</code> result

  • how do we adapt it to take normal text/number in case supplied instead of fraction? – Parthiban Rajendran Nov 24 '18 at 13:55
  • @PaariVendhan Don't use \frac and just use e.g. 0.34 rather than {1}{3} or whatever you need. – cfr Nov 25 '18 at 16:40
  • I mean, in some places frac and in some places decimals, how do we have a unified style to take both types and apply accordingly? – Parthiban Rajendran Nov 25 '18 at 17:02
  • @PaariVendhan Using different styles would probably be more efficient, but you can probably conditionalise it if you want. You need to be much clearer about the syntax you want to support, though. I doubt it can be answered in a comment. – cfr Nov 28 '18 at 2:39
  • sure, will post it as a Q when needed. I thought that might be marked as duplicate of this. – Parthiban Rajendran Nov 28 '18 at 3:21
1

Peraphs is it adapt for you this alternative solution?

enter image description here

\documentclass[a4paper,12pt]{article}

\usepackage{tikz-cd}
\usepackage{mathtools}

\begin{document}
\begin{tikzcd}
 &  & D \\
 & B \arrow[ru, "\dfrac35", no head] \arrow[r, "\tfrac25"', no head] & E \\
A \arrow[ru, no head] \arrow[rd, "\dfrac12"', no head] &  & F \\
 & C \arrow[ru, "\dfrac14", no head] \arrow[rd, "\dfrac34"', no head] &  \\
 &  & G
\end{tikzcd}
\end{document}

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.