“剪切”区域外部半透明?

“剪切”区域外部半透明?

为了突出显示我正在编写的演示文稿的图表的一部分,我想“圈出”一个区域并使该区域之外的所有内容都半透明。该区域内的所有内容都应保持不透明。有没有办法在 TikZ 中实现这一点?

举个例子,在下图中,我想让节点ac半透明,以及从ab位于圆外的边缘部分。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[vertex/.style={draw, fill={#1}}]
  \node (a) [vertex=blue] {};
  \node (b) [vertex=blue, right=of a] {};
  \node (c) [vertex=green, right=of b] {};

  \draw (a) edge (b);

  \draw [red] (b) circle [radius=0.75];
\end{tikzpicture}
\end{document}

当然,我可以通过手动将所需部分设为透明来实现这一点,但对于复杂的图片,最好使用某种裁剪。我已阅读手册,对裁剪和透明度略知一二,但我不知道如何将这些键组合在一起以获得我想要的效果。

答案1

这里有两次尝试,第一次并不理想,但通过覆盖半透明的白色矩形“伪造”透明度并使用该spy库:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{positioning,spy}
\tikzset{%
  white out/.style={
    preaction={%
      even odd rule, 
      fill=white, 
      fill opacity=0.75,
      insert path={
        % This covers the current picture.
        (current bounding box.south west) 
        rectangle
        (current bounding box.north east) 
      }
    }
  }
}

\begin{document}
\begin{tikzpicture}[vertex/.style={draw, fill={#1}}, spy using outlines={circle, magnification=1}]

  \node (a) [vertex=blue] {};
  \node (b) [vertex=blue, right=of a] {};
  \node (c) [vertex=green, right=of b] {};

  \draw (a) edge (b);

  \spy [size=0.75cm] on (b) in node [thin, red, white out] at (b);

\end{tikzpicture}
\end{document}

在此处输入图片描述

第二个管理半透明度,但必须在范围内捕获原始图片:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{positioning}

\newbox\tikzcapturebox
\tikzset{capture scope/.style={
   execute at begin scope={\global\setbox\tikzcapturebox=\hbox\bgroup}, 
   execute at end scope={\egroup\copy\tikzcapturebox}
},
captured scope as path picture/.style={
  preaction={path picture={\pgfextra{\copy\tikzcapturebox}}}
}}


\begin{document}
\begin{tikzpicture}[vertex/.style={draw, fill={#1}}]

\node [text=gray!50, font=\footnotesize] at (1.25,0) {some text behind the figure};

\begin{scope}[transparency group, opacity=0.5, capture scope]
  \node (a) [vertex=blue] {};
  \node (b) [vertex=blue, right=of a] {};
  \node (c) [vertex=green, right=of b] {};
  \draw (a) edge (b);
\end{scope}

\draw [red, captured scope as path picture] (a) circle [radius=0.5];
\draw [blue, captured scope as path picture] ([xshift=-0.25cm]c) circle [radius=0.25];

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这是一个非常简单的解决方案(技巧:白色背景)。

在此处输入图片描述

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[vertex/.style={draw, fill={#1}}]
  \node (a) [vertex=blue] {};
  \node (b) [vertex=blue, right=of a] {};
  \node (c) [vertex=green, right=of b] {};
  \draw (a) edge (b);
  \draw [red] (b) circle [radius=0.75];

  \fill[white,fill opacity=.75]
  (current bounding box.south west) rectangle (current bounding box.north east)
  (b) circle[radius=0.75cm+.5\pgflinewidth];
\end{tikzpicture}
\end{document}

答案3

外行人的解决方案(谁不喜欢间谍 ;)):

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[vertex/.style={draw, fill={#1}}]
  \node (a) [vertex=blue] {};
  \node (b) [vertex=blue, right=of a] {};
  \node (c) [vertex=green, right=of b] {};

  \draw (a) edge (b);

  \draw [red] (b) circle [radius=0.75];
  \fill[white,fill opacity=0.75](current bounding box.south west) rectangle 
                                (current bounding box.north east);
  \begin{scope}
    \clip (b) circle [radius=0.75];
    \node [vertex=blue, right=of a] {};
    \draw (a) edge (b);
    \draw [red] (b) circle [radius=0.75];
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案4

这看起来像是反向裁剪和用小于 1 的颜色填充的相同问题。opacity我从 Paul 那里偷来了很好的解决方案如何在 TikZ 中反转“剪辑”选择?。要使其执行比剪辑更多的操作,您需要重复使用路径,因此preaction

编辑:我自己也搞糊涂了。显然不需要剪辑(谢谢亨利)。只需填充就足够了。将其重命名为invselect。结果保持不变。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
    vertex/.style={draw, fill={#1}},
    invselect/.style={insert path={{[reset cm]
      (-16383.99999pt,-16383.99999pt) rectangle (16383.99999pt,16383.99999pt)
    }}
  },
  hilite/.style={fill=#1,opacity=0.9,overlay,invselect}
}

\begin{document}
\begin{frame}

\begin{tikzpicture}
  \fill[yellow] (-3,-2) rectangle (3,2);
  \node (a) [vertex=blue] {};
  \node (b) [vertex=blue, right=of a] {};
  \node (c) [vertex=green, right=of b] {};

  \draw (a) edge (b);

  \draw [red] (b) circle [radius=0.75];

  \path<2>[hilite=white] (b) circle (0.75) ;
  \path<3>[hilite=brown] (a) circle (1);
\end{tikzpicture}

\end{frame}
\end{document}

在此处输入图片描述

相关内容