
Frage: Wie kann ich ein rechteckiges Kästchen zeichnen, das diese Figur abdeckt?
Codes:
\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}
(von www.texample.net)
Antwort1
Eine Möglichkeit besteht darin, den Begrenzungsrahmen zu verwenden.
\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}
Falls du eine etwas größere Box benötigst (das muss nicht die eleganteste Lösung sein, funktioniert aber):
\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}
Antwort2
Sie können \usetikzlibrary{backgrounds} verwenden und einfach die Option hinzufügen
[show background rectangle]
direkt nach \begin{tikzpicture}
Antwort3
Ein alternativer Ansatz unter Verwendung der fit
Bibliothek.
Beachten Sie, wie egreg in einem Kommentar erwähnt, dass es in diesem speziellen Fall wenig Sinn macht, Makros für \firstcircle
und zu definieren \secondcircle
, da Sie sie nur einmal verwenden. Wenn Ihr eigentliches Dokument mehr Kreise hat, ist das eine andere Sache. Beachten Sie auch, dass die Verwendung von \newcommand
im Allgemeinen der von vorzuziehen ist \def
, da vorhandene Makros nicht überschrieben werden, sondern ein Fehler ausgegeben wird, wenn das Makro bereits für etwas verwendet wird.
\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}