TikZ 树节点周围的矩形

TikZ 树节点周围的矩形

下面您可以看到我的表格旁边的一棵树的代码。

现在我想在节点周围插入一些矩形和椭圆。有人能告诉我怎么做吗?我试过了,但没有成功。谢谢!

\documentclass[doc,apacite]{apa}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%%%%Tikz%%%%%%
\usepackage{tikz}
\usetikzlibrary{trees,matrix,fit}
%%%%%%%%%%%%%%
%%%%Table%%%%%
\usepackage{ctable} % needed for \cmidrule{}
\usepackage{multirow} % needed for \multirow{}
%%%%%%%%%%%%%%
\usepackage{pgfkeys}

\begin{document}

    \begin{tikzpicture}[
        grow=right,
        sloped,
        bag/.style={text width=4em, text centered},
        end/.style={circle, minimum width=3pt,fill, inner sep=0pt,font=\tiny},
        sibling distance=\sibdist,
        level 1/.style={level distance=2.0cm},
        level 2/.style={level distance=2.5cm},
        every node/.style={font=\strut},
        ]
      \node(n){x}
          child {
              node{U2}        
                  child {
                      node[label=right:
                          ]{D2} {}
                              child {node[end, label=right:
                              {R1}] {}
                              edge from parent
                              node[above] {$1-R$}
                              node[below]  {}}
                              child {node[end, label=right:
                              {R2}] {}
                              edge from parent
                              node[above] {$R$}
                              node[below]  {}}
                              child[missing]
                      edge from parent
                      node[above] {$1-D$}
                      node[below]  {}
                  }
                  child {%empty child on level 2
                    child{
                        node[end, label=right:
                            {D1}] {}
                        edge from parent
                        node[above] {$D$}
                        node[below]  {}
                    }
                  }
                  child[missing]
                  edge from parent 
                  node[above] {}
                  node[below]  {$1-u$}
          }
          child {
              node{U1}        
                  child{% empty child on level 2
                    child{
                        node[end, label=right:
                            {$T$}](e) {}
                        edge from parent
                        node[above] {}
                        node[below]  {}
                    }
                  }
              edge from parent         
                  node[above] {$u$}
                  node[below]  {}
          };
      \matrix[
          matrix of nodes,
          anchor=m-4-1.west,
          xshift=1cm,
          nodes={inner xsep=\tabcolsep,minimum height=\sibdist}
        ](m)at(n-1-1-1){%
          c&b&a&c&bd&a\\
          a&c&b&a&a&c\\
          x&g&f&c&d&b\\%[1cm]
          ta&g&a&x&p&d\\
      };
      \foreach \i in {1,...,6}\node[draw,fit=(m-1-\i) (m-2-\i) (m-3-\i) (m-4-\i),inner sep=-.5\pgflinewidth](c\i){};
      \foreach \i in {1,...,3}\draw([yshift=-.5*\sibdist]m-\i-1.center-|c1.west)--([yshift=-.5*\sibdist]m-\i-1.center-|c6.east);
    \end{tikzpicture}

\end{document}

答案1

在此处输入图片描述

您可以使用fit库来构建适合所需节点组的形状;对于椭圆形,您将需要库shapes。由于这三个节点中的根节点称为n,因此您可以使用访问子节点n-<string>;例如,n-1-1对应于D2,并且n-2U1

示例代码:

\documentclass[doc,apacite]{apa}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{trees,matrix,fit,shapes}
\usepackage{ctable}
\usepackage{multirow}
\usepackage{pgfkeys}

% I added the following two lines since they were missing in the example code
\newlength\sibdist
\setlength\sibdist{1cm}

\begin{document}

    \begin{tikzpicture}[
        grow=right,
        sloped,
        bag/.style={text width=4em, text centered},
        end/.style={circle, minimum width=3pt,fill, inner sep=0pt,font=\tiny},
        sibling distance=\sibdist,
        level 1/.style={level distance=2.0cm},
        level 2/.style={level distance=2.5cm},
        every node/.style={font=\strut},
        ]
      \node(n){x}
          child {
              node{U2}        
                  child {
                      node[label=right:
                          ]{D2} {}
                              child {node[end, label=right:
                              {R1}] {}
                              edge from parent
                              node[below] {$1-R$}
                              node[below]  {}}
                              child {node[end, label=right:
                              {R2}] {}
                              edge from parent
                              node[above] {$R$}
                              node[below]  {}}
                              child[missing]
                      edge from parent
                      node[below] {$1-D$}
                      node[below]  {}
                  }
                  child {%empty child on level 2
                    child{
                        node[end, label=right:
                            {D1}] {}
                        edge from parent
                        node[above] {$D$}
                        node[below]  {}
                    }
                  }
                  child[missing]
                  edge from parent 
                  node[above] {}
                  node[below]  {$1-u$}
          }
          child {
              node{U1}        
                  child{% empty child on level 2
                    child{
                        node[end, label=right:
                            {$T$}](e) {}
                        edge from parent
                        node[above] {}
                        node[below]  {}
                    }
                  }
              edge from parent         
                  node[above] {$u$}
                  node[below]  {}
          };
      \matrix[
          matrix of nodes,
          anchor=m-4-1.west,
          xshift=1cm,
          nodes={inner xsep=\tabcolsep,minimum height=\sibdist}
        ](m)at(n-1-1-1){%
          c&b&a&c&bd&a\\
          a&c&b&a&a&c\\
          x&g&f&c&d&b\\%[1cm]
          ta&g&a&x&p&d\\
      };
      \foreach \i in {1,...,6}\node[draw,fit=(m-1-\i) (m-2-\i) (m-3-\i) (m-4-\i),inner sep=-.5\pgflinewidth](c\i){};
      \foreach \i in {1,...,3}\draw([yshift=-.5*\sibdist]m-\i-1.center-|c1.west)--([yshift=-.5*\sibdist]m-\i-1.center-|c6.east);

% A red rectangle enclosing U1,U2,T,D1:
\node[draw=red,fit={(n-2) (n-1-2-1)}] {};      
% A blue ellipse enclosing R2,R1:
\node[draw=blue,ellipse,fit={(n-1-1-1) (n-1-1-2)}] {};      

    \end{tikzpicture}

\end{document}

更新:

在评论之后,要求将一些树的主体设计为矩形或椭圆形;这可以通过定义一些样式,然后将这些样式作为所需节点的选项传递来实现;在下面的例子中,我使用了

        myrect/.style={draw=orange,fill=orange!30,text width=1cm,align=center},
        myellipse/.style={ellipse,draw=cyan,fill=cyan!30,inner sep=2pt,text width=1cm,align=center},

但这些设置可以根据需要进行更改。

\documentclass[doc,apacite]{apa}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{trees,matrix,shapes,fit}
\usepackage{ctable}
\usepackage{multirow}
\usepackage{pgfkeys}

% I added the following two lines since they were missing in the example code
\newlength\sibdist
\setlength\sibdist{1cm}

\begin{document}

    \begin{tikzpicture}[
        grow=right,
        sloped,
        bag/.style={text width=4em, text centered},
        end/.style={circle, minimum width=3pt,fill, inner sep=0pt,font=\tiny},
        sibling distance=\sibdist,
        level 1/.style={level distance=2.5cm},
        level 2/.style={level distance=2.5cm},
        every node/.style={font=\strut},
        myrect/.style={draw=orange,fill=orange!30,text width=1cm,align=center},
        myellipse/.style={ellipse,draw=cyan,fill=cyan!30,inner sep=2pt,text width=1cm,align=center},
        ]
      \node[myrect] (n){x}
          child {
              node[myellipse] {U2}        
                  child {
                      node[myrect,label=right:
                          ]{D2} {}
                              child {node[end, label=right:
                              {R1}] {}
                              edge from parent
                              node[below] {$1-R$}
                              node[below]  {}}
                              child {node[end, label=right:
                              {R2}] {}
                              edge from parent
                              node[above] {$R$}
                              node[below]  {}}
                              child[missing]
                      edge from parent
                      node[below] {$1-D$}
                      node[below]  {}
                  }
                  child {%empty child on level 2
                    child{
                        node[end, label=right:
                            {D1}] {}
                        edge from parent
                        node[above] {$D$}
                        node[below]  {}
                    }
                  }
                  child[missing]
                  edge from parent 
                  node[above] {}
                  node[below]  {$1-u$}
          }
          child {
              node[myellipse] {U1}        
                  child{% empty child on level 2
                    child{
                        node[end, label=right:
                            {$T$}](e) {}
                        edge from parent
                        node[above] {}
                        node[below]  {}
                    }
                  }
              edge from parent         
                  node[above] {$u$}
                  node[below]  {}
          };
      \matrix[
          matrix of nodes,
          anchor=m-4-1.west,
          xshift=1cm,
          nodes={inner xsep=\tabcolsep,minimum height=\sibdist}
        ](m)at(n-1-1-1){%
          c&b&a&c&bd&a\\
          a&c&b&a&a&c\\
          x&g&f&c&d&b\\%[1cm]
          ta&g&a&x&p&d\\
      };
      \foreach \i in {1,...,6}\node[draw,fit=(m-1-\i) (m-2-\i) (m-3-\i) (m-4-\i),inner sep=-.5\pgflinewidth](c\i){};
      \foreach \i in {1,...,3}\draw([yshift=-.5*\sibdist]m-\i-1.center-|c1.west)--([yshift=-.5*\sibdist]m-\i-1.center-|c6.east);

    \end{tikzpicture}

\end{document}

在此处输入图片描述

只是一个建议,强大的forest这个包可能会让你感兴趣,以便你以更简单的方式绘制树木。

相关内容