5

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\egroup works by pure chance; \ensuremath\bgroup...\egroup will definitely not work. – egreg Oct 26 '14 at 19:03
  • Yes, egreg, I noticed this. Could you enlighten me why this is so? – Geert F Oct 26 '14 at 22:25
  • 1
    The braces around an argument should be explicit { and }. You can't say \mbox\bgroup X\egroup, for instance, because you're passing \bgroup as the argument to \mbox. The fact that such a construct seems to work doesn't mean it really does; the same is for \mathbb or other math family changing commands. For other macros you'll get unscrutable errors. – egreg Oct 26 '14 at 22:30
5

The latest version of PGF has a node contents=<node text> key which can be used in place of the {<node text>} in a node. It could be used like this:

\documentclass[tikz, border=5]{standalone}
\tikzset{node text/.style={node contents=\transformtext{#1}}}
\def\transformtext#1{\ttfamily(#1)}
\begin{document}
\begin{tikzpicture}
\foreach \l [count=\y] in {a,...,e}
  \node [draw] at (\y,\y) [node text=\l:\y];
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |

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.