Relative transparency in TikZ?

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


I just had a similar question marked as a duplicate of this one. Here is the link. Unfortunately, the answer posted here was not marked as an answer and OP indicated it as not working. Credit for this approach goes to @symbol-1.

As noted by @percusse here and @cfr and @symbol-1 in the link, the result will be viewer dependent. Examples of viewer differences provided below the code.

Here is modified from the OP and @percusse's answer using transparency group=knockout in the scope environment options:

\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=knockout]
        \draw (-2,0) to node(aaaaa){} (2,0);
        \path (aaaaa) node[fill,opacity=0,text opacity=1]{aaaaa};
    \end{scope}
\end{tikzpicture}

\end{document}

You can change the background to anything you like and no other changes are necessary. If you change the text to be longer/short, the background opacity will resize accordingly - i.e. no changes other than the label name.

In some PDF viewers (e.g. SumatraPDF, TeXworks previewer, etc.): Result of non-conforming PDF viewers

In other PDF viewers (e.g. Adobe Acrobat): Result of conforming PDF viewers

The PDF is the same in both cases. I simply took a screenshot of it opened in two different PDF viewers. It seems the background is affected as well, but the focus here is the transparency.

Also, here is an example of an additional node with text going vertically to illustrate what happens with transparency group=knockout with multiple nodes:

\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=knockout]
        \draw (-2,0) to node(aaaaa){} (2,0);
        \path (aaaaa) node[fill,opacity=0,text opacity=1]{aaaaa};
        \draw (0,-2) to node(bbbbb){} (0,2);
        \path (bbbbb) node[fill,opacity=0,text opacity=1]{\rotatebox[origin=c]{90}{bbbbb}};
        \draw (-2,-0.35) to node(ccccc){} (2,-0.35);
        \path (ccccc) node[fill=red,text opacity=1]{ccccc};
    \end{scope}
\end{tikzpicture}

In some PDF viewers (e.g. SumatraPDF, TeXworks previewer, etc.): Result of second example in non-conforming PDF viewers

In other PDF viewers (e.g. Adobe Acrobat): Result of second example in conforming PDF viewers

You will notice that whichever item is last with opacity=0 takes precedence over the previous item(s). If you flip the aaaaa and bbbbb lines and nodes (i.e bbbbb occurs first), for example, you'll see this (in the conforming PDF viewers): Result of third example in conforming PDF viewers

opacity=0 also takes precedence over other items which do not have opacity. For example, if we move the ccccc node/lines first within the tikzpicture, here is the result: Result of fourth example in conforming PDF viewers

Just remember! If your picture is very complicated and you open it in an old or (transparency) non-conforming PDF viewer, it will not be what you expect.

If you have many nodes with transparency and you do not like the overlapping issue, you will need to adjust the order of the nodes, xshift and yshift, and/or place the nodes in different positions completely.