# Is it possible to apply a style to all (tikz) rectangles?

I know that it's possible to apply styling to all tikz nodes or paths, as in the following example:

\tikzset{
every node/.style={
text=red
},
every path/.style={
color=green,
thin
}
}


(Which makes all nodes have red text and all paths have thin green lines.)

Is there a similar way to apply styling to all tikz rectangles, so those produced by draw commands such as \draw (1,1) rectangle (3,3);?

I tried the following, on the off-chance it might work (it didn't, hence my question!):

\tikzset{
every rectangle/.style={%
blue,
rounded corners,
ultra thick
}
}


My current work-around is to use a style which I apply to every rectangle manually, although this is a little tedious. This is demonstrated in the below minimal working example.

\documentclass{report}

\usepackage{tikz}

\tikzset{
every rectangle/.style={
blue,
rounded corners,
ultra thick
},
my rectangle/.style={
blue,
rounded corners,
ultra thick
}
}

\begin{document}

\begin{tikzpicture}

% A rectangle which would be styled by the "every rectangle" bit if
% it worked in the way that I would like it to:
\draw (1,5) rectangle (4,7);

% A rectangle with my custom styling applying to it:
\draw [my rectangle] (1,1) rectangle (4,3);

\end{tikzpicture}

\end{document}


The rectangles in the above would be identical (besides position) if every rectangle worked in the way I would like it to or if there's some other way to style all rectangles.

So essentially I would like to know if an actual equivalent to my pseudo every rectangle exists.

-
No, unfortunately not. rectangle is just a path command that gets translated into coordinates immediately, so it's not really treated as a special object. Also, this would be difficult from another perspective: You could have a \path command with a rectangle and a circle in it. If you had an every rectangle and an every circle style, which one would apply in that case, as paths can only have one drawing style. You might need to work with rectangle nodes in this case. –  Jake Mar 6 '12 at 22:47
Ah, that's a shame. Rectangle nodes - am I correct in thinking they are drawn with commands like: \node [draw, rectangle] ...? Do you know if a style could be applied to all of those in a similar manner to how I've been describing? –  Staves Mar 6 '12 at 23:59
@Jake: Oh and cheers for the clear explanation, I can see why it would be problematic thanks to the other perspective you provided ^^ –  Staves Mar 7 '12 at 0:06
It is possible to implement similar behaviour through styling every rectangle node and using rectangle nodes as Jake mentioned. I will post an answer with an example once I am able to do so. (There's a block on me doing so for another 5 hours due to my reputation being less than 100.) –  Staves Mar 7 '12 at 0:48
@Jake every circle style exists in the doc pgf 2.1 cvs : "You can use this key to set up, say, a default radius for every circle." but I think we can't do some limited actions like changed radius with r or setup things like radius= 1cm. –  Alain Matthes Mar 7 '12 at 6:14

Unfortunately, you can not define a style that automatically applies to all rectangles. rectangle is just a path command that gets translated into coordinates immediately, so it's not really treated as a special object. Also, this would be difficult from another perspective: You could have a \path command with a rectangle and a circle in it. Paths can only have one drawing style, so if you had an every rectangle and an every circle style, which one would apply in that case?

Two alternative approaches spring to mind:

1. Use edge instead of rectangle. Then you can specify every edge/.style={to path={rectangle (\tikztotarget)} and the necessary drawing options. Of course, you'll have to take care if you use edge in other contexts.
2. Use nodes instead of just paths for your rectangles. They allow the use of styles like every rectangle node/.style.

Here's an example with both approaches:

\documentclass{report}

\usepackage{tikz}

\tikzset{
rectnode/.style args={#1,#2}{
draw,
blue,
rounded corners,
ultra thick,
minimum width=#1 cm,
minimum height=#2 cm,
anchor=south west
},
every edge/.style={
draw,
ultra thick,
blue,
rounded corners,
to path={rectangle (\tikztotarget)}
}
}

\begin{document}

\begin{tikzpicture}
\draw (1,5) edge (4,7);

\node [rectnode={3,2}] at (1,1) {};
\end{tikzpicture}

\end{document}

-
Cheers Jake! I particularly like the edge-based solution as I can still use coordinates as in \draw ... [rectangle] ... and I don't have to apply a style to each edge manually ^^ –  Staves Mar 7 '12 at 1:55

Based on Jake's original comment, some searching and a little experimentation I arrived at the solution of applying a style to every rectangle node, which Jake also mentions in his answer.

Here is a brief example using this idea:

\documentclass{report}

\usepackage{tikz}

\tikzset{
every rectangle node/.style={
color=blue,
ultra thick
}
}

\begin{document}

\begin{tikzpicture}

% A rectangle node which will have the "every rectangle node" styling applied to it:
\node at (1,3) [draw, rectangle] {Node 1};

% The "color=blue" styling also applies itself to the text attached to this node,
% although it does not explicitly have a rectangle type
\node at (1,2) {Node 2};

% However it is not applied to this one, as it has a defined type (circle):
\node at (1,1) [circle] {Node 3};

\end{tikzpicture}

\end{document}


Which produces (zoomed and cropped somewhat):

The slight caveat that this example (hopefully) highlights is that the every rectangle node styling is also applied to node 2. This initially seemed odd to me as that node doesn't have rectangle specified as a type, but I believe that this happens because nodes have rectangle as a default type. One workaround in this situation is to use a node of type circle without drawing, as demonstrated with node 3.

So to answer my original question - every rectangle node (in conjunction with rectangle nodes) can be used to produce equivalent results to my desired pseudo every rectangle, with the above caveat.

-
Due to the described generally undesirable (at least to me) behaviour and Jake's edge-based solution being more elegant (in my humble opinion), I shall be using that. My answer is just to expand on the possibility of using every rectangle node. –  Staves Mar 7 '12 at 6:55