Go to tikz.code.tex
line 3713, there is the definition of \tikz@do@fig
. This is the essential part of typesetting nodes. In the definition one can see \tikz@atbegin@node
at line 3736 and \tikz@halign@check
at 3755.
As one might guess, \tikz@atbegin@node
is the macro the stores execute at begin node
-codes, and \tikz@halign@check
checks if you says align=something
and build up a \halign
. Therefore by default your prefix is not involved in \halign
-construction and hence is put aside the node content. Try
\node[align=left,execute at begin node=\myfunction]{MyTest\\aa\\bb\\cc};
to see this clearer.
To solve this, a stupid method is to rearrange the code so that \tikz@halign@check
happens before \tikz@atbegin@node
. A stupid instance is

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\newcommand{\myfunction}[1]{prefix \\ #1}
\makeatletter
\def\tikz@do@fig{%
% Ok, reset all node part boxes
\pgfutil@for\tikz@temp:=\tikz@nodepart@list\do{%
\expandafter\setbox\csname pgfnodepart\tikz@temp box\endcsname=\box\pgfutil@voidb@x%
}%
\setbox\pgfnodeparttextbox=\hbox%
\bgroup%
\tikzset{every text node part/.try}%
\ifx\tikz@textopacity\pgfutil@empty%
\else%
\pgfsetfillopacity{\tikz@textopacity}%
\pgfsetstrokeopacity{\tikz@textopacity}%
\fi%
\pgfinterruptpicture%
\ifx\tikz@text@width\pgfutil@empty%
\tikz@textfont%
\else%
\begingroup%
\pgfmathsetlength{\pgf@x}{\tikz@text@width}%
\pgfutil@minipage[t]{\pgf@x}\leavevmode\hbox{}%
\tikz@textfont%
\tikz@text@action%
\fi%
\bgroup%
\aftergroup\unskip%
\ifx\tikz@textcolor\pgfutil@empty%
\else%
\pgfutil@colorlet{.}{\tikz@textcolor}%
\fi%
\pgfsetcolor{.}%
\setbox\tikz@figbox=\box\pgfutil@voidb@x%
\setbox\tikz@figbox@bg=\box\pgfutil@voidb@x%
\tikz@uninstallcommands%
\iftikz@handle@active@code%
\tikz@orig@shorthands%
\let\tikz@orig@shorthands\pgfutil@empty%
\fi%
\ifnum\the\catcode`\;=\active\relax\expandafter\let\tikz@activesemicolon=\tikz@origsemi\fi%
\ifnum\the\catcode`\:=\active\relax\expandafter\let\tikz@activecolon=\tikz@origcolon\fi%
\ifnum\the\catcode`\|=\active\relax\expandafter\let\tikz@activebar=\tikz@origbar\fi%
\aftergroup\tikz@fig@collectresetcolor%
\tikz@halign@check%
\tikz@atbegin@node%
\ignorespaces%
}
\tikz\node[align=left,execute at begin node=\myfunction]{MyTest\\aa\\bb\\cc};
\end{document}
hbox
node halign header
part). I can’t find a workaround however in the manual. E.g., using a tabular inside\myfunction
definition yields “Extra }, or forgotten \endgroup.”.node contents
?node contents
is certainly a viable option in absence of a solution foralign
.minipage
is still there, try\node[align=left, execute at begin node=\myfunction] {MyTest\\second line};
.