Tikz,树中的节点变得不适合。

Tikz,树中的节点变得不适合。

我想画 4 棵树,并且希望每棵树上都有一些节点,就像第一张图那样。但第二张图里却出现了这样的节点。代码有什么问题?提前致谢!

在此处输入图片描述 在此处输入图片描述

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,backgrounds,fit,shapes,decorations.pathreplacing}


\begin{document}
\begin{tikzpicture}
[level distance=8.5mm,
level 1/.style={sibling distance=25mm},
level 2/.style={sibling distance=17mm},
level 3/.style={sibling distance=12mm},
level 4/.style={sibling distance=7mm},every fit/.style=    {rectangle,draw,inner sep=3.5pt}]

\node {+}
child {node {*}
    child {node{2}}
    child {node{$\pi$}}}
child {node {---}
    child {node {+}
        child {node {$x$}}
        child {node {3}}}
    child {node(/){/}
        child {node (y){y}}
        child {node (+){+}
            child {node (5){5}}
            child {node (1){1}}
}}};

\node [draw=green,fit=(/) (y) (+) (5) (1) ] {};
\end{tikzpicture}
\hskip 10pt 
\begin{tikzpicture}
[level distance=11mm,
level 1/.style={sibling distance=25mm},
level 2/.style={sibling distance=17mm},
level 3/.style={sibling distance=12mm}, fit/.style={rectangle,draw=red,inner sep=3.5pt}]

\node (1){*}
child {node(*){*}
    child {node(a){$a$}}
    child {node(3){3}}}
child {node {+}
    child {node{3}}
    child {node {+}
            child {node {y}}
            child {node {12}}}
};
\node[fit={(a) (3) (*) (1)}]{};
\end{tikzpicture}

\begin{tikzpicture}
[level distance=11mm,
level 1/.style={sibling distance=25mm},
level 2/.style={sibling distance=17mm},
level 3/.style={sibling distance=12mm}, fit/.style={rectangle,draw,inner sep=3.5pt}]
\node {+}
child {node {*}
    child {node{2}}
    child {node{$\pi$}}}
child {node {---}
    child {node {+}
        child {node {$x$}}
        child {node {3}}}
    child {node(*){*}
        child {node (a){$a$}}
        child {node (3){3}}
}};

\node [draw=red, fit={(*) ($a$) (3)}] {};
\end{tikzpicture}
\hskip 10pt 
\begin{tikzpicture}
[level distance=11mm,
level 1/.style={sibling distance=25mm},
level 2/.style={sibling distance=17mm},
level 3/.style={sibling distance=12mm}, fit/.style={rectangle,draw,inner sep=3.5pt}]

\node {*}
child {node(/){/}
            child {node (y){y}}
            child {node (+){+}
                child {node (5){5}}
                child {node (1){1}}
}}
child {node {+}
    child {node{3}}
    child {node {+}
            child {node {y}}
            child {node {12}}}
};

\node [fit=(/) (y) (+) (5) (1),draw=green ]{};
\end{tikzpicture}
    \end{document}

答案1

仅供参考,这里有一个使用强大forest我强烈建议用于树木的包;请注意,除了代码中的经济性之外,拟合还以非常简单的方式完成:

\documentclass{article}
\usepackage{forest}

\definecolor{mygreen}{RGB}{73,137,132}
\definecolor{myblue}{RGB}{150,177,192}

\forestset{
mytree/.style={
  baseline,
  for tree={
    parent anchor=south,
    s sep=15pt,
    content format={\ensuremath{\forestoption{content}}},
    }
  }  
}

\begin{document}

\noindent
\begin{minipage}{.5\textwidth}
\centering
\begin{forest}
mytree,
[+
  [*
    [2]
    [\pi]
  ]
  [-
    [+
      [x]
      [3]
    ]
    [/,tikz={\node[inner sep=2pt,draw=mygreen,line width=1pt,fit=()(!1)(!ll)] {};}
      [y]
      [+
        [5]
        [1]
      ]
    ]
  ]
]
\end{forest}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\centering
\begin{forest}
mytree,
[*
  [*,tikz={\node[inner sep=2pt,draw=myblue,line width=1pt,fit=()(!1)(!l)] {};}
    [a]
    [3]
  ]
  [+
    [3]
    [+
      [y]
      [12]
    ]
  ]
]
\end{forest}
\end{minipage}\par

\noindent
\begin{minipage}{.5\textwidth}
\centering
\begin{forest}
mytree,
[+
  [*
    [2]
    [\pi]
  ]
  [-
    [+
      [x]
      [3]
    ]
    [*,tikz={\node[inner sep=2pt,draw=myblue,line width=1pt,fit=()(!1)(!l)] {};}
      [a]
      [3]
    ]
  ]
]
\end{forest}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\centering
\begin{forest}
mytree,
[*
  [/,tikz={\node[inner sep=2pt,draw=mygreen,line width=1pt,fit=()(!1)(!ll)] {};}
    [y]
    [+
      [5]
      [1]
    ]
  ]
  [+
    [3]
    [+
      [y]
      [12]
    ]
  ]
]
\end{forest}
\end{minipage}

\end{document}

在此处输入图片描述

答案2

您想使用every fitnotfit键。否则您将重新定义 的含义fit,而这不是您想要的。

另外,当引用节点时(a)不要写($a$),而只需写(a)。我还(1)从第二个fit命令中删除了。

这是您的更正代码:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,backgrounds,fit,shapes,decorations.pathreplacing}


\begin{document}
\begin{tikzpicture}
[level distance=8.5mm,
level 1/.style={sibling distance=25mm},
level 2/.style={sibling distance=17mm},
level 3/.style={sibling distance=12mm},
level 4/.style={sibling distance=7mm},every fit/.style=    {rectangle,draw,inner sep=3.5pt}]

\node {+}
child {node {*}
    child {node{2}}
    child {node{$\pi$}}}
child {node {---}
    child {node {+}
        child {node {$x$}}
        child {node {3}}}
    child {node(/){/}
        child {node (y){y}}
        child {node (+){+}
            child {node (5){5}}
            child {node (1){1}}
}}};

\node [draw=green,fit=(/) (y) (+) (5) (1) ] {};
\end{tikzpicture}
\hskip 10pt 
\begin{tikzpicture}
[level distance=11mm,
level 1/.style={sibling distance=25mm},
level 2/.style={sibling distance=17mm},
level 3/.style={sibling distance=12mm}, every fit/.style={rectangle,draw=red,inner sep=3.5pt}]

\node (1){*}
child {node(*){*}
    child {node(a){$a$}}
    child {node(3){3}}}
child {node {+}
    child {node{3}}
    child {node {+}
            child {node {y}}
            child {node {12}}}
};
\node[fit={(a) (3) (*) }]{};
\end{tikzpicture}

\begin{tikzpicture}
[level distance=11mm,
level 1/.style={sibling distance=25mm},
level 2/.style={sibling distance=17mm},
level 3/.style={sibling distance=12mm},every fit/.style={rectangle,draw,inner sep=3.5pt}]
\node {+}
child {node {*}
    child {node{2}}
    child {node{$\pi$}}}
child {node {---}
    child {node {+}
        child {node {$x$}}
        child {node {3}}}
    child {node(*){*}
        child {node (a){$a$}}
        child {node (3){3}}
}};

\node [draw=red, fit={(*) (a) (3)}] {};
\end{tikzpicture}
\hskip 10pt 
\begin{tikzpicture}
[level distance=11mm,
level 1/.style={sibling distance=25mm},
level 2/.style={sibling distance=17mm},
level 3/.style={sibling distance=12mm}, every fit/.style={rectangle,draw,inner sep=3.5pt}]

\node {*}
child {node(/){/}
            child {node (y){y}}
            child {node (+){+}
                child {node (5){5}}
                child {node (1){1}}
}}
child {node {+}
    child {node{3}}
    child {node {+}
            child {node {y}}
            child {node {12}}}
};

\node [fit=(/) (y) (+) (5) (1),draw=green ]{};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容