箭

我有以下 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}

我正在努力添加独一无二箭头(不一定是红色)如下图所示 概率树

附加问题是如何使数字 14、21、25(圆圈)和 25(正方形)逐步出现,以便与 beamer 一起使用(即最初有空圆圈和空正方形,然后出现数字)。当然,我可以复制并粘贴树并从第一张幻灯片中删除数字,然后将它们添加到第二张幻灯片中,但是是否有\pause可以与 tikz 一起使用的项目来自动执行此操作?

答案1

对于箭头,我们可以设置every child/.style。如果你只想要一个箭头,例如使用,child[-triangle 90]但是因为子级的子级从其父级继承了这种风格,所以我们要么必须child[-]为每个子级的子级编写,要么我们在外部范围内-应用:every child/.style

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

beamer阶梯式节点

您可以使用\uncover<slides>{<number>}内部节点,但请注意,此命令必须被执行两次,因此要使平方祖父出现在幻灯片 2 至 4 和 6 上,请输入:

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

我们\uncover确保节点的内容只是被隐藏(具有相同的维度)而不是被遗漏(\only会做什么)。

没有要求,但还是做了

我最初以为问题的原作者想让孩子以分步法身的方式出现。起初,这似乎不可能做到,直到我发现“beamer 和 TikZ:逐渐揭开树木的面纱“…”

以下解决方案大量借鉴自马克·威尔布罗的“希望”最终解决方案

用户界面重要部分是 之后的序言的最后一部分\makeatother
在内部\tikzset我们定义所有我们想要隐藏的样式。

所以,我们使用的每个元素都会得到draw=none,节点的内容将被放入一个盒子中,然后盒子的内容将被\phantom编辑,即隐藏。

(在此示例中tikzpicture也是scaled,以便它适合一张标准beamer幻灯片。)

代码

%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}

输出此输出是使用选项进行编译的ultra thick

在此处输入图片描述

相关内容