2

How can I adjust this venn Diagram so it becomes like what I wrote on the Image below? enter image description here

I need to Color the "M" rectangle on the third Diagram and to remove the intersection between A and B on the fourth one.

I am using the following codes:

  \pgfkeys{not inside/.code={\clip[use path=#1,reverseclip];},
    inside/.code={\clip[use path=#1];},
    shade/.code=\fill[#1] (current bounding box.south west)rectangle 
    (current bounding box.north east);}
    \begin{tikzpicture}
    \draw[thick,fill=white] (-1,0) node{$A$} circle [radius=1.5cm]
    (1,0) node[above]{$M$} (0,-1.5) node[below]{$A^c$};
    \draw[frame=5pt];
    \end{tikzpicture}
    \begin{tikzpicture}
    \draw[thick,save path=\pathA,fill=gray] (-1,0) node{$A$} circle [radius=1.5cm];
    \draw[thick,save path=\pathB] (1,0) node{$B$} circle[radius=1.5cm];
    \path (0,1.5) node[above]{$M$}
    (0,-1.5) node[below]{$A - B$};
    \begin{scope}[on background layer]
    \pgfkeys{inside/.list={\pathB},shade=white}
    \end{scope}
    \draw[frame=5pt];
    \end{tikzpicture}

And things are ending up this way:

t

I am sorry if I'm lacking any information, I am very new on LaTeX and I have been trying to do this for quite a while now.

Anyway, Thank you for your attention/help!

1 Answers1

5
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
% based on https://tex.stackexchange.com/a/12033/121799
\tikzset{reverseclip/.style={insert path={(current bounding box.south west)rectangle 
(current bounding box.north east)} }, 
use path/.code={\pgfsetpath#1},%learned from Kpym
frame around/.style={insert path={
([xshift=-\pgfkeysvalueof{/tikz/frame
distance},yshift=-\pgfkeysvalueof{/tikz/frame distance}]#1.south west) rectangle
([xshift=\pgfkeysvalueof{/tikz/frame
distance},yshift=\pgfkeysvalueof{/tikz/frame distance}]#1.north east)}},
frame distance/.initial=5pt
}
\begin{document}
\pgfkeys{not inside/.code={\clip[use path=#1,reverseclip];},
inside/.code={\clip[use path=#1];},
shade/.code=\fill[#1] (current bounding box.south west)rectangle 
(current bounding box.north east);}
\begin{tikzpicture}
  \begin{scope}[local bounding box=TL]
   \draw[thick,fill=gray] (-1,0) node{$A$} circle [radius=1.5cm]
   (1,0) node{$B$} circle[radius=1.5cm] (0,1.5) node[above]{$M$}
   (0,-1.5) node[below]{$A\cup B$};
  \end{scope} 
  %
  \begin{scope}[xshift=5.5cm,local bounding box=TR]
   \draw[thick,save path=\pathA] (-1,0) node{$A$} circle [radius=1.5cm];
   \draw[thick,save path=\pathB] (1,0) node{$B$} circle[radius=1.5cm];
   \path (0,1.5) node[above]{$M$}
    (0,-1.5) node[below]{$A\cap B$};
   \begin{scope}[on background layer]
    \pgfkeys{inside/.list={\pathA,\pathB},shade=gray}
   \end{scope}
  \end{scope}
  %
  \begin{scope}[yshift=-4.5cm,local bounding box=BL]
   \draw[thick,fill=gray,even odd rule] (-0.5,0) node{$A$} circle [radius=1.5cm]
   (-2.5,-2) rectangle (2.5,2) node[below left]{$M$};
  \end{scope} 
  %
  \begin{scope}[xshift=5.5cm,yshift=-4.5cm,local bounding box=BR]
   \pgfresetboundingbox
   \draw[thick,save path=\pathC] (-1,0) node{$A$} circle [radius=1.5cm];
   \draw[thick,save path=\pathD] (1,0) node{$B$} circle[radius=1.5cm];
   \path (0,1.5) node[above]{$M$} (0,-1.5) node[below]{$A- B$};
   \pgfkeys{not inside=\pathD}
   \fill[gray,use path=\pathC];
  \end{scope}
  %
  \foreach \X in {TL,TR,BL,BR} {\draw[frame around=\X];}
\end{tikzpicture}
\end{document}

enter image description here