Tikz:将(适合的)节点转换为路径,用于剪切、填充

Tikz:将(适合的)节点转换为路径,用于剪切、填充

我想做类似的事情这里以便剪切区域外的文本。但我想使用给定节点(在我的情况下,使用 构造fit)来精确指定区域。问题是我找不到如何将节点边框转换为路径。有什么想法吗?

梅威瑟:

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{fit,shapes.misc}

\begin{document}
\begin{tikzpicture}
  [
    mybox/.style={fill=#1!30,draw=#1,rounded corners}
    ]
  \node[mybox=green] at (0,0) (a) {A};
  \node[mybox=blue] at (0,3) (b) {B};
  \node[mybox=orange] at (3,0) (c) {C};
  \node[mybox=pink] at (5,3) (d) {D};
  \draw[-latex] (a) -- (b);
  \draw[-latex] (b) -- (c);
  \draw[-latex] (c) -- (a);
  \draw[-latex] (c) -- (d);
  \node[fit=(a)(c), rounded rectangle, draw, inner sep=10pt] (fitnode) {};
  % How to remove 
  \fill[white,fill opacity=.75] [even odd rule]
  (current bounding box.south west) rectangle (current bounding box.north east)
  (fitnode.south west) rectangle (fitnode.north east);
\end{tikzpicture}
\end{document}

注意:我想要精确的边界拟合,而不仅仅是用矩形近似我的节点。

答案1

您可以使用spath3库来保存节点路径save spath=...,然后使用重复使用它restore spath=...

\documentclass[tikz,border=7pt]{standalone}
\usepackage{spath3}
\usetikzlibrary{fit,shapes.misc}

\begin{document}
  \begin{tikzpicture}[
      mybox/.style={fill=#1!30,draw=#1,rounded corners}
    ]
    \node[mybox=green] at (0,0) (a) {A};
    \node[mybox=blue] at (0,3) (b) {B};
    \node[mybox=orange] at (3,0) (c) {C};
    \node[mybox=pink] at (5,3) (d) {D};
    \draw[-latex] (a) -- (b);
    \draw[-latex] (b) -- (c);
    \draw[-latex] (c) -- (a);
    \draw[-latex] (c) -- (d);
    \node[fit=(a)(c), rounded rectangle, draw, inner sep=10pt,save spath=fit] {};
    \fill[white,fill opacity=.75,restore spath=fit]
      (current bounding box.north west) rectangle (current bounding box.south east);
  \end{tikzpicture}
\end{document}

在此处输入图片描述

笔记 :不便之处在于,拟合节点线有一半变暗了。

答案2

只是为了完整性,另一种解决方案是使用show path construction装饰代替,spath3以在全局宏中保存路径的高级表示\savedtikzpath

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{fit,shapes.misc}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{
  append tikz path/.style = {
    decoration={show path construction,
      moveto code={
        \xdef\savedtikzpath{\savedtikzpath (\tikzinputsegmentfirst)}
      },
      lineto code={
        \xdef\savedtikzpath{\savedtikzpath -- (\tikzinputsegmentlast)}
      },
      curveto code={
        \xdef\savedtikzpath{\savedtikzpath .. controls (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb) ..(\tikzinputsegmentlast)}
      },
      closepath code={
        \xdef\savedtikzpath{\savedtikzpath -- cycle}
      }
    },
    decorate
  },
  save tikz path/.style = {append tikz path},
  save tikz path/.prefix code={\xdef\savedtikzpath{}}
}

\begin{document}
  \begin{tikzpicture}[
      mybox/.style={fill=#1!30,draw=#1,rounded corners}
    ]
    \path (0,0) node[mybox=green](a) {A}
          (0,3) node[mybox=blue] (b) {B}
          (3,0) node[mybox=orange] (c) {C}
          (5,3) node[mybox=pink] (d) {D};
    \foreach \from/\to in {a/b,b/c,c/a,c/d}{
      \draw[-latex] (\from) -- (\to);
    }
    \node[fit=(a)(c), rounded rectangle, inner sep=10pt, save tikz path] {};
    \fill[white, fill opacity=.75] \savedtikzpath
      (current bounding box.north west) rectangle (current bounding box.south east);
    \draw \savedtikzpath;
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容