为什么范围内的不透明度变化不会应用于背景层?

为什么范围内的不透明度变化不会应用于背景层?

我有一些复杂的示意图,想通过演示来揭示它们。

原理图位于我不想修改的外部文件中,为此,我使用了可以定义或未定义的样式标签。如果未定义,它们将变为空样式。否则,它们可以更改不透明度(或其他)。像往常一样,这些样式可以应用于路径或范围。

这种方法效果很好,直到我为了改善图片效果在背景中添加了一些绘画部分(参见答案如何定义在背景层上绘制一些线条的样式?)。

现在发生了

  • 不透明度变化始终正确应用于前景层

  • 如果在路径样式中使用,它们会正确应用于背景层\draw[..., opacity=0.5] ...

  • 他们是不是应用于一定范围内的背景层 \begin{scope}[opacity=0.5]...\end{scope}(但不透明度变化正确应用于前景)

这是一个 mwe,说明了我想要做的事情并展示了问题

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

% to put something on the background
\tikzset{%
  on layer/.code={
    \pgfonlayer{#1}\begingroup
    \aftergroup\endpgfonlayer
    \aftergroup\endgroup
  }
}

% to process undefined tags
\pgfkeys{/tag/.is family, /tag, 
.unknown/.code = {
    \pgfkeyssetvalue{\pgfkeyscurrentpath/\pgfkeyscurrentname}{}
  }
}

% opacity changing styles
\pgfkeys{
  /tikz/greyed/.style={opacity=0.4},
  /tikz/hidden/.style={opacity=0},
}

% drawned of bg layer
\tikzset{
  wire/.style={
    thick,
    red,
    on layer=background},
}

\begin{document}
\centering

 Inital picture\\
\begin{tikzpicture}[]
  \node[draw, blue, /tag/A] (A) at (0,0) {A} ;
  \draw[wire,/tag/A] (-1,0) -- (A) ;
  \node[draw, blue, /tag/B] (B) at (2,0) {B} ;
  \draw[wire,/tag/B] (A) -- (B) ;
  \node[draw, blue, /tag/C] (C) at (4,0) {C} ;
  \draw[wire,/tag/C] (B) -- (C) ;
  \draw[wire,/tag/C] (C) -- ++(1,0) ;
\end{tikzpicture}
\bigskip

Picture with element opacity modified in the element style\\
All is properly processed\\
\begin{tikzpicture}[
  /tag/B/.style={greyed},
  /tag/C/.style={hidden},
  ]
  \node[draw, blue, /tag/A] (A) at (0,0) {A} ;
  \draw[wire,/tag/A] (-1,0) -- (A) ;
  \node[draw, blue, /tag/B] (B) at (2,0) {B} ;
  \draw[wire,/tag/B] (A) -- (B) ;
  \node[draw, blue, /tag/C] (C) at (4,0) {C} ;
  \draw[wire,/tag/C] (B) -- (C) ;
  \draw[wire,/tag/C] (C) -- ++(1,0) ;
\end{tikzpicture}
\bigskip

Picture with element opacity modified in scope style\\
Nodes in foreground are modified, but wires in bg are not\\
\begin{tikzpicture}[
  /tag/B/.style={greyed},
  /tag/C/.style={hidden},
  ]
  \begin{scope}[/tag/A]
  \node[draw, blue] (A) at (0,0) {A} ;
  \draw[wire] (-1,0) -- (A) ;
  \end{scope}
  \begin{scope}[/tag/B]
  \node[draw, blue] (B) at (2,0) {B} ;
  \draw[wire] (A) -- (B) ;
\end{scope}
\begin{scope}[/tag/C]
  \node[draw, blue] (C) at (4,0) {C} ;
  \draw[wire] (B) -- (C) ;
  \draw[wire] (C) -- ++(1,0) ;
\end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

有没有解决方法可以在范围内将不透明度更改应用于 bg 层?

答案1

您可以存储不透明度并在您的线路样式中明确设置它:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

% to put something on the background
\tikzset{%
  on layer/.code={
    \pgfonlayer{#1}\begingroup
    \aftergroup\endpgfonlayer
    \aftergroup\endgroup
  }
}

% to process undefined tags
\pgfkeys{/tag/.is family, /tag,
.unknown/.code = {
    \pgfkeyssetvalue{\pgfkeyscurrentpath/\pgfkeyscurrentname}{}
  }
}


\pgfkeys{/tikz/storeopacity/.store in=\currentopacity, /tikz/storeopacity=1}
% opacity changing styles
\pgfkeys{
  /tikz/greyed/.style={opacity=0.4,storeopacity=0.4},
  /tikz/hidden/.style={opacity=0,storeopacity=0},
}

% drawned of bg layer
\tikzset{
  wire/.style={
    thick,
    red,
    opacity=\currentopacity,
    on layer=background},
}

\begin{document}
\centering

 Inital picture\\
\begin{tikzpicture}[]

  \node[draw, blue, /tag/A] (A) at (0,0) {A} ;
  \draw[wire,/tag/A] (-1,0) -- (A) ;
  \node[draw, blue, /tag/B] (B) at (2,0) {B} ;
  \draw[wire,/tag/B] (A) -- (B) ;
  \node[draw, blue, /tag/C] (C) at (4,0) {C} ;
  \draw[wire,/tag/C] (B) -- (C) ;
  \draw[wire,/tag/C] (C) -- ++(1,0) ;
\end{tikzpicture}
\bigskip

Picture with element opacity modified in the element style\\
All is properly processed\\
\begin{tikzpicture}[
  /tag/B/.style={greyed},
  /tag/C/.style={hidden},
  ]
  \node[draw, blue, /tag/A] (A) at (0,0) {A} ;
  \draw[wire,/tag/A] (-1,0) -- (A) ;
  \node[draw, blue, /tag/B] (B) at (2,0) {B} ;
  \draw[wire,/tag/B] (A) -- (B) ;
  \node[draw, blue, /tag/C] (C) at (4,0) {C} ;
  \draw[wire,/tag/C] (B) -- (C) ;
  \draw[wire,/tag/C] (C) -- ++(1,0) ;
\end{tikzpicture}
\bigskip

Picture with element opacity modified in scope style\\
Nodes in foreground are modified, but wires in bg are not\\
\begin{tikzpicture}[
  /tag/B/.style={greyed},
  /tag/C/.style={hidden},
  ]
  \begin{scope}[/tag/A]
  \node[draw, blue] (A) at (0,0) {A} ;
  \draw[wire] (-1,0) -- (A) ;
  \end{scope}
  \begin{scope}[/tag/B]
  \node[draw, blue] (B) at (2,0) {B} ;
  \draw[wire] (A) -- (B) ;
\end{scope}
\begin{scope}[/tag/C]
  \node[draw, blue] (C) at (4,0) {C} ;
  \draw[wire] (B) -- (C) ;
  \draw[wire] (C) -- ++(1,0) ;
\end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容