# How to share selected options between different nodes in TikZ?

(I didn't find a better title for the question. Suggestions are welcomed.)

I have created some new shapes in TikZ and I would like to put one of them below another one when a certain key is passed to this last node. I'll show better what I mean with an example using two rectangles:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{patterns,calc}

\tikzstyle{ground}=[append after command={;\node[draw=red,rectangle,anchor=north] (B) at (\tikzlastnode.south){a}}]

\begin{document}
\begin{tikzpicture}
\node[draw,rectangle,ground,rotate=10] (A) at (0,0){a};% not good
\begin{scope}[shift={(1,0)}]
\node[draw,rectangle,rotate=10] (B) at (0,0){a};
\node[draw=red,rectangle,anchor=north,rotate=10] (C) at (B.south){a};% OK
\end{scope}
\end{tikzpicture}
\end{document}


As you can see, I've created a style that appends to the first node the creation of the second one referred to it correctly. Everything works fine, but, if I need to, for example, rotate both shapes together, I can't find a solution because only the first rectangle is rotated (left). The only way I achieved the desired result has been the one shown in the code, where I had to manually draw the second node and pass to it the rotate option.

So, there is a way to achieve the desired result (on the right) but using the syntax of the first example (on the left)? Notice that both the real shapes involved has been drawn by me so if they need to be customized it is ok.

-
I think, a rectangle split would ease the pain a little bit. – percusse Aug 23 '12 at 22:53
@percusse As I said, this is just an example. The real shapes are different and custom (they are not symmetric). If you need their real code, I'll post it – Spike Aug 24 '12 at 7:35

If you know what parameters will affect the additional node you can include them into the .style. Here you have an example based on your code. I don't know if it will serve for your real problem.

Within your example you want to rotate the additional node, so I've defined a ground style which includes this parameter with 0 as a default value.

    ground/.style={%
append after command={;%
\node[draw=red,rectangle,
anchor=north,rotate=#1] (B)
at (\tikzlastnode.south){a}}},
ground/.default=0,


Now it's possible to write \node[draw,rectangle,ground=10,rotate=10] (A) at (0,0){a}; but this way you must be sure that you write the same value in ground and rotate. A better solution would be to define a new style which includes both of them, this is what grounded does:

    grounded/.style={ground=#1,rotate=#1},
grounded/.default=0}


And the complete code is:

\documentclass[tikz,border=1mm]{standalone}

\tikzset{%
ground/.style={%
append after command={;%
\node[draw=red,rectangle,
anchor=north,rotate=#1] (B)
at (\tikzlastnode.south){a}}},
ground/.default=0,
grounded/.style={ground=#1,rotate=#1},
grounded/.default=0}

\begin{document}
\begin{tikzpicture}
\node[draw,rectangle,ground=10,rotate=10] (A) at (0,0){a};
\node[draw,rectangle,grounded] (A) at (0.5,0){a};
\node[draw,rectangle,grounded=25] (A) at (1,0){a};
\node[draw,rectangle,grounded=-25] (A) at (1.5,0){a};
\end{tikzpicture}
\end{document}


And, please, take a look at: Should \tikzset or \tikzstyle be used to define TikZ styles?

-
This is an easy and nice solution, thanks a lot. Options passed to the ground node are expected to be just a few, so this solves the problem without complications for the user. Thanks also for the hint about the difference between \tikzset and \tikzstyle, I learned a lot. – Spike Aug 27 '12 at 14:01

try this, with scope and transform shape

  \begin{scope}[shift={(2,0)},rotate=10,transform shape]
\node[draw,rectangle,ground,] (A) at (0,0){a};% not good
\end{scope}

-
Thank you very much. Didn't know about transform shape. Actually I tried to use scope without any result and you showed me why. This is a good solution, but the lines of code are now three (from one). The best way would be to completely avoid the necessity to write other code (if I should "rotate" 10 nodes, the velocity in writing and the readability of code would decrease a lot) – Spike Aug 23 '12 at 22:27
If you supply the options shift={(2,0)},rotate=10,transform shape to the \node[...] instead of the scope, they will apply to the "child" node as well, so there's no need for a scope. – Jake Aug 27 '12 at 10:12
@Jake Can you explain it better? If I write \node[draw, rectangle, shift={(3,0)}, rotate=10, transform shape, ground] (C) at (0,0){a}; I don't get the desired result (the red node is not rotated). Am I missing something? – Spike Aug 27 '12 at 17:30
@Spike: Never mind, my comment is wrong. I got confused about which node I was altering. – Jake Aug 27 '12 at 21:18