4

I'm trying to overlay a picture with a semiopaque rectangle, then cut out some shapes (rectangles and circles) so the image is clear in those, then point to the cutouts with something like tikz \pin command. My problem is that the only examples i could find for cutouts use \fill and then simply pile on the cutout shapes. Is there a way to create those cutouts directly from nodes so i can later reference those nodes? Currently i am simply defining another node with the same coordinates later, to refer to that, but it would be a lot cleaner if it was possible to use the nodes for cutting directly. MWE (without nodes, from an example in the answer by Alex Recuenco in this Q: How to create a rectangle with a transparent hole):

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

\section{Non Zero Rule}

\begin{tikzpicture}
    \draw[black, fill = black, fill opacity = 0.5, semithick] 
        (0,0) rectangle (5,5) (2.5,2.5) circle (0.5);
\end{tikzpicture} 

\section{Even Odd Rule}

\begin{tikzpicture}
    % \node (image) {\includegraphics{somepic_with_cool_features}}
      \draw[black, fill = black, fill opacity = 0.5, semithick, even odd rule]
            (0,0) rectangle (5,5) (2.5,2.5) circle (0.5);
\end{tikzpicture}
\end{document}

To clarify: My dream would be to have something like

   % \node (image) {\includegraphics{somepic_with_cool_features}}
     \draw[black, fill = black, fill opacity = 0.5, semithick, even odd rule]
            (0,0) rectangle (5,5) {%
            \node[pin={[red]60:Cool feature one!}] (circ1) (2.5,2.5) circle (0.5);
            \node[pin={[red]60:Wow another one!}] (circ2) (4,4) circle (0.5);
            \node[pin={[red]60:A whole bunch!}] (rect1) (5,6) rectangle (7,9);%
            }

1 Answer 1

4

You can use the node boundary path for a reverse clip to protect it from being overpainted. This is very much like the eraser tool.

\documentclass{article}
\usepackage{tikz}
% based on 
% https://tex.stackexchange.com/a/38995/121799 
% https://tex.stackexchange.com/a/76216 
% https://tex.stackexchange.com/a/59168/194703 
% https://tex.stackexchange.com/q/448920/194703 
\makeatletter
\tikzset{
    reuse path/.code={\pgfsyssoftpath@setcurrentpath{#1}}
}
\makeatother
\tikzset{even odd clip/.code={\pgfseteorule},
    protect/.code={
        \clip[overlay,even odd clip,reuse path=#1]
        (-5383.99999pt,-5383.99999pt) rectangle (5383.99999pt,5383.99999pt);
}}
\begin{document}


\section{Protect}

\begin{tikzpicture}
 \path (2.5,2.5) node[draw,circle,save path=\pathA,minimum size=1cm](c){}; 
 \tikzset{protect=\pathA}
 \draw[overlay=false,black, fill = black, fill opacity = 0.5, semithick]
            (0,0) rectangle (5,5);
 \draw[stealth-] (c) -- ++ (60:1) node[above]{here};            
\end{tikzpicture}
\end{document}

enter image description here

4
  • can you elucidate some of what you are doing? * what's with the ultra-precision coordinates? (in an answer by Symbol 1 to the eraser-related Q tex.stackexchange.com/questions/12010/…, something like [reset cm](-\maxdimen,-\maxdimen)rectangle(\maxdimen,\maxdimen) is used - is that related? * You seem to be doing "draw node, then draw eraser-node at same coords" - that is not much less complicated than my " draw eo-cutout, then draw fakenode at same coords" - i was looking for a way to do "draw rect, now cutout node"
    – bukwyrm
    Apr 22, 2020 at 6:36
  • 1
    @bukwyrm The point of this is that you can place many of these protected nodes and add many fills and they will still not be overwritten, and have the usual anchors. Of course, if you only have a single shape and a single fill, your method is shorter. This was under the assumption that "then cut out some shapes (rectangles and circles) so the image is clear in those," means several nodes. And yes, that answer is based on many other posts which I tried to collect in the links. Basically one does an even odd clip with the path of the node and an insanely large rectangle.
    – user194703
    Apr 22, 2020 at 6:44
  • ah! it is not about the precision (.999999) but about the size of the value? You are absolutely correct about the scalability of your method vs mine, i just wanted to point out that my dream-answer would be more succinct (in my heart of hearts i had been hoping that i had simply misread the tikz manual and was missing some parantheses or something)...
    – bukwyrm
    Apr 22, 2020 at 6:49
  • 1
    @bukwyrm The whole thing is only useful if you have a nontrivial background as in this answer. If you just want to have a filled node, you can add fill=white, say, and do not need any of this. However, when you have a nontrivial background of which you wish to "protect" some regions, and if these regions should have node anchors and a node boundary, these tools may be worthwhile.
    – user194703
    Apr 22, 2020 at 6:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.