4

I'm trying to draw an arrow with opacity=.35, but the shapes are overlapping, which makes the opacity stack.

I tried the solution of How can I draw a semi-transparent arrow in TikZ without internal overlap?.
But it doesn't work for me, because I'm using tikzpicture with the option overlay.

MWE:

\documentclass[varwidth, border=50]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}

\tikzset{->/.style={ultra thick, -{.Latex}, blue}}

\begin{tikzpicture}[overlay] \draw[->, opacity=.35] (0,.5) -- (1,0.5); \begin{scope}[opacity=.35] \draw[->] (0,0) -- (1,0); \end{scope} \begin{scope}[transparency group, opacity=0.5] \draw[->, red] (0,-0.5) -- (1,-0.5); \end{scope} \end{tikzpicture}

\end{document}

The third arrow doesn't show up.
In fact, none of the scope's content shows, when using overlay and transparency group together. When I remove either of those options, the scope contents are visible again.

Why is the scope not visible, and how can I make it visible?

Edit: Apparently the problem is known, and the manual suggests as a workaround to place the contents in a \transparencygroup within a tikzpicture within a node. Like this:

\node[left] at (1.15,-0.5) {
        \begin{tikzpicture}
            \pgfsetfillopacity{0.35}
            \pgftransparencygroup
            \draw[->, red] (0,-0.5) -- (1,-0.5);
            \endpgftransparencygroup
        \end{tikzpicture}
    };

The major drawback to this workaround is, that it requires manually adjusting the placement, which kinda defeats the purpose of using TikZ. Does anyone know a better way?

d8xa
  • 100
  • 2
    From TikZ & PGF Manual, v 3.12.7a, pp 367: Note that when a transparency group is created, TikZ must correctly determine the size of the material inside the group. Usually, this is no problem, but when you use things like overlay or transform canvas, trouble may result. In this case, please consult Section 115 on how to sidestep this problem in such cases. – Zarko Dec 29 '20 at 03:56
  • @Zarko Thank you. Seems like I skipped over that part when reading the manual. I'll edit the question to include the "sidestep", maybe someone knows a better workaround. – d8xa Dec 29 '20 at 14:17
  • 1
    Enclose your whole diagram in the pgfinterruptboundingbox environment instead, i.e. \begin{tikzpicture}\begin{pgfinterruptboundingbox}…\end{pgfinterruptboundingbox}\end{tikzpicture}. If you need this often, you can define your own environment or key for it, of course. The transparency group works now (because overlay is disables but the whole diagram's bounding box is thrown away at the end). The vertical alignment maybe needs adjustments but it does so with overlay, too, I believe. – Qrrbrbirlbel Sep 04 '23 at 12:35
  • @Qrrbrbirlbel Yep that seems to work, thank you! If you post it as an answer I can accept it. – d8xa Sep 21 '23 at 01:04

1 Answers1

0

As the manual states, transparency groups and overlay don't play nicely together:

Note that when a transparency group is created, TikZ must correctly determine the size of the material inside the group. Usually, this is no problem, but when you use things like overlay or transform canvas, trouble may result. In this case, please consult Section 115 on how to sidestep this problem in such cases.

Section 115 suggests to put the whole diagram in a node inside another TikZ picture but that's not necessary. You can use the pgfinterruptboundingbox instead.

Inside this environment PGF/TikZ keeps track of the bounding box (and the transparency group will work) but at the end of it the bounding box that existed before the environment (which is empty here) is restored.

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\tikzset{->/.style={ultra thick, -{.Latex}, blue}}
\begin{tikzpicture}
\begin{pgfinterruptboundingbox}
\draw[->, opacity=.35] (0,.5) -- (1,0.5);
\scoped[opacity=.35]
  \draw[->] (0,0) -- (1,0);
\scoped[transparency group, opacity=0.5]
  \draw[->, red] (0,-0.5) -- (1,-0.5);
\end{pgfinterruptboundingbox}
\end{tikzpicture}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821