Bug or feature: tikz interprets color specification differently for rectangles

You are using the wrong key. color sets fill, draw and text. That said, it will only set the color and does not apply it/the operation.

  • \draw[color=red] will draw a red frame (equals \draw[draw=red])
  • \draw[color=red,fill] will draw a red frame filled with red (equals \draw[fill=red] or \filldraw[red])
  • \draw[color=red,fill,draw=none] will draw a red fill but no frame (equals \fill[red])
  • \draw[color=red] node {Test}; will draw a node with red text (equals \draw[text=red] node {Test};

TLDR: Use draw to change the frame.

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \draw[color=red,very thick] (0,0) rectangle (1,1);
  \draw[color=red,very thick,fill=blue] (2,0) rectangle (3,1);
  \draw[very thick,fill=blue,draw=red] (4,0) rectangle (5,1);
\end{tikzpicture}
\end{document}

Yes it's a feature, because this color key rewrites the color value on the draw,fill and textkey, I quote the 3.1 manual on page 168 (15.2 Specifying a Color):

/tikz/color=<color name> (no default) This option sets the color that is used for fill, drawing, and text inside the current scope.

TikZ allows you to change the color value of the lines made with draw and the color value of the coloring made with fill separately.

The color key, assigns the same color (< color value >) to draw and fill. And since the keys are evaluated in the order in which they are written, the result you see is quite normal.

What does (no default) mean?

The majority of TikZ's usual keys can be called in 2 ways:

  • explicitly with for example \draw[color=red](0,0)rectangle(1,1); The key to which the red value applies is explicitly given, here the color key;

  • implicitly with \draw[red](0,0)rectangle(1,1);, here TikZ recognizes the color value and assigns it to the color key. It is not mandatory to say to which key the color value applies. TikZ implicitly assigns the color value to the color key.

On the other hand, if you write \draw[color], there is an error because the color value is not indicated. There is no default color. I quote the error:

Package pgfkeys Error: The key '/tikz/color' requires a value.


An example of a key that has a default value:

It is sometimes possible not to give a value to the key, in this case it takes the default value:

By example, the double key has the default value of white. You can omit the color and write

\draw[double](0,-1)--(2,-1);

Which is the same as \draw[double=white](0,-1)--(2,-1);

double-key

But it is possible to specify a color value other than the default value, for example, here cyan

\draw[double=cyan](0,-2)--(2,-2);

double-cyan

The pgfkeys

The options are given with the key=value system called pgfkeys described starting on page 946 of the 3.1 manual (86 Key Management).

  1. The first word on the left is always the name of the key (the option),
  2. the second word is always the name of the option value.

Thus:

  • color=red, the key is color, the value is red.
  • draw=blue, the key is draw, the value is blue.

The color word is designated differently in the manual:

/tikz/color=< color name >

  • the key is called color.
  • The manual write < color name > to indicate the value (red, blue, etc).

So, yes, we must be careful and specify what we are talking about. We commonly talk about "colour" but very often forget to specify whether it is the key or its value. This leads to confusion.

I corrected this answer and specified each time whether it is the key or its value. I hope that the answer is now clearer.

Tags:

Color

Tikz Pgf