Taking the idea to save the path to the aux file so that it is available at the next run.
I didn't test it much but for this example it works.
I might have experience with using the aux file but I'm no expert. Externalizing the picture with the external library won't work.
On any path of TikZ you can use the
remember path = <name> key so that it will be available at the next compilation and the
remembered path = <name> key that uses this path (if it is available).
The key will use \pgfpictureid as a prefix so that you can specify the same path name in different pictures.
I've also defined a reverse clip key that supports both a clockwise as well as a counter clockwise rectangle with maximum supported size. This is done on the PGF layer and with the quick commands for three reasons:
- they are quicker,
- they ignore transtormations and
- they don't update the bounding box (though, your
\path[remember path=…] might use overlay anyway.
This is how you use it:
\begin{tikzpicture}
\fill[left color=blue, right color=green] (0,0) rectangle (2,1);
\clip[remembered path=box];
\draw (0,0) -- (2,1);
\path[remember path=box] (0.5, 0) rectangle (1.5, 1) [reverse clip];
\end{tikzpicture}
Code
\documentclass[border=5pt, tikz, convert]{standalone}
% Part 1: Quick PGF versions of reverse clips.
%% * uses quick commands that
%% * ignore transformations and
%% * don't contribute to the bounding box (we use overlay though anyway)
%% * only uses 16000pt since this is what PGF does, too (TeX'd allow 16384pt)
%% * clockwise and counterclockwise for nonzero and even odd rule
%%
\tikzset{% quick versions of reverse clips
reverse clip/.is choice,
reverse clip/clockwise/.code={%
\pgfpathqmoveto{16000pt}{16000pt}%
\pgfpathqlineto{16000pt}{-16000pt}%
\pgfpathqlineto{-16000pt}{-16000pt}%
\pgfpathqlineto{-16000pt}{16000pt}%
\pgfpathclose},
reverse clip/counter clockwise/.code={%
\pgfpathqmoveto{16000pt}{16000pt}%
\pgfpathqlineto{-16000pt}{16000pt}%
\pgfpathqlineto{-16000pt}{-16000pt}%
\pgfpathqlineto{16000pt}{-16000pt}%
\pgfpathclose},
reverse clip/.default=counter clockwise}
% Part 2: The actual keys
% that save/use the path to/from the aux file.
%% * remember path will does what ever you do on the path
%% and save it to the aux file
%% * remembered path will set the remembers path (if available)
%% and use it as specified
%%
\makeatletter
\tikzset{
remember path/.code={%
\tikz@addmode{%
\expandafter\pgfsyssoftpath@getcurrentpath\csname qrr@tikzpath@\pgfpictureid @#1\endcsname
\immediate\write\pgfutil@auxout{%
\noexpand\expandafter\gdef\noexpand\csname qrr@tikzpath@\pgfpictureid @#1\endcsname{%
\expandafter\expandafter\expandafter\unexpanded
\expandafter\expandafter\expandafter{\csname qrr@tikzpath@\pgfpictureid @#1\endcsname}}}}%
},
remembered path/.code={%
\pgfutil@IfUndefined{qrr@tikzpath@\pgfpictureid @#1}{}{%
\tikz@addmode{%
\expandafter\pgfsyssoftpath@setcurrentpath
\expandafter{\csname qrr@tikzpath@\pgfpictureid @#1\endcsname}}}}}
\makeatother
\begin{document}
\begin{tikzpicture}
\fill[left color=blue, right color=green] (0,0) rectangle (2,1);
\clip[remembered path=box];
\draw (0,0) -- (2,1);
\path[remember path=box] (0.5, 0) rectangle (1.5, 1) [reverse clip];
\end{tikzpicture}
\end{document}
Output
First compilation

Second compilation

transparency groupof typeknockoutbut not many PDF viewers do (neither does the unofficial online version of the manual I've linked to). You could also save the needed clip when you know it, write it to theauxfile and then apply it on the second run. You could also place an empty rectangular box along a path with themark connection nodedecoration where PGF/TikZ automatically draws to seperate lines. What's your actual usecase? – Qrrbrbirlbel Dec 03 '22 at 16:32