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

\documentclass{minimal}
\usepackage{tikz,tikz-qtree}
\newcommand{\A}[1]{\node(a#1){Activity #1};}
\begin{document}
Works:\
\tikz\A{1};
\hfill
Works:
\Tree [.\node(a1){A1}; 1 2 ]
\hfill
Doesn't Work:
% \begin{tikzpicture}
%   \Tree [.\A{1} b c ]
% \end{tikzpicture}
\end{document}

Thanks,

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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

\expandafter<token1><token2>

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:

\documentclass{article}
\usepackage{tikz,tikz-qtree}

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

\begin{document}

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

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

\end{document}
share|improve this answer
    
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
    
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. –  macmadness86 Dec 9 '13 at 10:23
add comment

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.