How can I invert a 'clip' selection within TikZ?

What you can do is add a rectangle to your clipping path that's larger than the current bounding box, and clip with that. Andrew Stacey suggested using the current page as the clipping rectangle, because that will catch all elements that follow. By using the pgfinterruptboundingbox environment when defining the clipping rectangle, the actual size of the tikzpicture will not be influenced.

Note that, in order to use the current page, the remember picture,overlay options need to be passed to the tikzpicture, and you need two compile runs to get the positioning of all the elements right. Furthermore, this doesn't work with the minimal documentclass.

\documentclass{article} % Has to be a proper class, not minimal
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[remember picture,overlay]

% A path that follows the edges of the current page
\tikzstyle{reverseclip}=[insert path={(current page.north east) --
  (current page.south east) --
  (current page.south west) --
  (current page.north west) --
  (current page.north east)}
]

\coordinate (A) at (0,0);
\coordinate (B) at (1,0);
\coordinate (C) at (1,1);

\begin{pgfinterruptboundingbox} % To make sure our clipping path does not mess up the placement of the picture
\path [clip] (A) -- (B) -- (C) -- cycle [reverseclip];
\end{pgfinterruptboundingbox}

\draw[thick] (A) circle (2mm);
\draw[thick] (B) circle (2mm);    
\draw[thick] (C) circle (2mm);   

\end{tikzpicture}
\end{document}


And just to show that it works for the general case:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[remember picture,overlay]

% A path that follows the edges of the current page
\tikzstyle{reverseclip}=[insert path={(current page.north east) --
  (current page.south east) --
  (current page.south west) --
  (current page.north west) --
  (current page.north east)}
]

\draw [step=0.1,red] (0,0) grid  (2,2);

\begin{pgfinterruptboundingbox} % To make sure our clipping path does not mess up the placement of the picture
\path [clip,rounded corners] (0,0) -- (.75,0) -- (1.2,.8) -- (2,1) -- (1.4,1) -- (1.2,2) -- (.3,.75) -- cycle [reverseclip];
\end{pgfinterruptboundingbox}

\draw [step=0.1,thick] (0,0) grid  (2,2);

\end{tikzpicture}
\end{document}

general path clipped


To avoid remember picture and overlay, I mix Jack's solution and Altermundus's solution using the bigger rectangle that TikZ/PGF (TeX?) can used (Edit: as suggested by Qrrbrbirlbel, I add [reset cm] to get a solution independent from any scale transformations).

First tikzpicture shows two (inv)clipping triangles.

Second tikzpicture shows the effect of nonzero rule (even odd rule can't be used directly in a clipping path, see note below).

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

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

\begin{tikzpicture}[outer sep=0mm]
  \coordinate (A) at (0,0);
  \coordinate (B) at (1,0);
  \coordinate (C) at (.5,1);
  \coordinate (Ap) at (0,1);
  \coordinate (Bp) at (1,1);
  \coordinate (Cp) at (.5,0);
  \begin{scope}
  \begin{pgfinterruptboundingbox} % useful to avoid the rectangle in the bounding box
    \path[invclip]
    (A) -- (B) -- (C) -- (A)
    (Ap) -- (Cp) -- (Bp) -- (Ap);
  \end{pgfinterruptboundingbox} 

  \fill[orange!50] (-1,-1) rectangle (2,2);

  \draw (A) circle (2mm);    % this is the little angle marker
  \draw (B) circle (2mm);    % this is the little angle marker
  \draw (C) circle (2mm);    % this is the little angle marker

  \draw (Ap) circle (2mm);    % this is the little angle marker
  \draw (Bp) circle (2mm);    % this is the little angle marker
  \draw (Cp) circle (2mm);    % this is the little angle marker

  \end{scope}

  \draw[red] (current bounding box.south west)
  rectangle (current bounding box.north east);
\end{tikzpicture}

\begin{tikzpicture}[outer sep=0mm]
  \coordinate (A) at (0,0);
  \coordinate (B) at (1,0);
  \coordinate (C) at (.5,1);
  \coordinate (Ap) at (0,1);
  \coordinate (Bp) at (1,1);
  \coordinate (Cp) at (.5,0);
  \begin{scope}
  \begin{pgfinterruptboundingbox} % useful to avoid the rectangle in the bounding box
    \path[invclip]
    (A) -- (B) -- (C) -- (A)
    (Ap) -- (Bp) -- (Cp) -- (Ap);
  \end{pgfinterruptboundingbox} 

  \fill[orange!50] (-1,-1) rectangle (2,2);

  \draw (A) circle (2mm);    % this is the little angle marker
  \draw (B) circle (2mm);    % this is the little angle marker
  \draw (C) circle (2mm);    % this is the little angle marker

  \draw (Ap) circle (2mm);    % this is the little angle marker
  \draw (Bp) circle (2mm);    % this is the little angle marker
  \draw (Cp) circle (2mm);    % this is the little angle marker

  \end{scope}

  \draw[red] (current bounding box.south west)
  rectangle (current bounding box.north east);
\end{tikzpicture}
\end{document}

enter image description here

Note about rules and clip:

It's impossible to combine clip and even odd rule in a path (it seems to me that it's almost a bug). But, if you add the even odd rule option to the enclosing scope, the clip operation uses it. Applied on the previous example, the clipping paths can use any direction of rotation:

\documentclass{standalone}
\usepackage{tikz}
\tikzset{invclip/.style={clip,insert path={{[reset cm]
        (-16383.99999pt,-16383.99999pt) rectangle (16383.99999pt,16383.99999pt)}}}}
\begin{document}
\begin{tikzpicture}[outer sep=0mm]


  \coordinate (A) at (0,0);
  \coordinate (B) at (1,0);
  \coordinate (C) at (.5,1);
  \coordinate (Ap) at (0,1);
  \coordinate (Bp) at (1,1);
  \coordinate (Cp) at (.5,0);
  \begin{scope}[even odd rule]

    \begin{pgfinterruptboundingbox} % useful to avoid the rectangle in the bounding box
      \path[invclip]
      (A) -- (B) -- (C) -- (A)
      (Ap) -- (Bp) -- (Cp) -- (Ap);
    \end{pgfinterruptboundingbox} 

    \fill[orange!50] (-1,-1) rectangle (2,2);

    \draw (A) circle (2mm);    % this is the little angle marker
    \draw (B) circle (2mm);    % this is the little angle marker
    \draw (C) circle (2mm);    % this is the little angle marker

    \draw (Ap) circle (2mm);    % this is the little angle marker
    \draw (Bp) circle (2mm);    % this is the little angle marker
    \draw (Cp) circle (2mm);    % this is the little angle marker

  \end{scope}

  \draw[red] (current bounding box.south west)
  rectangle (current bounding box.north east);
\end{tikzpicture}

@Jack send me here from a question regarding invclip: Reverse clipping in a 3D figure - Projection issue.

That question shows a problem of @PaulGaborit's answer that double braces in

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

does not limit the scope of reset cm. In fact, it took four in the case in the link above. Therefore, in the link above, I suggested the following code

\tikzset{
    clip even odd rule/.code={\pgfseteorule}, % Credit to Andrew Stacey 
    invclip/.style={
        clip,insert path=
            [clip even odd rule]{
                [reset cm](-\maxdimen,-\maxdimen)rectangle(\maxdimen,\maxdimen)
            }
    }
}

A complete example goes like this

\documentclass[border=9,tikz]{standalone}
\usetikzlibrary{graphs,graphs.standard}
\begin{document}

\tikzset{
    clip even odd rule/.code={\pgfseteorule}, % Credit to Andrew Stacey 
    invclip/.style={
        clip,insert path=
            [clip even odd rule]{
                [reset cm](-\maxdimen,-\maxdimen)rectangle(\maxdimen,\maxdimen)
            }
    }
}

\tikz{
    \begin{pgfinterruptboundingbox}
        \clip[invclip](1,0)--(0,1)--(0,-1)--(-1,0)--cycle;
    \end{pgfinterruptboundingbox}
    \graph{subgraph K_n[n=20,clockwise,radius=50]};
}

\end{document}

Tags:

Tikz Pgf