1

I am trying to add different color to the circles in my diagram while using a foreach to embed the circles within each other.

\documentclass[tikz,border=3.14mm]{standalone}

\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{amsfonts} 
\usepackage{amssymb}

\begin{document}

\begin{tikzpicture}[font=\sffamily, scale=0.8]
\foreach \X/\t [count=\Y starting from 2] in {Natural $\mathbb{N}$/red! ,Whole  numbers   $\mathbb{Z}$/green ,Rational $\mathbb{Q}$, Real algebraic $\mathbb{A}$, Real  $\mathbb{R}$}
 {\draw (-\Y,-\Y/2) circle ({1.5*\Y} and \Y);
 \node at (1-2*\Y,-1.1*\Y) {\X}; }
 \draw ([xshift=-0.5cm,yshift=-0.5cm]current bounding box.south west)
 rectangle ([xshift=0.5cm,yshift=0.5cm]current bounding box.north east);
 \node[anchor=south] at (current bounding box.north) {Types of numbers};
 \end{tikzpicture}

 \end{document}

any help or hint? thanks.

1
  • If you want to fill the ellipses, you need to draw them from the largest to the smallest. Otherwise the largest will cover a large part of your drawing. Sep 27, 2022 at 13:36

1 Answer 1

2

If you want to fill the ellipses, you need to draw the largest first and the smallest last, since otherwise the largest will cover a large part of your drawing.

Since you use the x/y notation with a \foreach loop where you use \X and \t as variables, all you need to do is to define each for-each item in the form of <text of the node>/<color> and then use \t (where you store the color part) at the relevant position of the following code, for example using the option fill=\t.

So, you could do the following:

\documentclass[border=3.14mm]{standalone}

\usepackage{tikz}
\usepackage{amsfonts} 
\usepackage{amssymb}

\begin{document}

\begin{tikzpicture}[font=\sffamily, scale=0.8]
    \foreach \X/\t [count=\Y starting from 2] in {
        Real $\mathbb{R}$/cyan,
        Real algebraic $\mathbb{A}$/green, 
        Rational $\mathbb{Q}$/yellow, 
        Whole numbers $\mathbb{Z}$/orange, 
        Natural $\mathbb{N}$/red} {
            \pgfmathsetmacro{\nY}{8-\Y}
            \draw[fill=\t!25] (-\nY,-\nY/2) circle ({1.5*\nY} and \nY);
            \node at (1-2*\nY,-1.1*\nY) {\X};
        }
    \draw ([xshift=-0.5cm, yshift=-0.5cm]current bounding box.south west)
        rectangle ([xshift=0.5cm,yshift=0.5cm]current bounding box.north east);
    \node[anchor=south] at (current bounding box.north) {Types of numbers};
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

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