Next code is just a little example to describe the problem. I would like to add some elements (lines or text) inside a node
and I would like to do it through node's .style
.
I've tried with append after command
(with and without postaction
) which works until a fill
option is used in outer node. Then, all added elements are covered with filling color and nothing except node contents survives.
Previous figure shows the problem. I would like to obtain the right result
\node[draw, minimum size=3cm, fill=green] (test) {test};
\node[anchor=north] at (test.north) {A};
but using the code used in left node
\node[draw, minimum size=3cm, append after command={%
\pgfextra \node[anchor=north] at (\tikzlastnode.north) {A};\endpgfextra}] {test};
which produces the central figure when filled
\node[draw, minimum size=3cm, fill=green, append after command={%
\pgfextra \node[anchor=north] at (\tikzlastnode.north) {A};\endpgfextra}] {test};
I know I could do the same with pic
but I would like to understand what happens here. I tried using postaction={append after ...
, behind path
and in front of path
options but always with similar results.
Could you explain it?
\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
%Left figure: Works without `fill`.
\begin{scope}[xshift=-4cm]
\node[draw, minimum size=3cm, append after command={\pgfextra \node[anchor=north] at (\tikzlastnode.north) {A};\endpgfextra}] {test};
\end{scope}
%Center figure. Fill covers everything inside
\node[draw, minimum size=3cm, fill=green, append after command={\pgfextra \node[anchor=north] at (\tikzlastnode.north) {A};\endpgfextra}] {test};
%Right figure: two commands
\begin{scope}[xshift=4cm]
\node[draw, minimum size=3cm, fill=green] (test) {test};
\node[anchor=north] at (test.north) {A};
\end{scope}
\end{tikzpicture}
\end{document}
Update (2018)
The main reason of this question was that I wanted to add labels inside node's margins and I didn't know how to do it. I tested all mentioned options I didn't succeed.
Although percusse's answer solved my question, it didn't solve my real problem. So I think it's worth to show the solution here.
Now I know that a label can be drawn inside a node just changing its default anchor. Therefore, the right square can be obtained with a simple command like:
\node[draw, minimum size=3cm, fill=green, label={[anchor=north]A}] (test) {test};
There is no need for extra paths, or append after command
and path picture
options.