# How to clip overlaying surfaces/paths instead of filling them with white?

The following code

\newcommand*{\arcthickness}{0.3}
% node is positioned by splitting the arc in two parts
% see http://tex.stackexchange.com/a/76369/6255
\draw (#1,#2) arc (180:90:#3)
node[below=-1.5pt] {\tiny #4}
arc (90:0:#3)
-- ++(-\arcthickness,0) arc (0:180:#3-\arcthickness) -- cycle;
}
\begin{tikzpicture}
\myarc{0}{0}{1}{A}
\myarc{3}{0}{1}{C}
\myarc{1}{0}{1.5}{B}
\end{tikzpicture}


produces

, but what I'd really like to have is:

where the intersected parts are not white but should just be left out (clipped). The reason is that the figure should work on any background color. And, of course, ideally it should work in a generic way where a further application of \myarc overlays/clips the previous ones.

-
You need to clip the lower arcs against the higher ones, which suggests drawing them in reverse order: each arc draws itself and then defines a reverse clip against itself for all future drawings. Would that suit? Reverse clipping can be found at tex.stackexchange.com/q/12010/86 –  Andrew Stacey Oct 12 '12 at 10:41
@AndrewStacey Wow, seems to work, I'll post a solution, but this can be marked as duplicate. –  neo Oct 12 '12 at 10:53
@AndrewStacey I made a community wiki answer, there are still some issues though, maybe I haven't understood the reverse clipping completely yet. –  neo Oct 12 '12 at 11:11
I've modified your answer hopefully correcting the issues you found. The clipping one is subtle! –  Andrew Stacey Oct 12 '12 at 13:10
@AndrewStacey Thanks for that, looks a lot cleaner now! While refactoring I discovered a really strange problem, see my edit at the bottom –  neo Oct 12 '12 at 13:24

As suggested by Andrew, reverse clipping can be used:

\documentclass{article}
%\url{http://tex.stackexchange.com/q/76409/86}
\usepackage{tikz}

\tikzset{
reverseclip/.style={
clip even odd rule,
insert path={(current page.north east) --
(current page.south east) --
(current page.south west) --
(current page.north west) --
(current page.north east)}
},
clip even odd rule/.code={\pgfseteorule}
}

\newcommand*{\reverseclip}[1]{
\begin{pgfinterruptboundingbox}
\clip[reverseclip] \againpath #1;
\end{pgfinterruptboundingbox}
}

\newcommand*{\arcthickness}{0.3}
% node is positioned by splitting the arc in two parts
% (workaround until new pgf is released)
% see http://tex.stackexchange.com/a/76369/6255
\draw[save path=\arcclippath]
(#1,#2) arc (180:90:#3)
node[below=-1.5pt] {\tiny #4}
arc (90:0:#3)
-- ++(-\arcthickness,0) arc (0:180:#3-\arcthickness) -- cycle;
\reverseclip{\arcclippath}
}

\begin{document}

\begin{tikzpicture}[remember picture]

\myarc{1}{0}{1.5}{B}
\myarc{0}{0}{1}{A}
\myarc{3}{0}{1}{C}

\end{tikzpicture}
\end{document}


Notes:

1. The remember picture key on the main picture is needed to ensure that the current page pseudo-node works as it should. Without that, it is incorrectly positioned and so the clip region is not quite what is expected. A good way to debug clipping is to change \clip to \fill and set fill opacity=.5. Without the remember picture then the rectangle blanked by the current page is offset to the position of the current picture rather than of the page. However, overlay is not needed as the extra large pieces of the picture are not used when computing its bounding box.

2. To avoid path duplication, we use the ability of TikZ to save a path for later use. This is the save path key. To restore it, we use a command called \againpath.

3. To get the clips to obey the even odd rule we have to use a bit of subterfuge. We aren't allowed to put the even odd rule on the \clip path itself (as this triggers the dread warning from TikZ) and as we want the clip to continue in effect we cannot put it inside a scope. So we have to duplicated the effect of the even odd rule directly. This is the key clip even odd rule which is (now) invoked as part of the reverseclip style (since it probably should always be set for this type of clipping.

-