TransWikia.com

Tree labeling and node drawing

TeX - LaTeX Asked by alessandro_archetti on July 29, 2021

How can I add the label to the alternative paths (y_1, y_2, a_1,a_2…exc) and how can I transform the second children in squared as in figure 1, using forest?

This is my code:

begin{forest}
    [,circle, draw,grow=east
      [,circle, draw,grow=east,[,grow=east][,circle, draw,grow=east[...,grow=east][$u(a_1;theta_1)$,grow=east]]][,circle, draw,grow=east[,grow=east][,circle, draw,grow=east[$u(a_1;theta_2)$,grow=east][$u(a_1;theta_1)$,grow=east]]]
    ]
end{forest}

And figure2 is what I got.enter image description here

enter image description here

One Answer

Assuming you have a vanilla preamble i.e. load forest with no libraries etc.

documentclass[border=10pt]{standalone}
usepackage{forest}
usetikzlibrary{shapes.geometric}

begin{document}
begin{forest}
  for tree={grow=east,l sep'+=2em,s sep'+=1em},% l sep controls the length of branches, s sep controls how much they're pushed apart
  before typesetting nodes={
    where={>O_=O_=!&{content}{}{n children}{0}}% if there's no content but there are children
    {draw,
      if level=1{regular polygon,regular polygon sides=4}{circle}
    }{anchor=parent},% if there is content or there aren't children, align anchors towards parent
  },
  my label/.style={if={>O_={n}{1}}{edge label={node[below,midway,font=scriptsize]{$#1$}}}{edge label={node[above,midway,font=scriptsize]{$#1$}}}}% if it's the first child, put the label below; o'w put it above
    [
      [,my label=y_2[,my label=a_2][,my label=a_1[$cdots$,my label=theta_2][$u(a_1;theta_1)$,my label=theta_1]]]
      [,my label=y_1[,my label=a_2][,my label=a_1[$u(a_1;theta_2)$,my label=theta_2][$u(a_1;theta_1)$,my label=theta_1]]]
    ]
end{forest}
end{document}

labelled tree

Answered by cfr on July 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP