[Tex/LaTex] When to use TikZ styles

technical-drawingtikz-pgftikz-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:

enter image description here

(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.

Best Answer

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

enter image description here

\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} 
Related Question