3

I am trying to reproduce the following

enter image description here

So far I got this:

\begin{tikzpicture}[]
\small
\node[] (AB) {$A \succeq B$};
\node[]{}
    child{node[chance]{C}
      edge from parent
        node[above]{1-p}    
                }
    child{node[chance]{A} 
      edge from parent
          node[below]{p}
        };
\end{tikzpicture}

My basic problem is to label and connect simples nodes. So what is the command for "place this node A \succeq B left to the decision tree with decent distance"? Thank you very much.

Mac
  • 1,611
  • 3
  • 17
  • 42

2 Answers2

3

Here are two options showing how to produce the trees using two of the most popular packages for tree generation; the first one using the forest package and the second one using tikz-qtree:

\documentclass{article}   
\usepackage{forest}
\usepackage{tikz-qtree}

\begin{document}

\[
A \succeq B
\Rightarrow
\begin{forest}
baseline,
for tree={grow=0,parent anchor=east,l sep=35pt} 
[
  [C,edge label={node[sloped,pos=0.5,below,font=\scriptsize]{$1-p$}}]
  [A,edge label={node[sloped,pos=0.5,above,font=\scriptsize]{$p$}}]
] 
\end{forest}
\succeq
\begin{forest}
baseline,
for tree={grow=0,parent anchor=east,l sep=35pt} 
[
  [C,edge label={node[sloped,pos=0.5,below,font=\scriptsize]{$1-p$}}]
  [B,edge label={node[sloped,pos=0.5,above,font=\scriptsize]{$p$}}]
] 
\end{forest}
\]

\[
A \succeq B
\Rightarrow
\begin{tikzpicture}[baseline,grow=right,level distance=35pt]
\Tree[
  \edge node[sloped,pos=0.5,below,font=\scriptsize]{$1-p$}; C
  \edge node[sloped,pos=0.5,above,font=\scriptsize]{$p$}; A
] 
\end{tikzpicture}
\succeq
\begin{tikzpicture}[baseline,grow=right,level distance=35pt]
\Tree[
  \edge node[sloped,pos=0.5,below,font=\scriptsize]{$1-p$}; C
  \edge node[sloped,pos=0.5,above,font=\scriptsize]{$p$}; B
] 
\end{tikzpicture}
\]

\end{document}

The result:

enter image description here

Gonzalo Medina
  • 505,128
  • The code looks simple. But it gives me fatal errors, I cannot compile. Are there known package-clashes to forest? I use some other tikz-pictures in my code. – Mac Aug 05 '15 at 14:53
  • @Mac As a first step, please compile just my code (with no additions, in a simple test document). Does it give you errors? And if so, which ones exactly. – Gonzalo Medina Aug 05 '15 at 14:55
  • Ah ok, even MWE doesnt work. It's again some problem with package- loading. It says "forest.sty not found"... I'm working on it ... package manager says in turn: too many requests from my IP ... so there is a delay in testing your approach, which is certainly fine. So thanks anyway :) – Mac Aug 05 '15 at 14:58
  • @Mac I see, so the forest package isn't installed in your system. Once you've installed it, the code should work with no problems. I could also give an alternative using tikz-qtree if you want to. – Gonzalo Medina Aug 05 '15 at 15:04
  • @Mac In the meantime I updated my answer with an option using tikz-qtree. – Gonzalo Medina Aug 05 '15 at 15:22
  • The new qtree works! The code looks more inflated but this is fine (because it's more easy to understand). But same question as to LaRiFaRi: how can I scale this? A surrounding tikzpicture doesn't work here. The results is just too small. – Mac Aug 07 '15 at 08:03
  • @Mac In the qtree.tikz version, since it already uses a tikzpicture, you can use scale=<factor>, but this is not really good since the text will also scale. Control the lengths using level distance and sibling distance. – Gonzalo Medina Aug 07 '15 at 12:54
  • @Mac or do you mean to scale all the expression and not just to increase the lengths in the trees? – Gonzalo Medina Aug 07 '15 at 13:01
  • Yes, the entire expression with []. I cannot put a tikzpicture around all this and just scale the surrounding. Proportional scaling of all content. – Mac Aug 07 '15 at 13:03
  • @Mac you can first box the whole thing and then use \scalebox. – Gonzalo Medina Aug 07 '15 at 13:10
  • Sry for my extensive questioning. But the direct command \scalebox{2}{ content } does not work. Which box-environment I should use? – Mac Aug 07 '15 at 13:13
  • @Mac As I said, you need to box the thing first. Search the place for lrbox; in case of doubts, you can always opean a new question. – Gonzalo Medina Aug 07 '15 at 13:15
2

As Mr. Kumar says, I would go with . The symbol declaration is a bit of an over-kill here, but you may use it in other graphs as well even for vertical and diagonal arrows.

% arara: pdflatex

\documentclass{article}
\usepackage{tikz-cd}
\tikzset{%
    symbol/.style={%
        draw=none,
        every to/.append style={%
            edge node={node [sloped, allow upside down, auto=false]{$#1$}}}
    }
}

\begin{document}
\[
\begin{tikzcd}[every label/.append style={sloped}, row sep=tiny] % or small
& & & A & & B\\
A  \arrow[symbol={\succeq}]{r} & B \arrow[Rightarrow]{r} & \null\arrow[start anchor=center, dash]{ur}[above]{p}\arrow[start anchor=center,dash]{dr}[below]{1-p} & \arrow[symbol={\succeq}]{r} & \null\arrow[start anchor=center, dash]{ur}[above]{p}\arrow[start anchor=center,dash]{dr}[below]{1-p} & \\
& & & C & & D
\end{tikzcd}
\]
\end{document} 

enter image description here

If you want a more consistent spacing, you might want to add the \Rightarrow as a symbol as well.

\arrow[symbol=\Rightarrow]{r}

enter image description here

If you want to get tighter horizontal spacing, you can define each & of the first line to any value you like. E.g. &[-3ex] & & A & & B\\ might look good.

Another possibility would be to write the first part as a normal formula. Delete the first & from the first and last line and start the second one with A \succeq B \arrow[Rightarrow]{r}.

LaRiFaRi
  • 43,807
  • For simple trees like the one in the image, tikz-cd could be an option but for larger trees, perhaps I wouldn't use it. – Gonzalo Medina Aug 05 '15 at 14:51
  • @GonzaloMedina For sure. That's true. – LaRiFaRi Aug 05 '15 at 14:51
  • I can't neither compile this. Obviously I use several forbitten package-combinations ... what could be the most likely reason? – Mac Aug 05 '15 at 14:55
  • @Mac no idea. I can't guess. Begin with my example and add piece by piece of what you want to have as well. You will find the problem in about 5 minutes then... – LaRiFaRi Aug 05 '15 at 14:57
  • I managed to install the required package. How can I adjust the overall size in zikt-cd environment? The result looks too small :) – Mac Aug 07 '15 at 07:46
  • @Mac You can change to row sep=3cm, column sep=3cm or any other value. Please see the manual for predefined sizes like the tiny I was using here. If you really want to scale the whole thing including the letters, you might want to have a look here: http://tex.stackexchange.com/q/138984 – LaRiFaRi Aug 07 '15 at 07:51
  • Thanks! This is the perfect link. I couldn't find information in the tikcd-manual. I just place tikzcd in a bigger a tikzpicture environment :) – Mac Aug 07 '15 at 07:59