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.
Best Answer
Unfortunately, you can not define a style that automatically applies to all
rectangle
s.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 anevery rectangle
and anevery circle
style, which one would apply in that case?Two alternative approaches spring to mind:
edge
instead ofrectangle
. Then you can specifyevery edge/.style={to path={rectangle (\tikztotarget)}
and the necessary drawing options. Of course, you'll have to take care if you useedge
in other contexts.every rectangle node/.style
.Here's an example with both approaches: