14

When drawing a rectangle, Tikz interprets color in two different ways, depending on in which order it is specified among the options.

In the example below: When color is specified first, as in the first two example, it is used to color the frame of the rectangle---as I'd expect. But in the third rectangle, the red color specification actually fills the rectangle with red. Is this expected?

\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,color=red] (4,0) rectangle (5,1);
\end{tikzpicture}
\end{document}

3 squares

11

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.

  • I see now. I do not understand the (no default) part, since it seems the default color for text, fill and draw are black. – Máté Wierdl Jan 5 at 2:35
  • @MátéWierdl See here: tex.stackexchange.com/questions/370146/… – CarLaTeX Jan 5 at 7:44
  • @MátéWierdl I updated my answer, do you understand now? – AndréC Jan 5 at 8:34
  • I see, @AndréC, thanks. So it is different to say, draw or text or fill have default colors (they are black) and whether the key color has a default value. Same word "color", but different meaning. – Máté Wierdl Jan 6 at 2:05
  • 1
    @MátéWierdl I added some details about the pgfkeys system that manages the options and corrected my answer to avoid this ambiguity of language – AndréC Jan 6 at 7:54
16

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}
  • 2
    @Kpym I am not sure I agree. (Actually I like and upvoted your answer and am wondering why you removed it.) I guess the cleanest answer may be to just copy the \tikzoption{color}{....} code and discuss what it means. This also clarifies the "hierarchy" and "inheritances" between the different keys. And, as you know very well, it is actually nontrivial to figure out what the current color is. And there are much more advanced discussions IMHO such as this one and the links there... – user121799 Jan 3 at 20:37
  • @marmot I've tried to extend my post. Is it better now? – TeXnician Jan 4 at 8:23
  • @TeXnician Your answer was always good. I will remove my last comment now. The other comment will die with the one above it unless you ask me to remove it. ;-) – user121799 Jan 4 at 15:35
  • I now see completely (can I say "thanks"?). So specifying color=red or simply red sets the color for all three: text, fill, draw. But the color red is seen exactly when it is activated by text, fill, draw. It also seems that black is the default color specification. – Máté Wierdl Jan 5 at 2:42
  • In the answer, "\draw[color=red,draw=none] will draw a red fill but no frame (equals \fill[red])" needs to be changed by adding fill to the options, \draw[color=red,draw=none,fill]. – Máté Wierdl Jan 5 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.