I want to design a tikz style that will modify a node content according to some function of mine. As suggested in an answer to the original question, I can use the “execute at begin node” key for this. But it does not play well with “align” and linebreaks. Any idea why? (I suspect something related to the way \\ works, but I am not sure; replacing with \newline shows no improvement.)

% First picture: the align instruction works.
% The prefix word appears, as expected, on top of MyTest.
  \path node[align=left] {prefix \\ MyTest};

\newcommand{\myfunction}[1]{prefix \\ #1}
% Second picture: now align has no apparent effect any more.
% The prefix word and MyTest appear together on the same line.
  \path node[align=left, execute at begin node=\myfunction] {MyTest};
  • align installs a minipage you are overwriting it and making it an hbox
    – percusse
    Feb 1, 2016 at 13:42
  • @percusse Good point. I now see that this is probably explained in the pgf manual (although I don’t really understand it when it speaks about the 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.”. Feb 2, 2016 at 14:58
  • Why don't you simply use node contents ?
    – percusse
    Feb 2, 2016 at 15:55
  • For consistency reasons, I’d prefer keeping the usual syntax for nodes if possible, where the “contents” go in the brackets following the nodes. This way I can change the style and not change the rest of the syntax. Here, I consider the added prefix as an element of style, which I agree is only one of the possible points of view. But you are right, node contents is certainly a viable option in absence of a solution for align. Feb 2, 2016 at 16:52
  • @percusse minipage is still there, try \node[align=left, execute at begin node=\myfunction] {MyTest\\second line};.
    – Symbol 1
    Feb 2, 2016 at 18:22

1 Answer 1


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

\newcommand{\myfunction}[1]{prefix \\ #1}

    % Ok, reset all node part boxes
      \expandafter\setbox\csname pgfnodepart\tikz@temp box\endcsname=\box\pgfutil@voidb@x%
        \tikzset{every text node part/.try}%

\tikz\node[align=left,execute at begin node=\myfunction]{MyTest\\aa\\bb\\cc};


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.