[Tex/LaTex] Is it possible to apply a style to all (tikz) rectangles

tikz-pgftikz-styles

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 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}