考虑以下 MWE:
\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle +(4,3) node[below left] {$\mathcal{U}$};
\begin{scope}
\clip (1.25,1.25) coordinate (a) circle (1.0);
\fill[color=gray] (2.75,1.25) coordinate (b) circle (1.0);
\end{scope}
\draw[fill=gray, even odd rule] (a) circle (1.0) (1.25,2.25) node [above] {$A$}
(b) circle (1.0) (2.75,2.25) node[above] {$B$};
\node (c) at (a) {0};
\node (d) at (b) {7};
\node[yshift=3pt,label=below:3] (e) at ($(a)!.5!(b)$){};
\node[yshift=-3pt,label=above:2] (f) at ($(a)!.5!(b)$){};
\end{tikzpicture}
\end{document}
代码产生了预期的输出,但我想知道是否有更短的方法来实现相同的结果。
答案1
短一点。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[declare function={R=1cm;}]
\filldraw [fill=gray] node(a){0} circle[radius=R] (R*1.5,0) node(b){7} circle[radius=R];
\path (a) ++(0,R) node[above]{$A$} (b) ++(0,R) node[above]{$B$} (a) ++(R*0.75,0) node[align=center] {2\\3};
\path node[fit=(current bounding box),draw,inner sep=3mm] (bb) {} node [below left] at (bb.north east) {$\mathcal{U}$};
\end{tikzpicture}
\end{document}
答案2
稍微简单一点。
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\r{1} \def\a{.7}
\draw[fill=gray]
(\a,0) circle(\r) node{7} +(90:\r) node[above]{B}
(-\a,0) circle(\r) node{0} +(90:\r) node[above]{A}
(0,0) node[above]{2} node[below]{3};
\path
(current bounding box.south west)+(-.5,-.5) coordinate (M)
(current bounding box.north east)+(.5,.5) coordinate (N);
\draw (M) rectangle (N) node[below left]{$\mathcal{U}$};
\end{tikzpicture}
\end{document}