# Help with clip function in TikZ

I would like to extend the gray area to x=4.5 with the semi-circles not colored. The problem is how to clip the correct arcs. I tried to use a "for all" command and define multiple arcs but wont work. This is my code:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\pgfmathsetmacro{\myxlow}{-2}
\pgfmathsetmacro{\myxhigh}{6}
\pgfmathsetmacro{\myiterations}{12}

\begin{tikzpicture}[scale=2]
\draw[-latex](\myxlow-0.1,0) -- (\myxhigh+0.2,0);
\pgfmathsetmacro{\succofmyxlow}{\myxlow+0.5}

\begin{scope}
\clip (\myxlow,0) rectangle (\myxhigh,1.1);
\foreach \i in {1,...,\myiterations}
{   \pgfmathsetmacro{\mysecondelement}{\myxlow+1/pow(2,floor(\i/3))}
\foreach \x in {-2,\mysecondelement,...,6}
{   \draw[very thin, blue] (\x,0) arc(0:180:\myradius);
}
}
\end{scope}

\begin{scope}
\begin{pgfonlayer}{background}
\clip (-0.5,0) rectangle (4.5,3.0);
\clip   (1,2.5) -| (-1,0) arc (180:0:1) -- cycle;
%  \clip   (3,2.5) -| (1,0) arc (180:0:1) -- cycle;
\fill[gray,opacity=0.8] (-1,-1) rectangle (4,3);
\end{pgfonlayer}
\end{scope}
\end{tikzpicture}

\end{document}


And this is the picture:

Update according to comment, which just need to remove one of the clipping paths (the red one):

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}
\pgfmathsetmacro{\myxlow}{-2}
\pgfmathsetmacro{\myxhigh}{6}
\pgfmathsetmacro{\myiterations}{12}

\begin{tikzpicture}[scale=2]
\draw[-latex](\myxlow-0.1,0) -- (\myxhigh+0.2,0);
\pgfmathsetmacro{\succofmyxlow}{\myxlow+0.5}

\begin{scope}
\clip (\myxlow,0) rectangle (\myxhigh,1.1);
\foreach \i in {1,...,\myiterations}
{   \pgfmathsetmacro{\mysecondelement}{\myxlow+1/pow(2,floor(\i/3))}
\foreach \x in {-2,\mysecondelement,...,6}
{   \draw[very thin, blue] (\x,0) arc(0:180:\myradius);
}
}
\end{scope}

\begin{scope}
\begin{pgfonlayer}{background}
\clip (-0.5,0) rectangle (4.5,3.0);
\pgfmathsetmacro{\mysecondelement}{\myxlow + 3}
\clip
(-1, 3)
\foreach \x in {-1,\mysecondelement,...,6}
-- (4.5, 3) -- cycle
;
\fill[gray,opacity=0.8] (-1,-1) rectangle (4.5,3);
\end{pgfonlayer}
\end{scope}
\end{tikzpicture}
\end{document}


## Original version

The following example runs the loop for the half circles twice as clip paths. Since only the largest half circles are of interest, \i is 1 in the clip paths. That simplifies the formulas. The first clip path loop starts with the half circles at -2, the second clip path at -1. Increment is 2.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\pgfmathsetmacro{\myxlow}{-2}
\pgfmathsetmacro{\myxhigh}{6}
\pgfmathsetmacro{\myiterations}{12}

\begin{tikzpicture}[scale=2]
\draw[-latex](\myxlow-0.1,0) -- (\myxhigh+0.2,0);
\pgfmathsetmacro{\succofmyxlow}{\myxlow+0.5}

\begin{scope}
\clip (\myxlow,0) rectangle (\myxhigh,1.1);
\foreach \i in {1,...,\myiterations}
{   \pgfmathsetmacro{\mysecondelement}{\myxlow+1/pow(2,floor(\i/3))}
\foreach \x in {-2,\mysecondelement,...,6}
{   \draw[very thin, blue] (\x,0) arc(0:180:\myradius);
}
}
\end{scope}

\begin{scope}
\begin{pgfonlayer}{background}
\clip (-0.5,0) rectangle (4.5,3.0);
\pgfmathsetmacro{\mysecondelement}{\myxlow + 2}
\clip
(-1, 3)
\foreach \x in {-2,\mysecondelement,...,6}
-- (4.5, 3) -- cycle
;
\pgfmathsetmacro{\mysecondelement}{\myxlow + 3}
\clip
(-1, 3)
\foreach \x in {-1,\mysecondelement,...,6}
-- (4.5, 3) -- cycle
;
\fill[gray,opacity=0.8] (-1,-1) rectangle (4.5,3);
\end{pgfonlayer}
\end{scope}
\end{tikzpicture}

\end{document}


Visualization of the clipping paths:

\draw[cyan, line width=2mm]  % instead of \clip for the rectangle
\draw[red, line width=2mm]   % instead of \çlip
\draw[green, line width=2mm] % instead of \clip
opacity=0.4 % instead of opacity=0.8 for he gray rectangle


• Another very good answer. +1. – Sebastiano Apr 11 '18 at 22:30
• Ok, thanks, this is nice but I should be more specific in my description and I apologise for it. I want the dark region to cover also the "cones" between circles. This is what we get in fundamental groups of congruence subgroups of the SL(2,Z) group. So the "cone" between 0.5 and 1.5 should also be gray (in my picture I achieved half of it only). Same for the cone (1,5,1) to (2,5,1) etc – Marion Apr 11 '18 at 22:30

I think you almost got it in your question.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\pgfmathsetmacro{\myxlow}{-2}
\pgfmathsetmacro{\myxhigh}{6}
\pgfmathsetmacro{\myiterations}{12}

\begin{tikzpicture}[scale=2]
\draw[-latex](\myxlow-0.1,0) -- (\myxhigh+0.2,0);
\pgfmathsetmacro{\succofmyxlow}{\myxlow+0.5}

\begin{scope}
\clip (\myxlow,0) rectangle (\myxhigh,1.1);
\foreach \i in {1,...,\myiterations}
{   \pgfmathsetmacro{\mysecondelement}{\myxlow+1/pow(2,floor(\i/3))}
\foreach \x in {-2,\mysecondelement,...,6}
{   \draw[very thin, blue] (\x,0) arc(0:180:\myradius);
}
}
\end{scope}

\begin{scope}
\begin{pgfonlayer}{background}
\clip (-0.5,0) -- (-0.5,3) -- (4.5,3) -- (4.5,0)
-- (5,0) arc(0:180:1) -- (3,0) arc(0:180:1)  -- (1,0) arc(0:180:1)
--cycle;
\fill[gray,opacity=0.8]
(-0.5,0) -- (-0.5,3) -- (4.5,3) -- (4.5,0) -- cycle;
\end{pgfonlayer}
\end{scope}
\node at (2,2.5) {fundamental domain};
\end{tikzpicture}

\end{document}


UPDATE: Added something above the circles.

• Thanks a lot, yeah, I did not know how to make those clips :s – Marion Apr 11 '18 at 23:14
• I accepted your answer! sorry, i forgot it earlier! – Marion Apr 11 '18 at 23:54
• I try to create a node somewhere in the gray area (to write an equation) but I fail. Every node, no matter where I put it, goes to the x axis below. Why is that? – Marion Apr 13 '18 at 23:00
• @Marion I added a counter example. Of course you can write equations. – user121799 Apr 13 '18 at 23:04
• I was using the command: \node[] (2,1) {x+1}; and would not work as this! – Marion Apr 13 '18 at 23:06