9

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.

enter image description here

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.

7

Late options don't cover every property. The append after command is a clever shortcut mechanism for \pgfpositionnodelater<...> commands (here in particular \pgfpositionnodelaterpath). Hence the fill has been already stored but not executed. However the shape etc. has been decided otherwise you wouldn't be able to use that trick for placements. After the placement and other details then \pgfpositionnodenow is invoked and it overprints whatever there was before it.

You can argue maybe the drawing/filling/clipping might have been separated but that would be a feature request for T.Tantau.

  • Then I understand that I can use label for text but there's no solution for non node elements. – Ignasi Mar 4 '15 at 10:26
  • @Ignasi path picture is easier for more sophisticated element placement. Otherwise a simple label indeed would do. – percusse Mar 4 '15 at 11:08
  • I didn't know path picture. I'll think about using it. Thank you. – Ignasi Mar 4 '15 at 11:19
1

I'm really indebted to @Zarko for drawing this question to my attention in this post. My proposal is to draw things on the foreground layer.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{backgrounds}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\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{
\begin{pgfonlayer}{foreground} 
\node[anchor=north] at (\tikzlastnode.north) {A};
\end{pgfonlayer}}}] {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}

enter image description here

  • May be you are interested in percusse's suggested path picture option. Here you'll find an example of use. – Ignasi Apr 11 '18 at 18:15
  • @Ignasi Thanks. I was drawn here when I was playing with split nodes here. In this case, unfortunately I could not make the path picture work. Perhaps this was because the node parts were not "done", or, which is perhaps more likely, because I screwed it. Zarko then told me about this post. – user121799 Apr 11 '18 at 18:28
  • I've added an answer to your mentioned question using path picture is not so nice like yours, but possible. – Ignasi Apr 11 '18 at 19:07

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.