3

I'm using the following tex to draw some elements in a set using relative positioning:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes}
\usetikzlibrary{backgrounds}

\def\leftset{(0,0) circle(2cm)}
\def\rightset{(0:2.5cm) circle(2cm)}

\begin{document}

\begin{tikzpicture}[
    ce/.style={draw,circle,fill,red, minimum size=0.1cm},
    se/.style={draw,star,star points=5,star point ratio=0.5,fill=green, minimum size=0.1cm}
    ]

    \begin{scope}
        \draw \leftset node[below] [label=below: {$A$}] {};
        \node[ce] at (-.2, -1)  (a1) [label=right: {$a_1$}] {};
        \node[ce] at (-.2, 0)   (a2) [label=right: {$a_2$}] {};
        \node[ce] at (-.2, 1)   (a3) [label=right: {$a_3$}] {};
    \end{scope}

    \begin{scope}[shift={(4.2cm,0cm)}]
        \draw \leftset node[below] [label=below: {$B$}] {};
        \node[se] at (-.2, -1)  (b1) [label=right: {$b_1$}] {};
        \node[se] at (-.2, 0)   (b2) [label=right: {$b_2$}] {}; 
        \node[se] at (-.2, 1)   (b3) [label=right: {$b_3$}] {};
    \end{scope}

\end{tikzpicture}

\end{document}

My questions are:

  1. Why the set names $A$, $B$, are not below the set drawing?

  2. Is there a simpler way to define sets and it's elements something like: \drawset at (x,y) elements[circle] {$a_1$, $a_2$, $a_3$}?

Of course, any improvements are also welcome.

2 Answers 2

4
  1. Why the set names $A$, $B$, are not below the set drawing?

You use construction \draw (0,0) circle(2cm) node[below] [label=below: {$A$}] {}; which means place the pen at origin (0,0) and draw a circle. This instruction doesn't move the pen, therefore, node[below] is drawn also with pen at (0,0). If instead a circle, you draw a circular node with a label=below:..., this label will be below the circle.

  1. Is there a simpler way to define sets and it's elements something like

Following code could be a solution, you draw set elements where you want and the set is drawn with a fit node around them. By the way, I've placed labels to elements as labels instead of independent nodes.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes}
\usetikzlibrary{backgrounds, fit}

\def\leftset{(0,0) circle(2cm)}
\def\rightset{(0:2.5cm) circle(2cm)}

\begin{document}

\begin{tikzpicture}[
    ce/.style={draw,circle,fill,red, minimum size=0.1cm},
    se/.style={draw,star,star points=5,star point ratio=0.5,fill=green, minimum size=0.1cm}
    ]

    \begin{scope}
        \node[ce, label=right:$a_1$] at (-.2, -1) (a1) {};
        \node[ce, label=right:$a_1$] at (-.2, 0) (a2) {};
        \node[ce, label=right:$a_1$] at (-.2, 1) (a3) {};
        \node[draw, circle, fit=(a1) (a2) (a3), label=below:{$A$}] {};
    \end{scope}

    \begin{scope}[shift={(4.2cm,0cm)}]
        \node[se, label=right: {$b_1$}] at (-.2, -1)  (b1) {};
        \node[se, label=right: {$b_2$}] at (-.2, 0)   (b2) {}; 
        \node[se, label=right: {$b_3$}] at (-.2, 1)   (b3) {};
        \node[draw, circle, fit=(b1) (b2) (b3), label=below:{$B$}] {};
    \end{scope}

\end{tikzpicture}

\end{document}

enter image description here

2
  1. Node coordinate for A bad B is (0,0). Hence, these nodes coordinates are below of a2 or b2. Using positioning library, one can set it "below of a1".

  2. I don't know \drawset command. I prefer to use of \foreach.

Code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes}
\usetikzlibrary{backgrounds}

\def\leftset{(0,0) circle(2cm)}
\def\rightset{(0:2.5cm) circle(2cm)}

\begin{document}

\begin{tikzpicture}[
    ce/.style={draw,circle,fill,red, minimum size=0.1cm},
    se/.style={draw,star,star points=5,star point ratio=0.5,fill=green, minimum size=0.1cm}
    ]

    \begin{scope}
    \foreach \y [count=\x] in {-1,0,1}{
    \node [ce] at (-.2,\y) (a\x) [label=right: {$a_{\x}$}] {};
    };
    \draw \leftset node[below of=a1,yshift=3mm] {$A$};
   \end{scope}

    \begin{scope}[shift={(4.2cm,0cm)}]
    \foreach \y [count=\x] in {-1,0,1}{
    \node [se] at (-.2,\y) (b\x) [label=right: {$b_{\x}$}] {};
    };
    \draw \leftset node[below of=b1,yshift=3mm] {$B$};
    \end{scope}

\end{tikzpicture}
\end{document}

Output: enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .