1

I want to draw a tree where each edge has a certain weight visualized through both the line width as well as a label on the edge. Using TikZ, this is pretty easy:

\documentclass{standalone}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}[
        clabel/.style = { fill=white, circle, inner sep=1pt,font=\scriptsize},
    ]
    \Tree [.O
        \edge [line width = 2] node [clabel] {2}; L
        \edge [line width = 4] node [clabel] {4}; R
    ]
\end{tikzpicture}
\end{document}

Since the tree I want to draw is rather large, I want to factor out the edge code, i.e. I would like to have a macro like

\def\edgeweight#1{\edge [line width = #1] node [clabel] {#1}; }

Unfortunately, LaTeX complains about the \edge command when I call this macro which I assume is because it appears outside of the \Tree command. Is there a way to fix this?

1

Adding the following code to the preamble fixes the problem:

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

This solution is due to David Carlisle and is taken from here. Congrats to the guy who wrote the related questions detector, this took me straight to the right place!

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.