# Shading the middle section in 4 circles

I currently have a diagram involving four circles and a square connecting all four centers. However, I'm trying to figure out how to shade the inner area of the four circles. My current code looks like this, and is simply the diagram without the central shaded area.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}

% Positions of the centers of the circles

% Draw the circles

% Shade the central overlap of the four circles
\begin{scope}
\end{scope}

% Draw lines connecting W to X to Z to Y
\draw (W) -- (X) -- (Z) -- (Y) -- cycle;

% Label the centers in math mode, positioned closer to the box
\node at ($(W) + (-0.2,0.2)$) {$$W$$};
\node at ($(X) + (0.2,0.2)$) {$$X$$};
\node at ($(Y) + (-0.2,-0.2)$) {$$Y$$};
\node at ($(Z) + (0.2,-0.2)$) {$$Z$$};

\end{tikzpicture}
\end{document}


• Welcome to TeX.SE! Would you mind completing your code with a \begin{document} and everything? It really helps answerers. Your problem can be solved by enclosing the area with a path and then using fill. Your code is very clear and well documented by the way. Commented Sep 3 at 23:11
• \fill (X) -- ++(-\radius,0pt) arc (180:270:\radius) -- cycle; for the X one, for example.
– cfr
Commented Sep 4 at 3:33
• Hi! Thanks for letting me know and complimenting my code! I've updated the code including the packages. Commented Sep 4 at 22:33
• thanks for editing! which bit are you trying to shade? the bit of each circle inside the square? or the bit of the square not in any circle? (but note your code could not be compiled as-was.)
– cfr
Commented Sep 4 at 23:53
• your code says you are trying to shade the overlap of the four circles, but there is no overlap. so ...?
– cfr
Commented Sep 5 at 0:01

The first is a very slight modification of the code you posted, which shades the parts of the circles within the square.

The second is a more significant adaption which shades the part of the square not in any circle. This example uses a couple of tricks to make the code more concise e.g. it uses a loop to set the coordinates and create the nodes. It uses a combination of clipping and the even odd rule for filling.

\begin{tikzpicture}


Given the radius, we can calculate the distance from the origin of each coordinate quite easily.

  \newdimen\cdist


This enables us to use polar coordinates to place W, X, Y and Z, which makes it easy to do it in a loop without having to worry about combinations of negative and positive horizontal/vertical displacement. We also use the updated syntax for circle1.

  \foreach \i/\j in {W/135,X/45,Y/-135,Z/-45} \draw (\j:\cdist) coordinate (\i) circle [radius=\radius] (\j:{0.2cm+\cdist}) node {$\i$};


The last part adds the node at the same time, using an adjusted polar coordinate. We then start our scope

  \begin{scope}


clipping to the square

    \clip (X) |- (Y) |- cycle;


and using the even odd rule to fill the square minus the circles. (If we did not use the clipping above, the parts of the circles outside the square would also be filled, but the parts inside the square would not.)

    \path [fill=gray,opacity=0.5,postaction=draw,even odd rule] (W) -- (X) -- (Z) -- (Y) -- cycle  (W) circle (\radius) (X) circle (\radius) (Y) circle (\radius) (Z) circle (\radius)  ;
\end{scope}

\end{tikzpicture}


Complete code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}

% Positions of the centers of the circles

% Draw the circles

% Shade the central overlap of the four circles
\begin{scope}
\draw [fill=gray,opacity=0.5,postaction=draw] (W) -- (X) -- (Z) -- (Y) -- cycle;
\end{scope}

% Draw lines connecting W to X to Z to Y
% \draw (W) -- (X) -- (Z) -- (Y) -- cycle;

% Label the centers in math mode, positioned closer to the box
\node at ($(W) + (-0.2,0.2)$) {$$W$$};
\node at ($(X) + (0.2,0.2)$) {$$X$$};
\node at ($(Y) + (-0.2,-0.2)$) {$$Y$$};
\node at ($(Z) + (0.2,-0.2)$) {$$Z$$};

\end{tikzpicture}

\begin{tikzpicture}
\newdimen\cdist
\foreach \i/\j in {W/135,X/45,Y/-135,Z/-45} \draw (\j:\cdist) coordinate (\i) circle [radius=\radius] (\j:{0.2cm+\cdist}) node {$\i$};

\begin{scope}
\clip (X) |- (Y) |- cycle;
\path [fill=gray,opacity=0.5,postaction=draw,even odd rule] (W) -- (X) -- (Z) -- (Y) -- cycle  (W) circle (\radius) (X) circle (\radius) (Y) circle (\radius) (Z) circle (\radius)  ;
\end{scope}

\end{tikzpicture}
\end{document}


1Because Somebody will tell me off otherwise.

• Thank you so much! Really really appreciate this Commented Sep 5 at 3:50
• Thank you also for taking the time to clarify parts of my code that were incorrect for this diagram, and teaching me through your thought process in what you've designed here. Commented Sep 5 at 3:51

Here is two ways to achieved this.

If you draw the square first, then the circles will hide the unwanted part.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}

% Positions of the centers of the circles

% fill the square
\fill[gray,opacity=0.5] (W) -- (X) -- (Z) -- (Y) -- cycle;

% Draw the circles and hide the unwanted part

% Draw lines connecting W to X to Z to Y
\draw (W) -- (X) -- (Z) -- (Y) -- cycle;

% Label the centers in math mode, positioned closer to the box
\node at ($(W) + (-0.2,0.2)$) {$$W$$};
\node at ($(X) + (0.2,0.2)$) {$$X$$};
\node at ($(Y) + (-0.2,-0.2)$) {$$Y$$};
\node at ($(Z) + (0.2,-0.2)$) {$$Z$$};

\end{tikzpicture}
\end{document}


Second, with the arc command, you can run along the circles to create the path you need. I started it at the mid point between W and X, then run along in a counter clockwise motion.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}

% Positions of the centers of the circles

% Draw the circles and hide the unwanted part

% fill the center part
\fill[gray,opacity=0.5] ($0.5*(W)+0.5*(X)$) arc(0:-90:\radius) arc(90:0:\radius) arc(180:90:\radius) arc(270:180:\radius) -- cycle;

% Draw lines connecting W to X to Z to Y
\draw (W) -- (X) -- (Z) -- (Y) -- cycle;

% Label the centers in math mode, positioned closer to the box
\node at ($(W) + (-0.2,0.2)$) {$$W$$};
\node at ($(X) + (0.2,0.2)$) {$$X$$};
\node at ($(Y) + (-0.2,-0.2)$) {$$Y$$};
\node at ($(Z) + (0.2,-0.2)$) {$$Z$$};

\end{tikzpicture}
\end{document}

• Oh my goodness thank you so much for this! I've been scratching my head trying to figure out the most efficient method. Commented Sep 5 at 0:26
• Thank you Alain Commented Sep 5 at 21:40