2

I'm trying to employ the extremely cool inverted clipping from this question in combination with reusage of named paths with the use path key I stole from this question. However, it seems like my cargo-cult skills of stealing TikZ code from stackexchange are insufficient here, because in the MWE I constructed, the inversion of the clipping does not work as expected:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}

\tikzset{invclip/.style={clip,insert path={{[reset cm]
      (-16383.99999pt,-16383.99999pt) rectangle (16383.99999pt,16383.99999pt)
}}}}

\makeatletter
\tikzset{
  use path for main/.code={%
    \tikz@addmode{%
      \expandafter\pgfsyssoftpath@setcurrentpath\csname tikz@intersect@path@name@#1\endcsname
    }%
  },
  use path for actions/.code={%
    \expandafter\def\expandafter\tikz@preactions\expandafter{\tikz@preactions\expandafter\let\expandafter\tikz@actions@path\csname tikz@intersect@path@name@#1\endcsname}%
  },
  use path/.style={%
    use path for main=#1,
    use path for actions=#1,
  }
}
\makeatother

\begin{tikzpicture}[outer sep=0mm]
  \coordinate (A) at (-1,-1);
  \coordinate (B) at (1,-1);
  \coordinate (C) at (1,1);
  \coordinate (D) at (-1,1);
  \path[draw=black,name path=P] (A) -- (B) -- (C) -- (D) --cycle;
  \begin{scope}
    \begin{pgfinterruptboundingbox} % useful to avoid the rectangle in the bounding box
      \path[invclip] (A) -- (B) -- (C) -- (D); % works just fine
      %\path[invclip,use path=P]; % doesn't work at all
    \end{pgfinterruptboundingbox} 
  \fill[orange!50] (-2,-2) rectangle (2,2);
  \end{scope}
\end{tikzpicture} 
\end{document}

This works just fine:

this works

But this doesn't work:

this doesn't work

How can I use inverted clipping with use path?

  • In this example, of course, you could draw what you need much more easily. But I assume your real case is not so simple. – cfr Jul 19 '17 at 19:58
  • Yes, my actual case involves very complicated paths instead of rectangles, but is conceptually the same (an inner shape, an outer shape, and I want to fill just the area between the two). – carsten Jul 19 '17 at 20:40
  • Then why not just use the even odd rule? – cfr Jul 20 '17 at 0:29
  • 1
    There is an unofficial package spath that helps you connect two or more paths into one. In this case, you are connecting a huge rectangle with whatever complicated path you used to have. After all this is a matter of softpath operation. – Symbol 1 Jul 20 '17 at 12:48
  • 1
    @cfr According to Loop Space's comment, it is distinct but the same. (What am I talking about...) – Symbol 1 Jul 23 '17 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.