# TikZ: Node fill which is opaque to entities in tikzpicture but transparent to background color/image? [duplicate]

Inside a tikzpicture, I am using a white fill for the label of nodes and lines. This generally works well for my needs, but I realized it is not perfect after putting a picture on top of a background image. For my question and MWE here, I am using a simple background color for illustrative purposes. However, I hope that the answer can also be used where there is a background image (e.g. in beamer), hopefully without any modification.

MWE - works with white background only

\documentclass{article}

\usepackage{tikz}%

\begin{document}

\begin{tikzpicture}

\tikzset{mwe/.style={inner sep=1.15ex, text depth=0ex, text height=1ex, text width=0.5em, minimum size=0.5em}}%

\node (A) [mwe,fill=white] at (0,0) {A};%
\node (B) [mwe,fill=white] at (4,0) {B};%
\draw[bend right=45,line width=0.25mm,-]  (A) to node [midway,fill=white, text height=1.25ex] {ab} (B);%

\node (C) [mwe,fill=white] at (0,4) {C};%
\node (D) [mwe,fill=white] at (2,-4) {D};%
\draw[bend left=15,line width=0.25mm,-]  (C) to node [midway,fill=white, text height=1.25ex] {cd} (D);%

\node (E) [mwe,fill=white] at (-2,2) {E};%
\node (F) [mwe,fill=white] at (4,-2) {F};%
\draw[bend left=15,line width=0.25mm,-]  (E) to node [midway,fill=white, text height=1.25ex] {ef} (F);%

\end{tikzpicture}

\end{document}


MWE - different background color

\documentclass{article}

\usepackage{pagecolor}%

\usepackage{tikz}%

\begin{document}

\pagecolor{red!30}

\begin{tikzpicture}

\tikzset{mwe/.style={inner sep=1.15ex, text depth=0ex, text height=1ex, text width=0.5em, minimum size=0.5em}}%

\node (A) [mwe,fill=white] at (0,0) {A};%
\node (B) [mwe,fill=white] at (4,0) {B};%
\draw[bend right=45,line width=0.25mm,-]  (A) to node [midway,fill=white, text height=1.25ex] {ab} (B);%

\node (C) [mwe,fill=white] at (0,4) {C};%
\node (D) [mwe,fill=white] at (2,-4) {D};%
\draw[bend left=15,line width=0.25mm,-]  (C) to node [midway,fill=white, text height=1.25ex] {cd} (D);%

\node (E) [mwe,fill=white] at (-2,2) {E};%
\node (F) [mwe,fill=white] at (4,-2) {F};%
\draw[bend left=15,line width=0.25mm,-]  (E) to node [midway,fill=white, text height=1.25ex] {ef} (F);%

\end{tikzpicture}

\end{document}


Obviously, it would be simple enough to change the fill color to be the same as that of the page background. However, this is not possible to achieve this if you have a background image which has multiple/various colors in it.

Therefore, I am aiming for something like a fill that is effectively opaque to (or blocks out) other elements in the tikzpicture but is actually transparent/invisible to everything outside of the tikzpicture. It may be better to say that the fill is completely transparent...however it maintains the effect of erasing the lines, nodes, and any other elements in the tikzpicture behind/underneath.

Is this possible with tikz? Perhaps not possible using fill per se, but through any mechanism(s)?

edit: I wrote "translucent" originally...I meant "transparent". Sorry for the confusion!

Bonus: If the labels could also make foreground lines "disappear", that would be good. Except that labels should not be able to make other labels disappear. For example, in the first MWE, cd is underneath the ef line, so it would be nice to have that section of the ef line erased. The same is true for the ab label underneath the cd line. This would be a bonus, but is not necessary for an answer.

The intended image would look something like this, where any background/color would appear behind/around the labels:

• For a simple case like this you can work around the problem by defining all the nodes first, and drawing the lines via the nodes (example: bitbucket.org/snippets/torbjornt/4j7xo). That might not work for more complex things though. – Torbjørn T. Aug 2 '16 at 8:17
• @TorbjørnT. This seems to work for manual coding. However, I am working on a package for a new environment which will have an unknown number of lines drawn (anywhere from 0 to ??). The other problem is that I incorporate bends into the package. For example, c' and ab' would generally be within a bend, unlike my example above. I'll work on an edit with that incorporated to avoid confusion. – whatisit Aug 2 '16 at 8:54
• @TorbjørnT. I updated the code with additional nodes, the bends, and new images to reflect those changes. Hopefully this give an idea of how I think your solution (which works well for my original post) probably won't work as I need it to. – whatisit Aug 2 '16 at 9:12
• Indeed, that is an example or "more complex things", and shows why I posted a comment rather than an answer. – Torbjørn T. Aug 2 '16 at 11:09
• Maybe use a knockout group? But this is very PDF viewer dependent. (I can't test it for just this reason, in fact.) (Adobe Reader crashed repeatedly and I'm not sure it works properly in my version anyway. I don't know that knockout crashed it. It would probably have crashed anyhow. It is crap, basically.) – cfr Aug 3 '16 at 0:57

As @cfr said, this is viewer dependent.

\documentclass[tikz]{standalone}
\usepackage{pagecolor}\pagecolor{red!30}
\begin{document}
\begin{tikzpicture}[transparency group=knockout]
\node(A)at(0,0){A};
\node(B)at(4,0){B};
\node(C)at(0,4){C};
\node(D)at(2,-4){D};
\node(E)at(-2,2){E};%
\node(F)at(4,-2){F};%
\draw[bend left=15](C)to node(cd){}(D);
\draw[bend right=45](A)to node(ab){}(B);
\draw[bend left=15](E)to node(ef){}(F);%
\path(ab)node[fill,opacity=0,text opacity=1]{ab}
(cd)node[fill,opacity=0,text opacity=1]{cd}
(ef)node[fill,opacity=0,text opacity=1]{ef};
\end{tikzpicture}
\end{document}


• Thanks for adding the example. I wanted to answer but I had no way of testing my code since acroread kept crashing and Okular doesn't support this. (The example in the manual doesn't appear properly either.) So I was sure it should work but couldn't know that it did! – cfr Aug 4 '16 at 22:22
• @Symbol-1 the viewer dependency is acceptable for my immediate purposes, and this appears to accomplish what I need (in my non-MWE). Thanks! I plan to accept this as an answer, but I am working on a slight variaton which incorporates this and something else, which I will post as an alternate answer. I have a huge deadline on Friday, so I will get these two things next weekend. The reason I'd like to hold of marking it is in case any alternatives come and/or if my alternate answer sparks any edits to this one that might improve it somehow. – whatisit Aug 7 '16 at 5:35