Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to represent mathematical sets of number (R,Z,N) using nested circles like this:

enter image description here

This was very quickly created with gimp, but I am trying to do it properly in tikz but having some issue mainly to place circles and associated labels. I need a solution able to scale. What placement strategy should I use, I tried with

above =2

etc but it doesn't scale properly ?

share|improve this question
2  
It's just filled circle nodes with [label=-90:Z] etc options. Can you provide a minimal code and tell us where you get stuck? –  percusse Aug 29 '12 at 21:32

2 Answers 2

up vote 11 down vote accepted

Here's one option using circle \nodes and the positioning library to place the labels; scale=<factor>,transform shape gives you scaling:

\documentclass{article} 
\usepackage{amssymb} 
\usepackage{tikz} 
\usetikzlibrary{positioning}

\tikzset{set/.style={draw,circle,inner sep=0pt,align=center}}

\begin{document} 

\begin{tikzpicture}
\node[set,fill=blue!20,text width=3cm,label={[below=85pt of rea,text opacity=1]$\mathbb{R}$}] 
  (nat) at (0,-0.4)  (rea) {};
\node[set,fill=red!20,text width=2cm,label={[below=55pt of int]$\mathbb{Z}$}] 
  (int) at (0,-0.2)  {};
\node[set,fill=olive!20,text width=1cm] (nat) at (0,0) {$\mathbb{N}$};
\end{tikzpicture}

\begin{tikzpicture}[scale=0.7,transform shape]
\node[set,fill=blue!20,text width=3cm,label={[below=85pt of rea]$\mathbb{R}$}] 
  (nat) at (0,-0.4)  (rea) {};
\node[set,fill=red!20,text width=2cm,label={[below=55pt of int]$\mathbb{Z}$}] 
  (int) at (0,-0.2)  {};
\node[set,fill=olive!20,text width=1cm] (nat) at (0,0) {$\mathbb{N}$};
\end{tikzpicture}

\begin{tikzpicture}[scale=2.5,transform shape]
\node[set,fill=blue!20,text width=3cm,label={[below=85pt of rea]$\mathbb{R}$}] 
  (nat) at (0,-0.4)  (rea) {};
\node[set,fill=red!20,text width=2cm,label={[below=55pt of int]$\mathbb{Z}$}] 
  (int) at (0,-0.2)  {};
\node[set,fill=olive!20,text width=1cm] (nat) at (0,0) {$\mathbb{N}$};
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer

Here is a solution using positioning, fit, and backgrounds libraries.

enter image description here

The preamble:

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds}

The \drawnestedsets macro:

\newcommand\drawnestedsets[4]{
  % initial position
  \def\position{#1}
  % number of sets
  \def\nbsets{#2}
  % list of sets
  \def\listofnestedsets{#3}
  % reversed list of colors
  \def\reversedlistofcolors{#4}

  % position and draw labels of sets
  \coordinate (circle-0) at (#1);
  \coordinate (set-0) at (#1);
  \foreach \set [count=\c] in \listofnestedsets {
    \pgfmathtruncatemacro{\cminusone}{\c - 1}
    % label of current set (below previous nested set)
    \node[below=3pt of circle-\cminusone,inner sep=0]
    (set-\c) {$\mathbb{\set}$};
    % current set (fit current label and previous set)
    \node[circle,inner sep=0,fit=(circle-\cminusone)(set-\c)]
    (circle-\c) {};
  }

  % draw and fill sets in reverse order
  \begin{scope}[on background layer]
    \foreach \col[count=\c] in \reversedlistofcolors {
      \pgfmathtruncatemacro{\invc}{\nbsets-\c}
      \pgfmathtruncatemacro{\invcplusone}{\invc+1}
      \node[circle,draw,fill=\col,inner sep=0,
      fit=(circle-\invc)(set-\invcplusone)] {};
    }
  \end{scope}
}

The document:

\begin{document}
\begin{tikzpicture}
  \drawnestedsets{0,0}{4}{N,Z,R,C}{lime,orange,yellow,cyan}

  \begin{scope}[font=\tiny]
    \drawnestedsets{2,0}{3}{N,Z,R}{orange,yellow,cyan}
  \end{scope}
\end{tikzpicture}
\end{document}

Edit: Here is an horizontal variant with ellipses:

enter image description here

And the code (note: the \drawnestedsets macro is a modified version):

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds,shapes.geometric}

\newcommand\drawnestedsets[4]{
  % initial position
  \def\position{#1}
  % number of sets
  \def\nbsets{#2}
  % list of sets
  \def\listofnestedsets{#3}
  % reversed list of colors
  \def\reversedlistofcolors{#4}

  % position and draw labels of sets
  \coordinate (circle-0) at (#1);
  \coordinate (set-0) at (#1);
  \foreach \set [count=\c] in \listofnestedsets {
    \pgfmathtruncatemacro{\cminusone}{\c - 1}
    % label of current set (below previous nested set)
    \node[right=3pt of circle-\cminusone,inner sep=0]
    (set-\c) {$\mathbb{\set}$};
    % current set (fit current label and previous set)
    \node[ellipse,inner sep=0,fit=(circle-\cminusone)(set-\c)]
    (circle-\c) {};
  }

  % draw and fill sets in reverse order
  \begin{scope}[on background layer]
    \foreach \col[count=\c] in \reversedlistofcolors {
      \pgfmathtruncatemacro{\invc}{\nbsets-\c}
      \pgfmathtruncatemacro{\invcplusone}{\invc+1}
      \node[ellipse,draw,fill=\col,inner sep=0,
      fit=(circle-\invc)(set-\invcplusone)] {};
    }
  \end{scope}
}

\begin{document}
\begin{tikzpicture}
  \drawnestedsets{0,0}{4}{N,Z,R,C}{lime,orange,yellow,cyan}

  \begin{scope}[font=\tiny]
    \drawnestedsets{0,-1}{3}{N,Z,R}{orange,yellow,cyan}
  \end{scope}
\end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.