
질문: 이 그림을 덮는 직사각형 상자를 어떻게 그릴 수 있나요?
코드:
\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{배경}을 사용하고 간단히 옵션을 추가하면 됩니다.
[show background rectangle]
\begin{tikzpicture} 바로 뒤에
답변3
라이브러리를 사용하는 대체 접근 방식입니다 fit
.
\firstcircle
egreg가 주석에서 언급했듯이 이 특별한 경우에는 및 에 대한 매크로를 정의하는 데 거의 의미가 없습니다 \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}