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 want to draw 4 trees and I want to have some nodes in each of them fitted like in the first picture. But what is coming out is in the second picture. What is wrong with the code? Thanks in advance!!

enter image description here enter image description here

\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}
share|improve this question

2 Answers 2

up vote 5 down vote accepted

Just for the record, here's a solution using the powerful forest package which I strongly suggest for trees; notice that, besides the economy in the code, the fitting is done in a very simply way:

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

enter image description here

share|improve this answer

You want to use every fit not fit key. Otherwise you will redefine the meaning of fit and it is not what you want.

Also when referring to node (a) do not write ($a$) but just (a). I also removed (1) from the second fit command.

Here is your corrected code:

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

enter image description here

share|improve this answer
    
Thanks a lot!!! –  Dmitri Apr 24 at 11: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.