# Execute code after node has been created

I would like to define a TikZ key that automatically executes some code after a node has been created.

E.g., I would like to define stuff such that

\node[stuff] (Test) {Test};


will execute

\coordinate (eastSide) at (Test.east);


afterwards.

Note that execute at end node (or its internal equivalent \tikz@atend@node) does not help here, because when the code given to execute at end node is executed, the node does not yet exist. (We get the error Package pgf Error: No shape named eastSide is known.)

A minimal working example:

\documentclass{article}
\usepackage{tikz,etoolbox}

\begin{document}
\begin{tikzpicture}

\makeatletter

\def\afterNodeCode{%
\coordinate (eastSide) at (\previousNodeName.east);
}

\tikzset{%
stuff/.code=
\appto\tikz@atend@node{%
\xdef\previousNodeName{\tikz@fig@name}%
% \afterNodeCode  % does not work, too early to refer to the node
}%
}

\node[draw,stuff] (Test) {Test};
\afterNodeCode % I would like to remove this line

\node at (eastSide) {X}; % Just for testing

\end{tikzpicture}
\end{document}


In this example, I would like to remove the call to \afterNodeCode after the \node command, and I would like that the stuff key automatically executes the \afterNodeCode.

EDIT: append after command is not what I am after, either. append after command only evaluates paths while I need to execute arbitrary TiKZ/TeX code in \afterNodeCode.

This can be done via append after command key

\begin{tikzpicture}[stuff/.style={
append after command={coordinate (#1) at (\tikzlastnode.east)}
}
]
\node[stuff=eastSide] (Test) {Test};
\node at (eastSide) {X};
\end{tikzpicture}


But I think you are reinventing the wheel since this is simply a label=0:X for the Test node.

• append after command cannot be used to execute arbitrary code. It only executes paths. For example, append after command=\afterNodeCode will not work in my minimal example, because \afterNodeCode contains a command (\coordinate). In this simple setting, I can of course rewrite \afterNodeCode, but in my full scenario, this is not possible. (\afterNodeCode contains, for example, calls to \pgfpointanchor and tests based on the result of that call). – dominique-u Aug 24 '16 at 13:04
• @DominiqueUnruh Yes I know. But if you don't give your full test case I can't help further. You can always bundle your code in \pgfextra for arbitrary code. However, I still feel like you are reinventing the wheel. – percusse Aug 24 '16 at 13:10
• It's a bit tricky to make a test case that is still simple enough, because I am actually doing quite a lot of stuff in \afterNodeCode. And I don't know what exactly I can drop without risking that there is a simple solution for the special case (like label=0:X) that does not apply to the whole. So for what it's worth, here is a full test case: gist.github.com/dominique-unruh/… The call that I want to get rid of is in line 211. – dominique-u Aug 24 '16 at 13:35
• Using append after command=\pgfextra{\afterNodeCode} works! If you update to include that, I can accept it. – dominique-u Aug 25 '16 at 10:42
• @DominiqueUnruh Good that you figured your way out but there are some limitations to such use if you plan to use colors etc. but they might be edge cases. By the way, if you implement the typical gates like CNOT and Toffoli stuff I will be the first user :P – percusse Aug 25 '16 at 12:14