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.
2 Answers
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
- Added CSS-like padding syntax
- No more rules, added node for title positioning
The output
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=
{
\def\myPaddingArray{{#5,#5,#5,#5}}
\pgfmathsetmacro{\paddingTop}{\myPaddingArray[0]}
\pgfmathsetmacro{\paddingRight}{\myPaddingArray[1]}
\pgfmathsetmacro{\paddingBottom}{\myPaddingArray[2]}
\pgfmathsetmacro{\paddingLeft}{\myPaddingArray[3]}
\path {[name prefix ..](#1)} -- ++(\paddingLeft pt,\paddingBottom pt) coordinate (-belowLeft);
\path {[name prefix ..](#2)} -- ++(-\paddingRight pt,-\paddingTop pt) coordinate (-topRight);
\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>/%
% <title>/<CSS-syntax padding>/<title-position>
% }
% };
\def\myTopPadding{.85cm}
\def\myBottomPadding{.15cm}
\def\mySidePadding{.15cm}
\pic (R) {maBoite={a/b/myCyan/Reals/0/(-titleTop)}};
\pic (Q) {maBoite={R-belowLeft/mt/myRed/Rationals/\myTopPadding,.07cm,\myBottomPadding,.2cm/(-titleTop)}};
\pic (Z) {maBoite={Q-belowLeft/Q-topRight/myGreen/Integers/\myTopPadding,\mySidePadding,\myBottomPadding,\mySidePadding/(-titleTop)}};
\pic (N) {maBoite={Z-belowLeft/Z-topRight/myCyan/Naturals/\myTopPadding,\mySidePadding,\myBottomPadding,\mySidePadding/(-titleMiddle)}};
\pic (I) {maBoite={mb/R-topRight/myOrange/Irrationals/\myTopPadding,.2cm,\myBottomPadding,.07cm/(-titleMiddle)}};
\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
pic
s are perfect for me, but I'm a beginner! I'd like to answer here, too, usingfit
.– CarLaTeXJun 19, 2017 at 1:45 -
-
Yes, the blue part represents the algebraic irrationals, but “Reals” should go outside.– egregJun 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}
\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.\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.\draw first level
,\draw second level
and so on.