在 tikz 中添加换行符并为图表添加颜色

在 tikz 中添加换行符并为图表添加颜色

我拥有最少的代码来生成我想要的东西,但我不确定

  1. 如何将文本分成单独的行,例如定义下面的示例。

  2. 给椭圆涂上颜色。

      \documentclass[tikz,border=3.14mm]{book}
    
      \usepackage{tikz}
      \usepackage{amsfonts} 
      \usepackage{amssymb}
    
      \newcommand{\x}[1]{\mathbb{#1}}
    
      \begin{document}
    
      \begin{tikzpicture}[font=\sffamily, scale=0.8]
      \foreach \X [count=\Y starting from 2] in {Natural \(\x{N}\) ,Whole numbers \  (\x{Z}\) ,Rational \( \x {Q} \), Real algebraic \( \x{A} \), Real \(\x{R} \) \\   ($\pi$)}
       {\draw (-\Y,-\Y/2) circle ({1.5*\Y} and \Y);
       \node at (1-2*\Y,-1.1*\Y) {\X}; }
       \draw ([xshift=-0.5cm,yshift=-0.5cm]current bounding box.south west)
       rectangle ([xshift=0.5cm,yshift=0.5cm]current bounding box.north east);
       \node[anchor=south] at (current bounding box.north) {Types of numbers};
       \end{tikzpicture}
    
       \end{document}
    

欢迎任何帮助。

答案1

要有多行节点内容,您需要将align=center(或左侧或右侧)添加到节点。要为椭圆着色,请从大到小绘制它们并添加fill=<color>。我将颜色作为附加变量包含在 for 循环中。

{...}注意:由于有逗号,所以数字列表需要用括号 ( ) 括起来。

在此处输入图片描述

  \documentclass[tikz,border=3.14mm]{book}

  \usepackage{tikz}
  \usepackage{amsfonts} 
  \usepackage{amssymb}

  \newcommand{\x}[1]{\mathbb{#1}}

  \begin{document}

  \begin{tikzpicture}[font=\sffamily, scale=0.9]
  \foreach \X/\col [count=\N, evaluate=\N as \Y using 7-\N] in {Real $\x{R}$\\{$\pi,e,\ln2$}/blue, Real algebraic $\x{A}$\\{$\sqrt{5}, \varphi, \sqrt[3]{2}+\sqrt{3}$}/green, Rational $\x{Q}$\\{$-\frac{1}{2}, .7, .3\overline{3}$}/yellow, Integers $\x{Z}$\\{$-5,0,12$}/orange, Natural $\x{N}$\\{$1,2,3$}/red}
   {\draw[fill=\col!30] (-\Y,-\Y/2) circle ({1.5*\Y} and \Y);
   \node[align=center] at (1-2*\Y,-1.1*\Y) {\X}; }
   \draw ([xshift=-0.5cm,yshift=-0.5cm]current bounding box.south west)
   rectangle ([xshift=0.5cm,yshift=0.5cm]current bounding box.north east);
   \node[anchor=south] at (current bounding box.north) {Types of numbers};
   \end{tikzpicture}

   \end{document}

相关内容