I would like to make a node style that "filters" the node's label (text) through a command, i.e. replace the node text with a macro, with the original node text passed as an argument to that macro. Specifically, in this case, I want to add a node \node {C} and have it typeset/draw \node {$\mathbb{C}$}.
I read the following answer: How can I pass the body of a TikZ node to a font-changing macro? and thought to do it like there, inserting $\mathbb\bgroup into the execute at begin node and \egroup$ into the execute at end node. This works (see MWE below), but I thought it would be better to use \ensuremath; also I might want to use \bm{\mathbb{C}} or \pmb{\mathbb{C}} for an 'extra-bold blackboard bold' symbol.
As soon as I add \ensuremath\bgroup or the extra \bm\bgroup, however, I get errors. Also if I define a separate macro \mymacro and use that in the tikzstyle definition, I get errors (see MWE below). Also when defining this macro somewhat differently (inspired by Use node text as argument for a macro) do I get errors.
Is there any way to achieve this?
\documentclass{article}
\usepackage{tikz}
\usepackage{amsfonts} %for the mathbb command
\usepackage{amsmath}
\tikzset{
bb/.style={
execute at begin node=$\mathbb\bgroup,
execute at end node=\egroup$
}
}
\tikzset{
bb2/.style={
execute at begin node=\ensuremath\bgroup\mathbb\bgroup,
execute at end node=\egroup\egroup
}
}
\def\mymacro#1{\ensuremath{\mathbb{#1}}}
\tikzset{
my macro/.style={
execute at begin node=\mymacro\bgroup,
execute at end node=\egroup
}
}
\def\mysecondmacro{\ensuremath\bgroup\mathbb\bgroup}
\def\endmysecondmacro{\egroup\egroup}
\tikzset{
my secondmacro/.style={
execute at begin node={\mysecondmacro},
execute at end node={\endmysecondmacro}
}
}
\begin{document}
In text: \ensuremath{\mathbb{C}} %simply as text
bb style: \tikz \node[bb] {C}; %produces the blackboard-bold C
% Gives errors "Missing } inserted.", "\mathbb allowed only in math mode"
% and "Extra }, or forgotten \endgroup."
bb2 style: \tikz \node[bb2] {C}; %produces the blackboard-bold C
% Gives errors "Missing } inserted" and "Extra }, or forotten \endgroup"
my macro style: \tikz \node[my macro] {C};
% Gives same error as bb2 style
my second macro style: \tikz\node[my secondmacro] {C};
\end{document}

\mathbb\bgroup C\egroupworks by pure chance;\ensuremath\bgroup...\egroupwill definitely not work. – egreg Oct 26 '14 at 19:03{and}. You can't say\mbox\bgroup X\egroup, for instance, because you're passing\bgroupas the argument to\mbox. The fact that such a construct seems to work doesn't mean it really does; the same is for\mathbbor other math family changing commands. For other macros you'll get unscrutable errors. – egreg Oct 26 '14 at 22:30