3

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.

enter image description here

  • and what you try so far? – Zarko Jun 18 '17 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. – Gustavo Amarante Jun 18 '17 at 20:27
  • 1
    Please add a minimal working example (MWE) of you've tried so far. – CarLaTeX Jun 18 '17 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. – Bobyandbob Jun 18 '17 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. – Huang_d Jun 18 '17 at 21:24
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

enter image description here

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 pics are perfect for me, but I'm a beginner! I'd like to answer here, too, using fit. – CarLaTeX Jun 19 '17 at 1:45
  • ... but Ignasi was faster than me, LOL! – CarLaTeX Jun 19 '17 at 8:08
  • Yes, the blue part represents the algebraic irrationals, but “Reals” should go outside. – egreg Jun 19 '17 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. – marsupilam Jun 19 '17 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? – Ulrike Fischer Jun 19 '17 at 9:58
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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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