我已经设法完成以下代码,剩下就是绘制集合 $I = J \cap X \cap Y$ 并将字母 J 放下,如图所示:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) circle (1) (0,1) node [text=black,above] {$X$}
(1,0) circle (1) (1,1) node [text=black,above] {$Y$}
(-0.4, -0.3) rectangle (1.5,0.5) node [text=black,above] {$J$}
(-2,-2) rectangle (3,2) node [text=black,above] {$S$};
\end{tikzpicture}
\end{document}
答案1
这是一个使用save path
/use path
指令来避免重复代码的解决方案。我擅自重写了部分代码。
填充的部分放在背景层上,以免吃掉设置的边框的一部分。
\documentclass[tikz]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
\draw[save path = \circleX] (0,0) circle (1);
\node[above] at (0,1) {$X$};
\draw[save path = \circleY] (1,0) circle (1);
\node[above] at (1,1) {$Y$};
\draw[save path = \rectangleJ] (-0.4, -0.3) rectangle (1.5,0.5)
node [below right] {$J$};
\draw (-2,-2) rectangle (3,2)
node [above] {$S$};
\begin{scope}[on background layer]
\clip[use path = \circleX];
\clip[use path = \circleY];
\clip[use path = \rectangleJ];
\fill[use path = \rectangleJ, blue];
\end{scope}
\end{tikzpicture}
\end{document}
结果是:
编辑
获取J
底部节点的一种方法是执行以下操作,而不是\draw
定义矩形:
\draw[save path = \rectangleJ] (-0.4, -0.3) coordinate (A)
rectangle (1.5,0.5) coordinate (B);
\coordinate (C) at (A -| B);
\node[below] at (C) {$J$};
编辑2
要添加填充区域的名称,可以node[pos = .5] {$I$}
在定义矩形的命令末尾添加\draw
。实际上,在这种情况下,交叉点或多或少位于矩形的中心。
答案2
\documentclass[tikz, border=1 cm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (-2,-2) rectangle (3,2) node[above] {$S$};
\draw (0,0) circle[radius=1] (0,1) node[above left] {$X$};
\draw (1,0) circle[radius=1] (1,1) node[above right] {$Y$};
\begin{scope}
\clip (0,0) circle[radius=1];
\clip (1,0) circle[radius=1];
\fill[blue] (-0.4, 0.5) rectangle (1.5,-0.3) node[black,pos=0.5]{I};
\end{scope}
\draw (-0.4, 0.5) rectangle (1.5,-0.3) node[below] {$J$};
\end{tikzpicture}
\end{document}