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.

After looking around at some other answers on this site, I'm trying to typeset a tree using the tikz-qtree package. It works great, but by design it does not let you use macros in the tree nodes, unless you use the two specific macros \node and \edge to define TikZ nodes and edges directly.

What I'd like to have is something along the lines of

\tikzset{every tree node/.style={
   execute at begin node=\code\bgroup,
   execute at end node=\egroup}}

where at the moment, \code locally sets up some symbols and then sets the text in TT font:

\newcommand{\code}[1]{{\def\_{\wild}%
                       \def\"{\quot}% dummy " for hightlighting...
                       \def\~{\symbol{126}}%
                       \def\^{\symbol{94}}%
                       \texttt{#1}}}

This compiles, but doesn't typeset the node text in TT font. Bizarrely, if I just change \impl to \texttt directly, it does typeset in TT font. I know that TikZ plays around with null fonts and such, to ensure things get typeset only when they're supposed to, so I'm not terribly surprised that this font-changing of a node is tricky.

I saw in another question that it's suggested to use \begingroup/\endgroup instead of \bgroup/\egroup. I tried that, but LaTeX complains about "Missing } inserted; Extra } or forgotten \endgroup"

A minimum (non-)working example is below:

\usepackage{tikz-qtree}
\usepackage{tikz}
\usetikzlibrary[trees]
\newcommand{\code}[1]{{\def\_{\wild}%
                       \def\"{\quot}% dummy " for hightlighting...
                       \def\~{\symbol{126}}%
                       \def\^{\symbol{94}}%
                       \texttt{#1}}}
\begin{tikzpicture}[grow=down]
  \tikzset{every tree node/.style={
      execute at begin node=\strut\code\bgroup,
      execute at end node=\egroup}};
  \Tree [.html head body ]
\end{tikzpicture}

I'm not sure whether my use of \bgroup/\egroup is wrong, or whether my definition of \code is faulty...

share|improve this question

1 Answer 1

up vote 3 down vote accepted

I can't give you a direct answer to your problem, but I can give you an alternative, more direct solution:

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

\begin{document} 
\newcommand{\code}{\def\_{\wild}%
                   \def\"{\quot}% dummy " for hightlighting...
                   \def\~{\symbol{126}}%
                   \def\^{\symbol{94}}%
                   \ttfamily}

\begin{tikzpicture}[grow=down]
    \tikzset{every tree node/.style={font=\code}}
\Tree [.html head body ]
\end{tikzpicture}

\end{document}

Also, for whatever reason, when I remove one of the group in your definition of \code, your example works as expected. Maybe that extra group confuses TikZ?

share|improve this answer
    
Not sure what the expected etiquette is for old questions; since this alternative solution worked for my particular problem, I'm accepting it as the answer, but it doesn't quite give the direct answer to the problem... Either way, @Caramdir, thanks for this alternate approach :) –  Ben Lerner Jun 15 '11 at 22:57

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.