# Overlayed Diagrams with TikZ

I am looking for an efficient way to my a diagram like the one below with TikZ. I have looked in the manual and searched for other questions but could not find something that helped me. Any help on this topic is very welcome. Thanks everyone.

• and what you try so far? Jun 18, 2017 at 20:19
• so far I used the \draw command to do the rectangles. But they get overlayed in the wrong order and I do not know how to change the position of the text inside. Jun 18, 2017 at 20:27
• Please add a minimal working example (MWE) of you've tried so far. Jun 18, 2017 at 20:30
• 1. You could define different boxes(block1, block2,...) with \tikzset{block/.style={ rectangle, draw=blue!100, fill=blue!70, rounded corners=15pt, minimum height=10em, minimum width=20em, ... }} 2. Positioning with \node[block] (A) at (0,0) {}; 3. Text inside: \node[below] at (A.north) {Real}; . Related links-> An example for defining a box you can find here and for tikzset here. Jun 18, 2017 at 21:05
• Tikz draws in the order you give the commands, i.e. back to front. If you have a simple overlap problem, just re-arrange your code to reflect the levels. That is, \draw first level, \draw second level and so on. Jun 18, 2017 at 21:24

My audacious mathematical theory about Euler diagrams seem to have failed to convince in the comments, so I revert back.

(and my code did not compile due to a linebreak)

Anyway, I borrow some of the techniques demo'd by @CarLaTeX here. Props to her !

### Edits

• Fixed colors and fonts
• No more rules, added node for title positioning

## The code

\documentclass[tikz]{standalone}
\usepackage[scaled]{helvet}
\renewcommand\familydefault{\sfdefault}
\usepackage{tikz}

\begin{document}

\definecolor{myCyan}{HTML}{479FF8}
\definecolor{myRed}{HTML}{DB3D24}
\definecolor{myGreen}{HTML}{81D552}
\definecolor{myOrange}{HTML}{EFBD3F}
\tikzset
{
pics/maBoite/.style args={#1/#2/#3/#4/#5/#6}%
{
code=
{
\fill [rounded corners=.4cm, #3] (-belowLeft) rectangle (-topRight) ;
\makeatletter
\path (-belowLeft) |- (-topRight) coordinate[pos=.75] (-titleT@pTmp);
\path (-titleT@pTmp) -- ++(0,-4.5mm) coordinate (-titleTop);
\makeatother
\path (-belowLeft) -- (-topRight) coordinate[pos=.5] (-titleMiddle);
\node at #6 [white, font=\large, inner xsep=0pt,inner ysep=2.5mm] {\textbf{#4}} ;
}
},
}
\begin{tikzpicture}[x=7cm,y=4cm]
\coordinate (a) at (0,0);
\coordinate (b) at (1,1);
\coordinate (mt) at (.5,1);
\coordinate (mb) at (.5,0);
% syntax
% \pic
% {
%   maBoite=
%   {
%     <lowerLeft>/<upperRight>/<color>/%
%   }
% };
\pic (R) {maBoite={a/b/myCyan/Reals/0/(-titleTop)}};
\end{tikzpicture}
\end{document}

• You did well to ping me in a comment under my answer because it doesn't work in an answer body. OMG, I have a follower, LOL! +1, your pics are perfect for me, but I'm a beginner! I'd like to answer here, too, using fit. Jun 19, 2017 at 1:45
• ... but Ignasi was faster than me, LOL! Jun 19, 2017 at 8:08
• Yes, the blue part represents the algebraic irrationals, but “Reals” should go outside. Jun 19, 2017 at 9:44
• @egreg I think what I drew is coherent : the green area integers is to be understood as containing the naturals, even though those are not filled in green. The same goes for reals. Jun 19, 2017 at 9:50
• @marsupilam Imho it looks wrong (not your fault as the starting image in itself is problematic). Where e.g. would you place \sqrt{2}? And where \pi? Jun 19, 2017 at 9:58

Another solution with nodes placed on a matrix (you could also use positioning library) and color boxes drawn as fit nodes on a background layer.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix, backgrounds, fit}
\usepackage{lmodern}
\begin{document}
\begin{tikzpicture}[mytext/.style={text=white, font=\sffamily\large}]

\matrix (N) [
matrix of nodes,
nodes={mytext, anchor=center, rounded corners},
label={[name=name, mytext]Reals},
column sep=3mm,
row sep= 0mm,]{
Rationals & \\
Integers & Irrationals \\
|[fill=cyan]|Naturals \\
};

\begin{scope}[on background layer]
\node[fit={(N) (name)}, fill=cyan, rounded corners] {};

\node[fit=(N-1-1) (N-3-1), fill=red, rounded corners] (aux) {};

\node[fit=(N-2-1) (N-3-1), inner ysep=1pt, fill=green, rounded corners] {};

\node[fit=(aux), inner sep=0pt, anchor=center, fill=orange, rounded corners] at (N-2-2){};
\end{scope}
\end{tikzpicture}
\end{document}