146

When I use the tikz key opacity for a polygon in a tikzpicture, it applies to the drawn fill as well as to the draw lines.

I would like to have it this way that the lines remain untouched by the opacity key, so that they behave as if they had opacity=1.

(What I do now is redraw the polygon with fill=none, but that is a crippled solution to the problem).

Martin Scharrer
  • 262,582
romeovs
  • 9,102

1 Answers1

214

You can specify the fill opacity separately from the draw opacity. In the first example below I used opacity and in the second I used fill opacity which will only affect the fill and not the draw.

enter image description here

Note:

  • As pointed out by Martin Scharrer: Specifying \opacity=<x> affects the fill, draw and text so is equivalent to saying fill opacity=<X>, draw opacity=<X>, text opacity=<X>.
  • fill opacity applies not only to the fill, but also applies to text. To override this behavior, you can separately set the text opacity which applies only to the text labels. So, apply text opacity=1 to ensure that opacity is not applied to the text.

Code:

\documentclass[border=3pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \draw [ultra thick, draw=black, fill=yellow, opacity=0.2]
       (0,0) -- (0,1) -- (1,1) -- cycle;
\end{tikzpicture}
\begin{tikzpicture}
    \draw [ultra thick, draw=black, fill=yellow, fill opacity=0.2]
       (0,0) -- (0,1) -- (1,1) -- cycle;
\end{tikzpicture}
\end{document}
Peter Grill
  • 223,288
  • 28
    It should be noted that opacity=<X> is just a short-cut for fill opacity=<X>,draw opacity=<X>. Both are actual to different low-level settings in the output file (e.g. PDF). – Martin Scharrer Dec 21 '11 at 18:52
  • @MartinScharrer: Good point. Have added a note incorporating your comment. – Peter Grill Dec 21 '11 at 19:09
  • wow, sometimes things can be so easy... I thought I had already tried that. Great! – romeovs Dec 21 '11 at 20:54
  • 14
    Note that when there is text inside the node, it will also fade. Use text opacity=1. – oarfish Sep 01 '15 at 08:57
  • @oarfish: That was the intent of the second bullet point, but have expanded to make it more explicit. – Peter Grill Sep 03 '15 at 17:32
  • Unfortunately TikZ does not seem to be able to handle transparencies separeately in a transparency group, e.g., transparency group, fill opacity=0.3, text opacity=1 will not work as expected. – cgogolin Jan 17 '19 at 09:38
  • @cgogolin: I don't have experience with transparency groups so I recommend you post a new question with a fully compilable MWE including \documentclass and the appropriate packages that reproduces the problem. – Peter Grill Jan 17 '19 at 16:43