Clip a rectangle by the size of another (small) rectangle

clippath-clippingtikz-pgf

I'm trying to make a hole in the 2nd rectangle by the dimension of the 3rd rectangle. It seems that \clip, as it specified in the manual, is clipping all the subsequent drawings. Is there an invert clip command? I'm new to tikz.

Basically, I want the 3rd rectangle to have a white background by removing a portion from the 2nd rectangle. Currently, I used \path[fill=white] to solve the problem.

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
%\usepackage{tkz-euclide}
\usetikzlibrary{calc,shapes,decorations,decorations.text, mindmap,shadings,patterns,matrix,arrows.meta,intersections,automata,backgrounds,patterns.meta}


\begin{document}
%\begin{figure}[H]
%\centering

\begin{tikzpicture}[%
  coord/.style = {help lines, color=gray!40, dashed,xstep=0.2cm,ystep=0.2cm},%
  2ndRect/.style = {magenta, pattern=crosshatch, pattern color=magenta},%
  3rdRect/.style = {magenta, pattern=crosshatch dots, pattern color=blue},%
  insideArrow/.style = {black,{latex[scale=2]}-{latex[scale=2]}},%
  simpleArrow/.style = {black,{-latex}},%
  every node/.append style = {font =\bfseries\footnotesize},%
  % scale=.75,
  transform shape
  ]
  %
  % Draw HelpLines just to work faster
  %
   \draw[coord] (-2,-2) grid (20,10);
   \draw[<->,ultra thin] (0,0)--(20,0) node[right]{$x$};
   \draw[<->,ultra thin] (0,0)--(0,5) node[above]{$y$};.
   \foreach \x in {0,0.5,...,19}
     \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
   \foreach \y in {0,0.5,...,3}
     \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};

  %
  % Draw Rectangles
  %
  \draw (2,0.2) rectangle (18,2.4);                       % 1st rectangle
  \draw[2ndRect] (4.5,0.4) rectangle (15.5,2.2);          % 2nd rectangle
  \path[fill=white](6.5,0.6) rectangle (13.5,2);          % workaround 4 clipping background
  \draw[3rdRect] (6.5,0.6) rectangle (13.5,2);            % 3rd rectangle
  %\clip (6.5,0.6) rectangle (13.5,2);          % clipping background
  \node [draw, fill=white] at (7.8,1.3) {S/R1};                              % label
  \node [draw, fill=white]  at (12.3,1.3) {S/R1};                             % label
  \draw[insideArrow] (8.5,1.3) -- (11.6,1.3);             % arrow btwen small rectangles
  \node at (1,1.3) {S4};        % label
  \draw[simpleArrow] (1.4,1.3) -- (2.8,1.3);             % arrow
  \node at (3.25,1.3) {S3};     % label
  \draw[simpleArrow] (3.6,1.3) -- (5.2,1.3);             % arrow
  \node at (5.5,1.3) {S2};      % label
  \draw[simpleArrow] (5.8,1.3) -- (6.4,1.3);             % arrow
  \node at (19,1.3) {R4};       % label
  \draw[simpleArrow] (13.6,1.3) -- (14.2,1.3);           % arrow
  \node at (16.75,1.3) {R3};    % label
  \draw[simpleArrow] (15,1.3) -- (16.4,1.3);             % arrow
  \node at (14.5,1.3) {R2};     % label
  \draw[simpleArrow] (17.2,1.3) -- (18.6,1.3);           % arrow
   
\end{tikzpicture}
%\label{fig:tikz}
%\caption{diagrama}
%\end{figure}
\end{document}

Best Answer

You can use the even odd rule to make holes. See section 15.5.2 in pgfmanual

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
%\usepackage{tkz-euclide}
\usetikzlibrary{calc,shapes,decorations,decorations.text, mindmap,shadings,patterns,matrix,arrows.meta,intersections,automata,backgrounds,patterns.meta}


\begin{document}
%\begin{figure}[H]
%\centering

\begin{tikzpicture}[%
  coord/.style = {help lines, color=gray!40, dashed,xstep=0.2cm,ystep=0.2cm},%
  2ndRect/.style = {magenta, pattern=crosshatch, pattern color=magenta},%
  3rdRect/.style = {magenta, pattern=crosshatch dots, pattern color=blue},%
  insideArrow/.style = {black,{latex[scale=2]}-{latex[scale=2]}},%
  simpleArrow/.style = {black,{-latex}},%
  every node/.append style = {font =\bfseries\footnotesize},%
  % scale=.75,
  transform shape
  ]
  %
  % Draw HelpLines just to work faster
  %
   \draw[coord] (-2,-2) grid (20,10);
   \draw[<->,ultra thin] (0,0)--(20,0) node[right]{$x$};
   \draw[<->,ultra thin] (0,0)--(0,5) node[above]{$y$};.
   \foreach \x in {0,0.5,...,19}
     \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
   \foreach \y in {0,0.5,...,3}
     \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};

  %
  % Draw Rectangles
  %
  \draw (2,0.2) rectangle (18,2.4);                       % 1st rectangle
  \draw[2ndRect, even odd rule] (4.5,0.4) rectangle (15.5,2.2) (6.5,0.6) rectangle (13.5,2);          % 2nd rectangle
%  \path[fill=white](6.5,0.6) rectangle (13.5,2);          % workaround 4 clipping background
  \draw[3rdRect, even odd rule] (6.5,0.6) rectangle (13.5,2) (7.3,1.05) rectangle node[midway, black]{S/R1} (8.3,1.55) (11.8,1.05) rectangle node[midway, black]{S/R1} (12.8,1.55);            % 3rd rectangle
  %\clip (6.5,0.6) rectangle (13.5,2);          % clipping background
%  \node [draw, opacity=0.5] at (7.8,1.3) {S/R1};                              % label
%  \node [draw, opacity=.5]  at (12.3,1.3) {S/R1};                             % label
  \draw[insideArrow] (8.5,1.3) -- (11.6,1.3);             % arrow btwen small rectangles
  \node at (1,1.3) {S4};        % label
  \draw[simpleArrow] (1.4,1.3) -- (2.8,1.3);             % arrow
  \node at (3.25,1.3) {S3};     % label
  \draw[simpleArrow] (3.6,1.3) -- (5.2,1.3);             % arrow
  \node at (5.5,1.3) {S2};      % label
  \draw[simpleArrow] (5.8,1.3) -- (6.4,1.3);             % arrow
  \node at (19,1.3) {R4};       % label
  \draw[simpleArrow] (13.6,1.3) -- (14.2,1.3);           % arrow
  \node at (16.75,1.3) {R3};    % label
  \draw[simpleArrow] (15,1.3) -- (16.4,1.3);             % arrow
  \node at (14.5,1.3) {R2};     % label
  \draw[simpleArrow] (17.2,1.3) -- (18.6,1.3);           % arrow
   
\end{tikzpicture}
%\label{fig:tikz}
%\caption{diagrama}
%\end{figure}
\end{document}

enter image description here

Related Question