4

I am trying to build a tizktree that should look like the following:

enter image description here

As of now my latex code is given by:

\begin{tikzpicture}
\node {r} [grow'=right]
child {node {$C^h$}  
child {node {$C^h$} }
child {node {$C^l$}}
}
child {node {$C^l$}
child {node {$C^h$}}
child {node {$C^l$}}
};
\end{tikzpicture}

The issue is that this code is making some of the nodes overlap and I am unsure on how to label the arrows with the P^h and P^l.

Any help would be appreciated.

  • Please complete your code so that it can be compiled. – cfr Jun 7 '16 at 15:52
3

If forest is an option, it will automatically adjust the layout of the tree to accommodate the nodes. It also allows trees to be specified very concisely and is very customisable in terms of the tree's appearance. For example:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    grow'=0,
    math content,
    tier/.wrap pgfmath arg={tier #1}{level()},
    l sep+=5mm,
  },
  before typesetting nodes={
    for tree={
      if n=1{
        edge label/.wrap value={node [midway, font=\scriptsize, above] {$#1$}}
      }{
        edge label/.wrap value={node [midway, font=\scriptsize, anchor=north] {$#1$}}
      }
    }
  }
  % now to specify the tree itself - as you see, the syntax is quite concise
  [r
    [C^h, edge label=P^h
      [C^h, edge label=P^h]
      [C^l, edge label=P^l]
    ]
    [C^l, edge label=P^l
      [C^h, edge label=P^h]
      [C^l, edge label=P^l]
    ]
  ]
\end{forest}
\end{document}

Forest tree

EDIT

A comment asked how to do without the root node. Any tree must have a root, however, it can be an invisible one which takes no space. Forest provides the phantom option for this kind of case:

\begin{forest}
  for tree={
    grow'=0,
    math content,
    tier/.wrap pgfmath arg={tier #1}{level()},
    l sep+=5mm,
  },
  before typesetting nodes={
    for tree={
      if n=1{
        edge label/.wrap value={node [midway, font=\scriptsize, above] {$#1$}}
      }{
        edge label/.wrap value={node [midway, font=\scriptsize, anchor=north] {$#1$}}
      }
    }
  }
  [, phantom
    [C^h, edge label=P^h
      [C^h, edge label=P^h]
      [C^l, edge label=P^l]
    ]
    [C^l, edge label=P^l
      [C^h, edge label=P^h]
      [C^l, edge label=P^l]
    ]
  ]
\end{forest}

phantom root

  • Thanks for your answer. It works like charm. Just a quick question, how would I need to change the code if I want to eliminate the first node? I.e. if I want to start the tree with the first two elements: C^h and C^l? – phdstudent Jun 7 '16 at 16:26
  • 1
    @volcompt Please see edit and also, take a look at Forest's manual for further customisation options and examples. (Or check out the forest tag on this site.) – cfr Jun 7 '16 at 21:05

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.