tags:

views:

210

answers:

2

I am trying to build my trees using macros but I don't get the result I want. Here is a minimal example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\newcommand{\LeafNode}[1]{%
  child {node  {#1}}
}

\newcommand{\InnerNode}[3]{%
  child {node {#3}
           #1
           #2
        }
}

\begin{document}

\begin{tikzpicture}
 \node (A) {A}
    \LeafNode{B}
    \LeafNode{C}
 ;
\end{tikzpicture}%
\hspace{2cm}%
\begin{tikzpicture}
 \node (A) {A}
    \InnerNode{\LeafNode{D}}{\LeafNode{E}}{B}
    \LeafNode{C}
 ;
\end{tikzpicture}

\end{document}

I expected this to produce two trees:

    A             A
   / \           / \
  B   C         B   C
               / \
              D   E

but I am getting:

           A
           |
   A       B
   |       |
   B       D
   |       |
   C       C

Am I missing something or there is no way to do it?

BTW, if I omit the label on my root node, I get a PGF error:

! Package pgf Error: No shape named  is known.

-- Tsf

A: 

This is not a direct answer to you question, but you could check out the tikz-qtree package. It provides a simpler syntax for creating trees.

Kjell Magne Fauske
That's true but I have already lots o material using pure tikz and don't want to mix the two styles. Thanks!-- Tsf
Tsf
+1  A: 

It seems that LaTeX implicitly groups the output of a \newcommand. So, the result of

\begin{tikzpicture}
 \node (A) {A}
    \LeafNode{B}
    \LeafNode{C}
 ;
\end{tikzpicture}

is the same as this:

 \begin{tikzpicture}
 \node (A) {A}
   {child {node {B}}}
   {child {node {C}}}
 ;
\end{tikzpicture}

TikZ scans for explicit "child" keywords and does not find it when it is hidden in a command or block.

I don't know any way around this, but I don't see that your macros make the syntax any easier.

Meinersbur