# Balancing opacities between fill and draw [duplicate]

Probably my Title is not so informative. My problem is that I try to "draw" hyperedges on some vertices with the following code:

\usepackage{tikz}

\tikzstyle{vertex} = [fill, shape=circle, opacity=1, node distance=80pt]
\tikzstyle{hyperedge} = [fill, opacity=1, cap=round, join=round, line width=60pt]
\tikzstyle{elabel} = [fill, shape=circle, node distance=30pt]

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\begin{tikzpicture}
\node[vertex,label=above left:$v_1$] (v1) {};
\node[vertex,right of=v1,label=above right:$v_2$] (v2) {};
\node[vertex,below of=v1,label=below left:$v_3$] (v3) {};
\node[vertex,right of=v3,label=below right:$v_4$] (v4) {};

\begin{pgfonlayer}{background}

\draw[hyperedge, color=yellow] (v1.center)--(v2.center)--(v3.center)--cycle;
\draw[hyperedge, color=pink, line width=45pt] (v2.center)--(v3.center)--(v4.center)--cycle;

\end{pgfonlayer}

\node[elabel,color=yellow,label=right:$C_1$]  (e1) at (-3,0) {};
\node[elabel,below of=e1,color=pink,label=right:$C_2$]  (e2) {};
\end{tikzpicture}

\end{document}


This produces the following:

It is not bad, but the intersection is not so visible. If I set opacity of the hyperedges to 0.5:

\tikzstyle{hyperedge} = [fill, opacity=0.5, cap=round, join=round, line width=60pt]


I get:

The intersection is more visible here, but since the "fill" and the "lines" of the cycles also intersect, the affected parts become more opaque(?). Is there a workaround for this situation? Maybe to draw the lines only on one side of the cycle avoiding intersection with the fill, but is it possible?

## marked as duplicate by user36296, Phelype Oleinik, dexteritas, Circumscribe, TroyJan 11 at 14:42

• Welcome to the site. Look at the blend modes in the PGF/TikZ manual. – percusse Feb 21 '18 at 16:59
• Thank you @percusse! There are many blend modes as I see. I'm not sure which one would fit the color1+color1=color1 criteria with the same opacity, but I will try to find one. – Mathiassa Feb 21 '18 at 17:09

Dirty hack:

If the lines of your particular shape are a tiny bit broader, you don't need to worry about any fill colour as the lines fill the whole shape:

\documentclass{standalone}

\usepackage{tikz}

\tikzstyle{vertex} = [fill, shape=circle, opacity=1, node distance=80pt]
\tikzstyle{hyperedgeline} = [opacity=0.5, cap=round, join=round,line width=60pt]
\tikzstyle{elabel} = [fill, shape=circle, node distance=30pt]

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\begin{tikzpicture}
\node[vertex,label=above left:$v_1$] (v1) {};
\node[vertex,right of=v1,label=above right:$v_2$] (v2) {};
\node[vertex,below of=v1,label=below left:$v_3$] (v3) {};
\node[vertex,right of=v3,label=below right:$v_4$] (v4) {};

\begin{pgfonlayer}{background}

\draw[hyperedgeline, color=yellow] (v1.center)--(v2.center)--(v3.center)--cycle;
\draw[hyperedgeline, color=pink, line width=47pt] (v2.center)--(v3.center)--(v4.center)--cycle;

\end{pgfonlayer}

\node[elabel,color=yellow,label=right:$C_1$]  (e1) at (-3,0) {};
\node[elabel,below of=e1,color=pink,label=right:$C_2$]  (e2) {};
\end{tikzpicture}

\end{document}


• @Mathiassa And I love your clean and correct answer :) – user36296 Feb 21 '18 at 20:57

I think to proper solution is this. I found it in the manual:

Remove opacity from the "hyperedge" style:

\tikzstyle{hyperedge} = [fill, cap=round, join=round, line width=60pt]


Put every edge in its own transparency group:

\begin{scope}[transparency group,  opacity=0.5]
\draw[hyperedge, color=yellow] (v1.center)--(v2.center)--(v3.center)--cycle;
\end{scope}

\begin{scope}[transparency group,  opacity=0.5]
\draw[hyperedge, color=pink, line width=45pt] (v2.center)--(v3.center)--(v4.center)--cycle;
\end{scope}


• We are the ones who should thank you! Thanks for sharing your answer with the community. Together we are strong! – Paulo Cereda Feb 21 '18 at 18:55

Use fill opacity.

\documentclass{article}
\usepackage{tikz}

\tikzstyle{vertex} = [fill, shape=circle, opacity=1, node distance=80pt]
\tikzstyle{hyperedge} = [opacity=0.5,fill opacity=1, cap=round, join=round, line width=60pt]
\tikzstyle{elabel} = [fill, shape=circle, node distance=30pt]

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\begin{tikzpicture}
\node[vertex,label=above left:$v_1$] (v1) {};
\node[vertex,right of=v1,label=above right:$v_2$] (v2) {};
\node[vertex,below of=v1,label=below left:$v_3$] (v3) {};
\node[vertex,right of=v3,label=below right:$v_4$] (v4) {};

\begin{pgfonlayer}{background}

\draw[hyperedge, color=yellow] (v1.center)--(v2.center)--(v3.center)--cycle;
\draw[hyperedge, color=pink, line width=45pt] (v2.center)--(v3.center)--(v4.center)--cycle;

\end{pgfonlayer}

\node[elabel,color=yellow,label=right:$C_1$]  (e1) at (-3,0) {};
\node[elabel,below of=e1,color=pink,label=right:$C_2$]  (e2) {};
\end{tikzpicture}

\end{document}


• +1, however there is a tiny yellow triangle in the middle of the pink shape :) – user36296 Feb 21 '18 at 17:22
• @samcarter Thanks. I'll (try to) fix it later... (need to run now) – marmot Feb 21 '18 at 17:27
• @samcarter I am actually really surprised that it is there and impressed by the OP. I am also surprised that TikZ builds the shapes like this, meaning that the transparency group thing should not be necessary. But thanks to the OP I learned something. ;-) – marmot Feb 21 '18 at 20:59
• @marmot I guess the tiny triangle is there because fill is no longer in the hyperedge style, so you only draw the lines, but you are not filling the shape (just like me :) and the triangle is the small area which is not covered by the line. (I totally agree that this behaviour of tikz is strange) – user36296 Feb 21 '18 at 21:12
• @samcarter Yes, I agree with everything you wrote. It's just really strange that TikZ sort of knows what it is supposed to do when one uses these transparency groups, and otherwise not. Why isn't that the default behavior? (Or is it a triangular duck? It has the same color, after all... ;-) – marmot Feb 21 '18 at 21:35