# When to use TikZ styles?

I planned to illustrate how a quadtree data structure I'm using is implemented, showing all internal pointers and arrays. The image will look something like this:

(Here, if a pointer points at the symbol ∅ it means that it is a null pointer, i.e. it has the value 0)

I wanted to use something like in this question to illustrate what the linking looks like. What I noticed is that the code featured in this question defines the style box which is used to draw an array that consists of two elements.

My first question is: When should a style be used? Why doesn't the code in the question I linked to use a macro for drawing the rectangle that represents the array for example?

A second I have question is: How do they work? I haven't been able to find very much about them in the pgf manual (see pages 493–494), so I really don't know at all what is going on when a style is used. Is the code defined in the style kind of like copied and pasted into the place where the style is used, and how exactly does that happen?

A third I have question is: If a style is used to draw a rectangle like in this case, how can I make it draw the rectangle so that it is scaled accordingly with the definitions of x and y? For example, I begin my TikZ picture with the code \begin{tikzpicture}[x={(.035\textwidth,0)},y={(0,.035\textwidth)}]. The value of x and y may vary from picure to picture, and so also the scale of what is drawn. How can I make the rectangle use the values of x and y? I have tried to change inner sep=2ex in the code in the question I linked to, which is an absolute distance, to a distance that depends on x and y but I haven't succeeded.

-
Styles are roughly option templates such that when provided you don't have to repeat for each object. But your question is not clear. What's the exact problem that you can't do with styles or why do you need styles? I can't see how the linked drawings are relevant for your quadtree structure. What does it mean to visualize an array? Can you give a simple example so that we are on the same page about the concepts? –  percusse Jul 26 '12 at 9:55

I think it's preferable to use style every time and every where. The code is more readable and there if you look at pgfkeys you can find a lot of possibilities that you can use inside styles. below you can find a first version to get something like you want. Perhaps you can find a better answer because there are a lot of possibilities but you need to use styles in each way. If necessary, I can explain the next code.

Question 1 : always because it's easy to modify a picture with the styles

It's possible to scale the tree with [x=.5cm,y=.5cm]and with scale inside the styles for the nodes.

Update

\documentclass[11pt]{article}
\usepackage{tikz,amsmath}
\usetikzlibrary{arrows,shapes.multipart,calc}

\begin{document}
\pgfmathsetmacro{\wdbox}{.02\textwidth}
\tikzset{
>= stealth,
every picture/.style={ultra thick},
every node/.style={anchor=north},
simple/.style={draw,minimum size=3*\wdbox,scale=.8},
array/.style={%
draw,scale=.8,
inner sep=\wdbox,
rounded corners,
rectangle,
rectangle split,
rectangle split parts=4,
rectangle split ignore empty parts=false,
rectangle split horizontal,
append after command={%
\pgfextra{\let\mainnode=\tikzlastnode}
coordinate (c1 \mainnode) at ($(\mainnode.west)!.5!(\mainnode.one split)$)
coordinate (c2 \mainnode) at ($(\mainnode.one split)!.5!(\mainnode.two split)$)
coordinate (c3 \mainnode) at ($(\mainnode.two split)!.5!(\mainnode.three split)$)
coordinate (c4 \mainnode) at ($(\mainnode.three split)!.5!(\mainnode.east)$)
}
}
}

\begin{tikzpicture}[x=.035\textwidth,y=.035\textwidth]
\node[simple] (1) {};
\draw[->] (1.center) -- +( 0,-2) node[array]  (2) {};
\draw[->] (c1 2)     -- +(-3,-2) node[simple] (3) {};
\draw[->] (c2 2)     -- +(-1,-2) node[simple] (4) {};
\draw[->] (c3 2)     -- +( 0,-2) node         (5) {$\emptyset$};
\draw[->] (c4 2)     -- +(+3,-2) node[simple] (6) {};
\draw[->] (3.center) -- +( 0,-2) node[array]  (7) {};
\draw[->] (4.center) -- +( 0,-2) node         (8) {$\emptyset$};
\draw[->] (c1 7)     -- +(-3,-2) node[simple] (9) {};
\end{tikzpicture}
\end{document}

-
Thanks, I'll look up pgfkeys! Is it possible to make inner sep depend on x and y kind of like when you define a coordinate in a TikZ picture? I mean, it's quite impractical to define the size of the rectangles in any of the predefined length units while the lengths of the pointers are defined in terms of x and y. That means that if I change x and y (which is my preferred way of rescaling a picture), some parts of the picture will be rescaled while other parts won't. –  StrawberryFieldsForever Jul 26 '12 at 14:00
@StrawberryFieldsForever Yes it's possible. I updated my answer. I kept scale for the node because you can adapt the size if it's necessary. –  Alain Matthes Jul 26 '12 at 16:54
Ah, okay, you solve it with a help variable. And then x and y can be defined to be dependent of the help variable, so it is only that you have to change. Nice solution, thank you very much. –  StrawberryFieldsForever Jul 26 '12 at 18:44