Tikz node style that will programmatically modify the node content

Slightly different from your expected input, but here's one approach using the node contents key mentioned by percusse.

I use the interface name as an argument to the style, and piece together the full node contents inside the definition of the style.

\documentclass[tikz]{standalone}
\usepackage[T1]{fontenc} % just for the guillemets
\tikzset{/uml/interface/.style={
  % ... more style defs as needed ...
  rectangle, draw, align=left,
  node contents={<<interface>> \\ \textbf{#1}},
}}

\begin{document}
\tikz{\node[/uml/interface=MyInterface];}
\end{document}

enter image description here


You can apply a macro to the node text by injecting it with execute at begin node. This works, because in the TikZ code this key is immediately followed by \bgroup.

/uml/interface/.style={
  rectangle, draw,
  execute at begin node=\umlinterface
}

To be safe, I added another level of grouping.

\documentclass{article}
\usepackage[T1]{fontenc} % just for the guillemets
\usepackage{tikz}
\begin{document}
\newcommand{\umlinterface}[1]{<<interface>> \\ \textbf{#1}}
\tikzset{
  /uml/interface/.style={
    rectangle, draw,
    execute at begin node=\umlinterface\bgroup,
    execute at end node=\egroup
  }
}
\begin{tikzpicture}
  \path node[/uml/interface] (MyInterface) {MyInterface};
\end{tikzpicture}
\end{document}

enter image description here

Tags:

Tikz Pgf