使用 TikZ 叠加图表

使用 TikZ 叠加图表

我正在寻找一种有效的方法,用 TikZ 绘制如下图所示的图表。我查看了手册并搜索了其他问题,但找不到对我有帮助的东西。非常欢迎任何有关此主题的帮助。谢谢大家。

在此处输入图片描述

答案1

我的大胆数学理论欧拉图 评论中似乎未能令人信服,所以我恢复了原样。

(由于换行,我的代码无法编译)

无论如何,我借用了@CarLaTeX 演示的一些技巧这里. 向她致敬!

編輯

  • 固定颜色和字体
  • 添加了类似 CSS 的填充语法
  • 不再有规则,添加了标题定位节点

输出

在此处输入图片描述

代码

\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}

答案2

matrix另一种解决方案是将节点放置在(您也可以使用库)上positioning,并将颜色框绘制为层fit上的节点background

\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}

在此处输入图片描述

相关内容