问题:
两个小问题我还没能解决。
- 我该如何填充第二个图形(右侧)的交叉点?
- 如何在每个框中的圆圈周围创建“填充”或更多的“空气”?
附加问题:
为什么图 2 的矩形比图 1 的矩形高?这两个框的代码相同。
最小工作示例(MWE):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning,calc}
\usepackage{float}
\usepackage{showframe}
\begin{document}
\begin{figure}[H]
\centering
\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[show background rectangle]
% Sets colors for cirlces
\colorlet{circle edge}{black}
\colorlet{circle area}{gray!50}
% Sets style for filled and outline
\tikzset{
filled/.style={fill=circle area, thick,inner sep=0pt},
outline/.style={draw=circle edge, thick,inner sep=0pt}}
% The circles
\node (secondcircle) [circle,filled,text width=3cm] {};
\node (firstcircle) [circle,filled,left=-1cm of secondcircle,outline,text width=3cm] {};
\draw [outline] (secondcircle) circle (1.5cm);
% The labels
\node at ([xshift=-0.15cm]firstcircle) {$A$};
\node at ([xshift=0.4cm]secondcircle) {$B$};
\end{tikzpicture}
\caption{$\mathbf{A} \cup \mathbf{B}$}
\label{fig:venncup}
\end{minipage}
\hspace{0.02\linewidth}
\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[show background rectangle]
% Sets colors for cirlces
\colorlet{circle edge}{black}
\colorlet{circle area}{gray!50}
% Sets style for filled and outline
\tikzset{
filled/.style={fill=circle area, thick,inner sep=0pt},
outline/.style={draw=circle edge, thick,inner sep=0pt}}
% The circles
\node (secondcircle) [circle,fill=white,text width=3cm] {};
\node (firstcircle) [circle,left=-1cm of secondcircle,outline,text width=3cm] {};
\draw [outline] (secondcircle) circle (1.5cm);
% The labels
\node at ([xshift=-0.15cm]firstcircle) {$A$};
\node at ([xshift=0.4cm]secondcircle) {$B$};
\end{tikzpicture}
\caption{$\mathbf{A} \cap \mathbf{B}$}
\label{fig:venncap}
\end{minipage}
\end{figure}
\end{document}
电流输出:
期望输出:
填充第二个图形(右边的一个)的交点并“缩小”圆圈以便在它们周围增加空间。
答案1
以前也曾有人问过这种类型的问题,但最近情况有所改变:最新版本的 Ti钾Z 有use path
键,这使我们能够重复使用路径。(这个save path
键存在了很长时间,但没有提到。)这允许您重新绘制节点的圆路径,而无需手动拨动半径等。因此,在左图中,您可以使用
\node[save path=\pathB] (secondcircle) [my circ] {};
并将其回收利用
\draw[outline,use path=\pathB];
在第二张图中,你可以使用
\node[save path=\pathB] (secondcircle) [my circ] {};
\node[save path=\pathA] (firstcircle) [my circ,left=-1cm of secondcircle] {};
然后使用这些路径进行裁剪和填充,
\clip[use path=\pathA];
\fill[blue,use path=\pathB];
为了方便起见,我还添加了一个bb
扩展和绘制边界框的样式,请参阅下面的用法。(这里不能做得过头,因为没有太多剩余空间,但如果你改变外部参数,你可以给图片更多的呼吸空间。)
我还(在本地)定义了一个样式my circ
,固定了所有圆的属性。大小由minimum size
键控制。在左图中,定义附加在 上filled
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\usepackage{float}
\usepackage{showframe}
\begin{document}
\begin{figure}[H]
\tikzset{bb/.style={insert path={([xshift=-#1,yshift=-#1]current bounding box.south west) rectangle
([xshift=#1,yshift=#1]current bounding box.north east) }},
filled/.style={fill=circle area, thick,inner sep=0pt},
outline/.style={draw=circle edge, thick,inner sep=0pt},
my circ/.style={minimum size=2.5cm,circle,outline}}
\centering
\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[my circ/.append style={filled}]
% Sets colors for cirlces
\colorlet{circle edge}{black}
\colorlet{circle area}{gray!50}
% Sets style for filled and outline
% The circles
\node[save path=\pathB] (secondcircle) [my circ] {};
\node (firstcircle) [my circ,left=-1cm of secondcircle] {};
\draw[outline,use path=\pathB];
% The labels
\node at ([xshift=-0.15cm]firstcircle) {$A$};
\node at ([xshift=0.4cm]secondcircle) {$B$};
\draw[bb=1.3em];
\end{tikzpicture}
\caption{$\mathbf{A} \cup \mathbf{B}$}
\label{fig:venncup}
\end{minipage}
\hspace{0.02\linewidth}
\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}
% Sets colors for cirlces
\colorlet{circle edge}{black}
\colorlet{circle area}{gray!50}
% Sets style for filled and outline
% The circles
\node[save path=\pathB] (secondcircle) [my circ] {};
\node[save path=\pathA] (firstcircle) [my circ,left=-1cm of secondcircle] {};
% The labels
\node at ([xshift=-0.15cm]firstcircle) {$A$};
\node at ([xshift=0.4cm]secondcircle) {$B$};
\begin{scope}[on background layer]
\clip[use path=\pathA];
\fill[blue,use path=\pathB];
\end{scope}
\draw[bb=1.3em];
\end{tikzpicture}
\caption{$\mathbf{A} \cap \mathbf{B}$}
\label{fig:venncap}
\end{minipage}
\end{figure}
\end{document}
答案2
venndiagram
你可以使用该包(基于 TikZ)获得更简单的代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning,calc}
\usepackage{venndiagram}
\usepackage{float, caption}
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.3pt}
\begin{document}
{\captionsetup[figure]{skip =-1.5ex}
\begin{figure}[H]
\centering
\begin{minipage}[t]{0.45\textwidth}\begin{venndiagram2sets}[radius=1.3cm]
\fillA \fillB
\end{venndiagram2sets}
\caption{$\mathbf{A} \cup \mathbf{B}$}
\label{fig:venncup}
\end{minipage}
\hspace{0.02\linewidth}
\begin{minipage}[t]{0.45\textwidth}\begin{venndiagram2sets}[radius=1.3cm]
\fillACapB
\end{venndiagram2sets}
\caption{$\mathbf{A} \cap \mathbf{B}$}
\label{fig:venncap}
\end{minipage}
\end{figure}}
\end{document}