我需要为第三个图上的“M”矩形着色,并删除第四个图上的 A 和 B 之间的交点。
我正在使用以下代码:
\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}
事情的结局是这样的:
如果我缺少任何信息,我很抱歉,我对 LaTeX 很陌生,并且我已经尝试这样做很长一段时间了。
无论如何,感谢您的关注/帮助!
答案1
\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}