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.
Asked
Active
Viewed 429 times
3
2 Answers
11
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}
marsupilam
- 6,383
-
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, usingfit. – CarLaTeX Jun 19 '17 at 01:45 -
-
Yes, the blue part represents the algebraic irrationals, but “Reals” should go outside. – egreg Jun 19 '17 at 09: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. – marsupilam Jun 19 '17 at 09: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? – Ulrike Fischer Jun 19 '17 at 09:58
-
@UlrikeFischer Sorry, I don't see what you mean :
sqrt(2)is a real that is neither rational nor non-algebraic=>blue zone.piis a real that is non-algebraic=>orange zone. – marsupilam Jun 19 '17 at 10:02 -
But the blue zone is around the rationals. So following your own argument above "10" is a natural, an integer, a rational and a "neither rational nor non-algebraic" number. – Ulrike Fischer Jun 19 '17 at 10:07
-
@UlrikeFischer Sorry if I'm being dense, but I think this is what a mathematically sound Euler diagram of those sets looks like when we fill without using transparency (the layers are even correct in the pdf file). – marsupilam Jun 19 '17 at 10:17
-
The irrational numbers are all the numbers that are not rational. So rational/irrational splits the real number in two disctinct, non overlaping parts, which fills up the reals. Your image doesn't convey this idea. Replace "rational" by mammals, and "irrational" by non-mammals and "real" by animals. – Ulrike Fischer Jun 19 '17 at 10:28
-
@UlrikeFischer, I understand your point about rationals and irrationals, you are right. But the purpose of the post is just about TikZ!
But thanks to everyone that helped with the code examples. This was very helpful!
– Gustavo Amarante Jun 20 '17 at 12:35
8
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}
Ignasi
- 136,588



\drawcommand 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. – Gustavo Amarante Jun 18 '17 at 20:27\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 leveland so on. – Huang_d Jun 18 '17 at 21:24