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 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?

share|improve this question
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Apr 22 '13 at 13:05

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
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

 
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.