
質問: この図形を覆う長方形のボックスを描くにはどうすればよいでしょうか?
コード:
\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
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
。
egreg がコメントで述べているように、この特定のケースでは、 と のマクロを定義してもあまり意味がありません\firstcircle
。\secondcircle
なぜなら、それらは一度しか使用されないからです。実際のドキュメントに円がもっとある場合は、それはまた別の話です。また、 を使用する方が、既存のマクロを上書きせず、マクロがすでに何かに使用されている場合はエラーをスローするため、\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}