根據給定的維恩圖繪製一個矩形

根據給定的維恩圖繪製一個矩形

問題:如何繪製一個覆蓋該圖形的矩形框?

代碼:

\documentclass{article}
\usepackage{tikz}
\begin{document}

% Definition of circles
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(0:2cm) circle (1.5cm)}

\colorlet{circle edge}{blue!50}
\colorlet{circle area}{blue!20}

\tikzset{filled/.style={fill=circle area, draw=circle edge, thick},
    outline/.style={draw=circle edge, thick}}
% Set A or B
\begin{tikzpicture}
    \draw[filled] \firstcircle node {$A$}
                  \secondcircle node {$B$};
    \node[anchor=south] at (current bounding box.north) {$A \cup B$};
\end{tikzpicture}

\end{document}

(取自 www.texample.net)

維恩圖

答案1

一種可能性是使用邊界框。

\documentclass{article}
\usepackage{tikz}
\begin{document}

% Definition of circles
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(0:2cm) circle (1.5cm)}

\colorlet{circle edge}{blue!50} \colorlet{circle area}{blue!20}

\tikzset{filled/.style={fill=circle area, draw=circle edge, thick},
    outline/.style={draw=circle edge, thick}}
% Set A or B
\begin{tikzpicture}
 \draw[filled] \firstcircle node {$A$}
               \secondcircle node {$B$};
 \node[anchor=south] at (current bounding box.north) {$A \cup B$};
 \draw (current bounding box.north west) rectangle
       (current bounding box.south east);
\end{tikzpicture}

\end{document}

如果您需要一個更大的盒子(這不一定是最優雅的解決方案,但它有效):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

% Definition of circles
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(0:2cm) circle (1.5cm)}

\colorlet{circle edge}{blue!50} \colorlet{circle area}{blue!20}

\tikzset{filled/.style={fill=circle area, draw=circle edge, thick},
    outline/.style={draw=circle edge, thick}}
% Set A or B
\begin{tikzpicture}
 \draw[filled] \firstcircle node {$A$}
               \secondcircle node {$B$};
 \node[anchor=south] at (current bounding box.north) {$A \cup B$};
 \draw ($(current bounding box.north west)+(-1,1)$) 
       node [below right] {$U$}
       rectangle ($(current bounding box.south east)+(1,-1)$);
\end{tikzpicture}

\end{document}

答案2

您可以使用 \usetikzlibrary{backgrounds} 並只需新增選項

[show background rectangle]

就在 \begin{tikzpicture} 之後

答案3

使用庫的替代方法fit

\firstcircle請注意,正如 egreg 在評論中提到的那樣,在這種特殊情況下,為和定義巨集沒有什麼意義\secondcircle,因為您只使用它們一次。如果您的實際文件有更多圓圈,那就是另一回事了。另請注意, using\newcommand通常優於\def,因為它不會覆寫現有巨集,但如果該巨集已用於某些用途,則會拋出錯誤。

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}

\colorlet{circle edge}{blue!50}
\colorlet{circle area}{blue!20}

\tikzset{
  filled/.style={fill=circle area, draw=circle edge, thick},
  outline/.style={draw=circle edge, thick}
}
\begin{document}
% Set A or B
\begin{tikzpicture}
  \draw[filled] (0,0) circle[radius=1.5cm] node {$A$}
               (0:2cm) circle[radius=1.5cm] node {$B$};
  \node[anchor=south] at (current bounding box.north) {$A \cup B$};

  % draw frame
  \node [draw,fit=(current bounding box),inner sep=3mm] (frame) {}; % modify inner sep to adjust gap from circles to frame

  % add label
  \node [below right] at (frame.north west) {$U$};
\end{tikzpicture}
\end{document}

相關內容