I am using tikz-qtree to draw a binary tree. As I need to have them labeled I wanted to ues the following macro which work in ordinary tikz:

\newcommand{\A}[1]{\node(a#1){Activity #1};}

However, this does not work; the error is that node is an undefined control sequence.

Of course this macro is used inside a tikzpicture, so basically I just want to dump \node(a1){Activity 1}; if \A{1} is called. I am pretty sure it has to do with my TeXnic. . . Maybe expansion?

Note, the tikz-qtree manual states that

\Tree specifically watches out for the token \node; do not use \path node or other equivalents.

Here is a minimal example:

\newcommand{\A}[1]{\node(a#1){Activity #1};}
\Tree [.\node(a1){A1}; 1 2 ]
Doesn't Work:
% \begin{tikzpicture}
%   \Tree [.\A{1} b c ]
% \end{tikzpicture}



As you suspected, the problem is related to expansion: \Tree is expanded first and looks for its parameters before expanding \A. The problem can be solved if \A is expanded first. The \expandafter primitive allows you to do this: the sequence


is replaced by

<token1><expansion of token2>

and this replacement is reexamined by the expansion processor.

Since you also have to take into account the tokens [ and . you'll need three \expandafter commands:


\newcommand{\A}[1]{\node(a#1){Activity #1};}


\expandafter\Tree \expandafter[\expandafter.\A{A} 1 2 ]

  \expandafter\Tree \expandafter[\expandafter.\A{1} b c ]

  • My tree consists of \A's only actually. It seems that \expandafter\Tree \expandafter[\expandafter.\A{1} \expandafter\A{2} c ] doesn't work. – Rasmus Jun 9 '11 at 15:25
  • @Rasmus: I doubt that finding and maintaining the correct \expandafter sequence is easier that writing the \node directly. – Ulrike Fischer Jun 10 '11 at 8:32
  • @Rasmus: I agree with Ulrike Fischer. Doing some tests, as the tree grows, the host of \expandafters will render the code almost impossible to read; it's even difficult to find the exact sequence that they should follow. – Gonzalo Medina Jun 10 '11 at 14:48
  • 1
    @Ulrike, @Gonzalo, yeah I agree. Given the current solution it is not practical. Had it been possible to write \Tree [.\A{1} \A{2} \A{3} ] it would have been much more clean. Oh well. Thanks, though. – Rasmus Jun 13 '11 at 9:47
  • 2
    I would really like to see a solution with \Tree [.\A{1} \A{2} \A{3} ] as the syntax for the actual tree. This would be extremely useful for linguistics. – Jonathan Komar Dec 9 '13 at 10:23

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.