Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have the following MWE

%http://www.texample.net/tikz/examples/probability-tree/
\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees,arrows}
\begin{document}
\pagestyle{empty}


% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=1.2cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [circle,draw]
\tikzstyle{square} = [rectangle,draw]

% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels. 
\begin{tikzpicture}[grow=right, sloped]
\node[square] {25}
    child {
        node[bag] {25}        
            child {
                node[label=right:
                    {25}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
           child {
                node[label=right:
                    {30}] {}
                edge from parent
                node[above] {0.6}
                node[below]  {}
            }
            child {
                node[label=right:
                    {10}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
            edge from parent
            node[above] {Buy}
            node[below] {abroad}
    }
    child {
        node[bag] {21}        
            child {
                node[label=right:
                    {40}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
           child {
                node[label=right:
                    {20}] {}
                edge from parent
                node[above] {0.6}
                node[below]  {}
            }
            child {
                node[label=right:
                    {5}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
            edge from parent 
            node[above] {Buy}
            node[below]  {locally}
    }    
    child {
        node[bag] {14}        
            child {
                node[label=right:
                    {55}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
           child {
                node[label=right:
                    {10}] {}
                edge from parent
                node[above] {0.6}
                node[below]  {}
            }
            child {
                node[label=right:
                    {-15}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
        edge from parent         
            node[above] {Manufacture}
            node[below]  {themselves}
    };
\end{tikzpicture}

\end{document}

I am struggling to add one and only one arrow (not red necessarily) as shown below probability tree

The bonus question is how to make numbers 14,21,25 (circles) and 25 (square) appear in a step-wise fashion for use with beamer (i.e. initially having empty circles and empty square and then numbers appearing). Of course I could copy and paste the tree and remove the numbers from the first slide and add them on the second, but is there a \pause item possible to be utilised with tikz to automate this?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Arrow

For the arrows, we can set the every child/.style. If you only want one arrow, use for example child[-triangle 90] but because the child's child inherit this style from their parrent we either have to write child[-] for every child's child or we apply - to every child/.style in the outer scope:

\begin{tikzpicture}[
    grow=right,
    sloped,
    every child/.style=-
    ]

beamer step-wise nodes

You can use \uncover<slides>{<number>} inside nodes, but beware that this command must embraced twice, so to have the squared grandparent appear on slides 2 to 4 and 6 you type:

\node[square] {{\uncover<{2-4,6}>{25}}}

With \uncover we make sure that the node's content is just hidden (to have the same dimension) instead of left out (what \only would do).

Not asked, but done anyway

I initially thought the question's OP wanted to have the childs appear in a step-wise fashen. At first, this seemed impossible to do, until I found „beamer & TikZ: gradually unveil trees” …

The following solution is heavily borrowed from Mark Wilbrow's “hopefully” final solution.

The user-interface-important part is the last bit of the preamble after \makeatother.
In the inner \tikzset we define all the style, we want to hide.

So, every element we use gets draw=none, the nodes' contents will be put in a box and then the box's content will be \phantomed, i.e. hidden.

(The tikzpicture is also scaled in this example, so that it fits on one standard beamer slide.)

Code

%http://www.texample.net/tikz/examples/probability-tree/
\documentclass{beamer}

\usepackage[latin1]{inputenc}
\usepackage{tikz,calc}
\usetikzlibrary{trees,arrows}

% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=1.2cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [circle,draw]
\tikzstyle{square} = [rectangle,draw]

\makeatletter
\def\tikz@collect@child hild{% 
        \pgfutil@ifnextchar<{\tikz@collect@child@overlay}% 
                {\pgfutil@ifnextchar[{\tikz@collect@childA}{\tikz@collect@childA[]}}% 
} 
\def\tikz@collect@child@overlay<#1>{% 
        \pgfutil@ifnextchar[{\tikz@collect@child@@overlay{#1}}{\tikz@collect@childA[child 
overlay={#1}]}}%} 

\def\tikz@collect@child@@overlay#1[{\tikz@collect@childA[child overlay={#1},} 

\def\tikzprocessoverlay#1#2#3{% 
        \def\beamer@doifinframe{#2}% 
        \def\beamer@doifnotinframe{#3}% 
        \beamer@masterdecode{#1}% 
        \beamer@donow% 
} 

% Extra hackery to allow preactions on different layers. 
% 
\def\tikz@extra@preaction#1{% 
  {% 
    \pgfsys@beginscope% 
      \setbox\tikz@figbox=\box\voidb@x% 
      \begingroup\tikzset{#1}\expandafter\endgroup% 
      \expandafter\def\expandafter\tikz@preaction@layer\expandafter{\tikz@preaction@layer}% 
      \ifx\tikz@preaction@layer\pgfutil@empty% 
      \path[#1];% do extra path 
      \else% 
      \begin{pgfonlayer}{\tikz@preaction@layer}% 
      \path[#1];% 
      \end{pgfonlayer} 
      \fi% 
      \pgfsyssoftpath@setcurrentpath\tikz@actions@path% restore 
      \tikz@restorepathsize% 
    \pgfsys@endscope% 
  }% 
} 
\let\tikz@preaction@layer=\pgfutil@empty 

\tikzset{preaction layer/.store in=\tikz@preaction@layer} 

\makeatother

\tikzset{% 
        child overlay/.code={% 
                \tikzprocessoverlay{#1}{}% 
                {% 
                        \tikzset{% 
                                edge from parent/.style={draw=none}, 
                                every node/.style={ 
                                        draw=none, fill=none, 
                                        execute at begin node={\setbox0=\hbox\bgroup\hskip0pt\let\\=\relax}, 
                                        execute at end node=\egroup\phantom{\box0} 
                                },%
                                bag/.style={draw=none}%
                        }% 
                }% 
        } 
}

\begin{document}
\pagestyle{empty}

\begin{frame}
% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels.
\begin{tikzpicture}[scale=.8,
    grow=right,
    sloped,
    every child/.style=-
    ]
\node[square] {{\uncover<8->{25}}}
    child<6->[-triangle 60] {
        node[bag] {{\uncover<7->{25}}}        
            child {
                node[label=right:{25}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
            child {
                node[label=right:{30}] {}
                edge from parent
                node[above] {0.6}
                node[below]  {}
            }
            child {
                node[label=right:{10}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
            edge from parent
            node[above] {Buy}
            node[below] {abroad}
    }
    child<4-> {
        node[bag] {{\uncover<5->{21}}}
            child {
                node[label=right:{40}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
           child {
                node[label=right:{20}] {}
                edge from parent
                node[above] {0.6}
                node[below]  {}
            }
            child {
                node[label=right:{5}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
            edge from parent 
            node[above] {Buy}
            node[below]  {locally}
    }
    child<2-> {
        node[bag] {{\uncover<3->{14}}}
            child {
                node[label=right:{55}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
           child {
                node[label=right:{10}] {}
                edge from parent
                node[above] {0.6}
                node[below]  {}
            }
            child {
                node[label={right:$-15$}] {}
                edge from parent
                node[above] {0.2}
                node[below]  {}
            }
        edge from parent         
            node[above] {Manufacture}
            node[below] {themselves}
    };
\end{tikzpicture}
\end{frame}
\end{document}

Output This output was compiled with the option ultra thick.

enter image description here

share|improve this answer
    
Thank you for your input; though either I wasn't clear or I am missing something. I have edited my question to be more explicit: I need only one arrow at the specific place. Applying your code puts an arrow everywhere. Oh, and I don't understand the usefulness of the "every label" part of your code (it adds circles which I didn't need). Thanks –  ilakast Oct 25 '12 at 23:02
    
@ilakast Then I have misunderstood your first part of the sentence: “The bonus question is how to make numbers in circles”. Maybe you can clarify what has to be shown on which slide. (Draw up a sketch in Paint if all fails.) –  Qrrbrbirlbel Oct 25 '12 at 23:07
    
Sorry, you were absolutely right, the way I expressed it was confusing. I rephrased it. The arrow looks perfect and is exactly what I wanted! Will try the code after posting this. I feel bad for making you create things that later you scraped off because of my bad English. I appreciate your effort a lot. –  ilakast Oct 26 '12 at 8:06
    
Code for the single arrow did the trick! Thanks for the explanation too, I am tikz naive and it helps a lot grasping the idea behind the code. In case you or someone else has an idea for the second question, please don't remove the answer you have given already, because it is impressive and could be useful in another context. –  ilakast Oct 26 '12 at 8:22
    
@ilakast If you just want the node's text (the numbers) to appear later you can use node[bag] {{\uncover<3->{<number>}}} or \node[square] {{\uncover<2->{<number>}}} … for the grandparent. (Note, that a double pair of braces is used!) –  Qrrbrbirlbel Oct 26 '12 at 17:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.