# How can I pass the body of a TikZ node to a font-changing macro?

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}};
\end{tikzpicture}


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

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

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?