10

I want to typeset a picture similar to the following one, using tikz.

enter image description here

For that purpose I have written the following latex document

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{chains}

\begin{document}

\begin{tikzpicture}[
  level distance=10mm,
  op/.style={circle,draw,fill=red!20},
  leaf/.style={circle,fill=blue!20,font=\ttfamily},
  ]
  \node (in) [fill=green!10] {\texttt{1 + 2*3}};
  \node (out) [fill=green!10,right=30mm of in] {
    \tikz{
      \node [op] {$+$}
        child { node [leaf] {1} }
        child { node [op] {$\times$}
          child { node [leaf] {2} }
          child { node [leaf] {3} }};
    }
  };
  \draw[->] (in) -- node [above] {\emph{parsing}} (out);
\end{tikzpicture}

\end{document}

which gives me the output

Notice that in the tree, the left and right edges from each node are not symmetric: the left one is shorter than the right one. When the same tree is drawn outside of the out node, they are symmetric.

Why am I getting this effect, and how to fix this issue?

5

Say it backwards:

\begin{tikzpicture}[
  level distance=10mm,
  op/.style={circle,draw,fill=red!20},
  leaf/.style={circle,fill=blue!20,font=\ttfamily},
  ]
  \node (out) [fill=green!10] {
    \tikz{
      \node [op] {$+$}
        child { node [leaf] {1} }
        child { node [op] {$\times$}
          child { node [leaf] {2} }
          child { node [leaf] {3} }};
    }
  };
  \node (in) [fill=green!10,left=30mm of out] {\texttt{1 + 2*3}};
  \draw[->] (in) -- node [above] {\emph{parsing}} (out);
\end{tikzpicture}

I really don't know why it works.

  • 1
    Yep, seems that the right=of something interferes with the layout of the tree nodes. I'd also really like to know why? – Count Zero Nov 10 '11 at 18:42
  • In fact the right=of something option enabled by the chains library seems to be the problem. Using node distance=50mm,right of=in instead of right=30mm of in in the out node gives a correct layout for the tree. – Romildo Nov 10 '11 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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