# newcommand containing tikz node

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,

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}

• My tree consists of \A's only actually. It seems that \expandafter\Tree \expandafter[\expandafter.\A{1} \expandafter\A{2} c ] doesn't work. Jun 9, 2011 at 15:25
• @Rasmus: I doubt that finding and maintaining the correct \expandafter sequence is easier that writing the \node directly. Jun 10, 2011 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. Jun 10, 2011 at 14:48
• @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. Jun 13, 2011 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. Dec 9, 2013 at 10:23
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.722905158996582, "perplexity": 3999.6136999860414}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-27/segments/1656103617931.31/warc/CC-MAIN-20220628203615-20220628233615-00621.warc.gz"}