12

I have always wondered whether it was possible to define some sort of "relative" transparency in TikZ. What do I mean?

Sometimes, you want a node to be filled in order for its content to be readable among lines and shapes. Now, imagine you have a TikZ picture on some shaded background. It would be great if one could specify that the node should be opaque with respect to the rest of the picture (or the scope), but stay transparent with respect to the background defined outside.

\documentclass{article}

\usepackage{framed,xcolor,tikz}
\definecolor{shadecolor}{gray}{.85}

\begin{document}

\begin{shaded}
   \begin{tikzpicture}
      \draw (0,0) --node[fill=shadecolor] {a} (5,0);
   \end{tikzpicture}
\end{shaded}

\end{document}

In the above situation, it is possible to use shadecolor, because this is how the shaded environment works. I would like to know whether a more general solution exists, e.g. one that works in situations where you cannot find out the background color of the surrounding environment.

Updated information

I will try to state things a bit clearer. I often use TikZ for geometric figures and/or plots. Then, sometimes you have lines running through a coordinate label. In most cases, it is enough to slightly change the figure, e.g. by moving the label to a different anchor or even adapt some angle and thus have the line just miss the label.

However, in some cases, there is no other way than to put the label in a filled node with an inner sep of 1 or 2 points, so that it stands out among all other stuff. The main problem is that I have to use the background color to fill my node, normally this is white, but sometimes it might be some gray color.

The example I included is very easy, just to illustrate the problem. In this simple case, I could, of course, just draw the line in two pieces. In a normal use case, this is not possible.

So another way of saying it: I would love to draw the figure just as if there was a white background, using fill=white for such nodes. And then, after all is finished, I could say that all white stuff becomes transparent, so that the gray (or whatever) background shines through.

Using percusse's solution with transparency groups, it is not possible to achieve what I am looking for. The whole scope becomes partially transparent, so the line is not black anymore and the background is still lightened up. I would want the line to stay completely black and the node fill to be completely invisible.

enter image description here

18

This is done via transparency groups and relies mostly on PDF rendering so sometimes it might go awry. Here is an example that within the scope everything is as usual overprinting with respect to drawing order but the overall result is transparent with respect to the background.

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\path[pattern=crosshatch dots light steel blue](-2,-2) rectangle (2,2);
\begin{scope}[transparency group,opacity=.6]
\node[fill=yellow] {Heeeeeeeeeeeeeeeeeeey!};
\node[align=center,fill,circle,text=white] {Transparent\\ but overpaints\\ the rectangle};
\end{scope}
\end{tikzpicture} 
\end{document}

enter image description here

  • Thank you for the idea, I missed that in the manual. However, I cannot see how to solve the initial issue with this, maybe I misunderstood your solution: the fill of the node should be fully opaque w.r.t. the line, but fully transparent w.r.t. the background defined outside of tikzpicture. With a transparency group, the background will still lighten up and the line would be 100% black. – Philipp Imhof Aug 30 '15 at 17:36
  • @PhilippImhof I don't understand the line example in your description. If you make the whole TikZ picture a transparency group then it would be transparent with respect to the page no? Do you mind making a visual example with more elements? – percusse Aug 30 '15 at 22:45
  • I updated the description. If the transparency group becomes transparent with respect to the page, it becomes invisible. Transparency will apply to all group members. However, I would want the line to remain black. The node fill should become transparent, but still cover (interrupt) the line. – Philipp Imhof Aug 31 '15 at 4:21
  • @PhilippImhof A painted pixel can't be "unpainted"... – Paul Gaborit Aug 31 '15 at 5:19
  • 1
    @PhilippImhof I think now I'm starting to understand your point. Let me think about it for a while. But in any case it would require a very very recent viewer and printer to be useful. Because these are pretty recent specifications in PDF standards >v1.7 – percusse Aug 31 '15 at 20:14

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.