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'm trying to define a macro for writing empty nodes in a binary tree using tikz-qtree. Referring to the code below, I want the string \missing in my code to be replaced by \edge[draw=none]; {} as-is (just like if I simply did a find and replace in my code).

Is this possible and how can it be done? I don't want to paste \edge[draw=none]; {} every time there is an empty node in my binary tree.

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


\newcommand{\missing}{ \edge[draw=none]; {} }
%intended usage inside tikzpicture enviornment: 
%\Tree  [.3  1
%               \missing ]
%To producce the same result as code below but
%it gave me an error:
%! Undefined control sequence.
%\missing -> \edge 
%                  [draw=none]; {} 

\begin{document}

\begin{tikzpicture}
\Tree  [.3  1
               \edge[draw=none]; {} ]
\end{tikzpicture}

\end{document}
share|improve this question
    
Welcome to TeX.sx! A tip: You can use backticks ` to mark your inline code as I did in my edit. :) –  Paulo Cereda Jun 7 '12 at 21:35
    
Unfortunately \edge is not a real command, but a sentinel; if there is, then tikz-qtree is able to do its business. But you can't hide it in a macro. –  egreg Jun 7 '12 at 22:54

1 Answer 1

You need to encourage \missing to expand at the right time:

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

\let\old@@children\@@children
\def\@@children{\futurelet\my@next\my@@children}
\def\my@@children{%
\ifx\my@next\missing\else
\expandafter\@gobble
\fi
\expandafter\old@@children}

\makeatother

\newcommand{\missing}{ \edge[draw=none]; {} }
%intended usage inside tikzpicture enviornment: 
%\Tree  [.3  1
%               \missing ]
%To producce the same result as code below but
%it gave me an error:
%! Undefined control sequence.
%\missing -> \edge 
%                  [draw=none]; {} 

\begin{document}

\begin{tikzpicture}

\Tree  [.3  1 \missing ]
\end{tikzpicture}

\end{document}
share|improve this answer
    
You are my hero. –  Richard Dec 19 '12 at 10:27

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.