如何设置节点的精确半径?

如何设置节点的精确半径?

我有一堆节点,我想调整它们的大小,使它们的面积代表我拥有的一些数字(这样,如果我有两个节点,其对应值分别为 1 和 2,则第二个节点的面积应该是第一个节点的两倍)。我尝试使用minimum size和的某种组合来实现这一点inner sep,但我注意到,如果我设置minimum size0pt,则具有 的节点inner sep = 2pt的面积不会是具有 的节点的两倍inner sep = 1pt

答案1

对于节点,您可以设置inner sep=0pt然后使用minimum size(如果节点文本为空或短于声明的大小)来控制区域:

\documentclass{article} 
\usepackage{tikz} 

\begin{document} 

\begin{tikzpicture}[every node/.style={draw=blue,thick,circle,inner sep=0pt}]
  \draw[help lines] (-3,-3) grid (3,3);
  \node[minimum size=2cm] (0,0)  {};
  \node[minimum size=2.828cm] (0,0)  {};
  \node[minimum size=4cm] (0,0)  {};
\end{tikzpicture} 

\end{document}

在此处输入图片描述

如果要使大小保持固定,而不依赖于节点文本,则可以设置text width(也可能是text height):

\documentclass{article} 
\usepackage{tikz} 

\begin{document} 

\begin{tikzpicture}[every node/.style={draw=blue,thick,circle,inner sep=0pt}]
  \draw[help lines] (-3,-3) grid (3,3);
  \node[text width=2cm] (0,0)  {};
  \node[text width=2.828cm] (0,0)  {};
  \node[text width=4cm] (0,0)  {};
\end{tikzpicture} 

\end{document}

正如 Andrew Stacey 所说,您可以使用形状代替节点,这样就可以轻松控制形状属性;以下是使用该circle操作的相同三个圆圈:

\documentclass{article} 
\usepackage{tikz} 

\begin{document} 

\begin{tikzpicture}[every node/.style={draw=blue,thick,circle,inner sep=0pt}]
  \draw[help lines] (-3,-3) grid (3,3);
  \draw[blue,thick] (0,0) circle [radius=1cm] ;
  \draw[blue,thick] (0,0) circle [radius=1.414cm] ;
  \draw[blue,thick] (0,0) circle [radius=2cm] ;
\end{tikzpicture} 

\end{document}

答案2

我认为以下应该起作用:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\begin{document}

\pgfmathsetmacro{\nodebasesize}{1} % A node with a value of one will have this diameter
\pgfmathsetmacro{\nodeinnersep}{0.1}

\newcommand{\propnode}[5]{% position, name, options, value, label
    \pgfmathsetmacro{\minimalwidth}{sqrt(#4*\nodebasesize)}
    \node[#3,minimum width=\minimalwidth*1cm,inner sep=\nodeinnersep*1cm,circle,draw] (#2) at (#1) {#5};
}

\begin{tikzpicture}
    \draw[<->] (2,-0.5) --  node[right] {$r=\sqrt{1} \Rightarrow A=\pi(\sqrt{1})^2=\pi$} (2,0.5);
    \draw[gray] (2,-0.5) -- (0,-0.5);
    \draw[gray] (2,0.5) -- (0,0.5);
    \propnode{0,0}{n1}{fill=red,text=blue}{1}{1}

    \draw[<->] (2,2-0.707) -- node[right] {$r=\sqrt{2} \Rightarrow A=\pi(\sqrt{2})^2=2\pi$} (2,2+0.707);
    \draw[gray] (2,2-0.707) -- (0,2-0.707);
    \draw[gray] (2,2+0.707) -- (0,2+0.707); 
    \propnode{0,2}{n2}{fill=green,text=black}{2}{2}

    \draw[<->] (2,4-0.866) -- node[right] {$r=\sqrt{3} \Rightarrow A=\pi(\sqrt{3})^2=3\pi$} (2,4+0.866);
    \draw[gray] (2,4-0.866) -- (0,4-0.866);
    \draw[gray] (2,4+0.866) -- (0,4+0.866); 
    \propnode{0,4}{n3}{fill=yellow,text=violet}{3}{3}

    \draw[<->] (11,1) -- node[right] {$r=\sqrt{9} \Rightarrow A=\pi(\sqrt{9})^2=9\pi$} (11,4);
    \draw[gray] (11,1) -- (9,1);
    \draw[gray] (11,4) -- (9,4);    
    \propnode{9,2.5}{n9}{fill=black,text=white}{9}{9}

    \draw[<->] (11,-0.354) -- node[right] {$r=\sqrt{0.5} \Rightarrow A=\pi(\sqrt{0.5})^2=0.5\pi$} (11,0.354);
    \draw[gray] (11,-0.354) -- (9,-0.354);
    \draw[gray] (11,0.354) -- (9,0.354);    
    \propnode{9,0}{n05}{fill=pink,text=black}{0.5}{0.5}
    \draw[ultra thick,red] (8.5,-0.5) -- (11.5,-0.5);
\end{tikzpicture}\\[2cm]

\pgfmathsetmacro{\nodebasesize}{1} % A node with a value of one will have this diameter
\pgfmathsetmacro{\nodeinnersep}{0.0}

\begin{tikzpicture}
    \draw[<->] (2,-0.5) --  node[right] {$r=\sqrt{1} \Rightarrow A=\pi(\sqrt{1})^2=\pi$} (2,0.5);
    \draw[gray] (2,-0.5) -- (0,-0.5);
    \draw[gray] (2,0.5) -- (0,0.5);
    \propnode{0,0}{n1}{fill=red,text=blue}{1}{1}

    \draw[<->] (2,2-0.707) -- node[right] {$r=\sqrt{2} \Rightarrow A=\pi(\sqrt{2})^2=2\pi$} (2,2+0.707);
    \draw[gray] (2,2-0.707) -- (0,2-0.707);
    \draw[gray] (2,2+0.707) -- (0,2+0.707); 
    \propnode{0,2}{n2}{fill=green,text=black}{2}{2}

    \draw[<->] (2,4-0.866) -- node[right] {$r=\sqrt{3} \Rightarrow A=\pi(\sqrt{3})^2=3\pi$} (2,4+0.866);
    \draw[gray] (2,4-0.866) -- (0,4-0.866);
    \draw[gray] (2,4+0.866) -- (0,4+0.866); 
    \propnode{0,4}{n3}{fill=yellow,text=violet}{3}{3}

    \draw[<->] (11,1) -- node[right] {$r=\sqrt{9} \Rightarrow A=\pi(\sqrt{9})^2=9\pi$} (11,4);
    \draw[gray] (11,1) -- (9,1);
    \draw[gray] (11,4) -- (9,4);    
    \propnode{9,2.5}{n9}{fill=black,text=white}{9}{9}

    \draw[<->] (11,-0.354) -- node[right] {$r=\sqrt{0.5} \Rightarrow A=\pi(\sqrt{0.5})^2=0.5\pi$} (11,0.354);
    \draw[gray] (11,-0.354) -- (9,-0.354);
    \draw[gray] (11,0.354) -- (9,0.354);    
    \propnode{9,0}{n05}{fill=pink,text=black}{0.5}{0.5}
\end{tikzpicture}\\[2cm]

\pgfmathsetmacro{\nodebasesize}{1.5} % A node with a value of one will have this diameter
\pgfmathsetmacro{\nodeinnersep}{0.0}

\begin{tikzpicture}
    \propnode{0,0}{n1}{fill=red,text=blue}{1}{1}

    \propnode{0,2}{n2}{fill=green,text=black}{2}{2}

    \propnode{0,4}{n3}{fill=yellow,text=violet}{3}{3}

    \propnode{9,2.5}{n9}{fill=black,text=white}{9}{9}

    \propnode{9,0}{n05}{fill=pink,text=black}{0.5}{0.5}
\end{tikzpicture}

\end{document}

示例 1:如果节点直径变得太小,则节点将变得太大(参见红色下划线)

在此处输入图片描述

示例 2:如果发生这种情况,您可以减少inner sep

在此处输入图片描述

示例 3:这里这有帮助,但如果仍然不够,您可以增加base size节点数:

在此处输入图片描述



编辑1:我添加了自动绘制控制线以及影响节点字体大小的选项,因此您应该能够轻松选择合适的设置:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xifthen}

\begin{document}

\pgfmathsetmacro{\nodebasesize}{1} % A node with a value of one will have this diameter
\pgfmathsetmacro{\nodeinnersep}{0.1}

\newcommand{\propnode}[7]{% position, name, options, value, label, show control lines (s for show), font size 
    \pgfmathsetmacro{\minimalwidth}{sqrt(#4*\nodebasesize)}
    \node[#3,minimum width=\minimalwidth*1cm,inner sep=\nodeinnersep*1cm,circle,draw] (#2) at (#1) {#7 #5};
    \ifthenelse{\equal{#6}{s}}
        {   \draw[gray] ($(#1)+(0,\minimalwidth/2)$) -- ($(#1)+(\minimalwidth/2+1,\minimalwidth/2)$);
            \draw[gray] ($(#1)+(0,-\minimalwidth/2)$) -- ($(#1)+(\minimalwidth/2+1,-\minimalwidth/2)$);
            \draw[very thick,<->] ($(#1)+(\minimalwidth/2+1,\minimalwidth/2)$) -- ($(#1)+(\minimalwidth/2+1,-\minimalwidth/2)$);
        }
        {}
}

\begin{tikzpicture}
    \propnode{0,0}{n1}{fill=red,text=blue}{1}{1}{s}{}
    \propnode{0,2}{n2}{fill=green,text=black}{2}{2}{s}{}
    \propnode{0,4}{n3}{fill=yellow,text=violet}{3}{3}{s}{}
    \propnode{9,2.5}{n9}{fill=black,text=white}{9}{9}{s}{}
    \propnode{9,0}{n05}{fill=pink,text=black}{0.5}{0.5}{s}{}
\end{tikzpicture}\\[2cm]

\pgfmathsetmacro{\nodebasesize}{0.5} 
\pgfmathsetmacro{\nodeinnersep}{0.2}

\begin{tikzpicture}
    \propnode{0,0}{n1}{fill=red,text=blue}{1}{1}{s}{\tiny}
    \propnode{0,2}{n2}{fill=green,text=black}{2}{2}{s}{\tiny}
    \propnode{0,4}{n3}{fill=yellow,text=violet}{3}{3}{s}{\tiny}
    \propnode{9,2.5}{n9}{fill=black,text=white}{9}{9}{s}{\tiny}
    \propnode{9,0}{n05}{fill=pink,text=black}{0.5}{0.5}{s}{\tiny}
\end{tikzpicture}\\[2cm]

\pgfmathsetmacro{\nodebasesize}{0.2} 
\pgfmathsetmacro{\nodeinnersep}{0}

\begin{tikzpicture}
    \propnode{0,0}{n1}{fill=red,text=blue}{1}{1}{s}{\scriptsize}
    \propnode{0,2}{n2}{fill=green,text=black}{2}{2}{s}{\scriptsize}
    \propnode{0,4}{n3}{fill=yellow,text=violet}{3}{3}{s}{\scriptsize}
    \propnode{9,2.5}{n9}{fill=black,text=white}{9}{9}{s}{\scriptsize}
    \propnode{9,0}{n05}{fill=pink,text=black}{0.5}{0.5}{s}{\scriptsize}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

更新

如果圆节点的面积代表一些具有比例的数字,那么您需要准确知道半径。半径取决于minimum宽度和\pgflinewidth

  we have : radius = (minimum width + line width) / 2  if inner sep = 0pt

在下一个例子中,我在所有情况下选择 first minimum width=2cmthen minimum width=2cm,line width=5mmand finally 。line width=5mm,minimum width=2cm-\pgflinewidthinner sep= 0 pt

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}  

\begin{document} 
\begin{tikzpicture} 
  \draw[help lines,step=0.1,,draw=orange] (0,0) grid (8,1); 
  \draw[help lines] (0,0) grid (8,1);     
  \node[minimum width=2cm,circle,inner sep=0pt,fill=blue!20,fill opacity=.5]{};
  \node[minimum width=2cm,circle,inner sep=0pt,fill=blue!20,fill opacity=.5,
        line width=5mm,draw=gray,opacity=.5] at (3,0){}; 
  \node[circle,inner sep=0pt,fill=blue!20,,fill opacity=.5,
        line width=5mm,draw=gray,opacity=.5,minimum width=2cm-\pgflinewidth]  at (6,0) {}; 
\end{tikzpicture}  
\end{document} 

在此处输入图片描述

现在,如果我想得到三个面积分别为 pi、2pi 和 3pi 的圆,我创建了一个宏 `def\lw{2mm}` 来快速更改所有节点中的线宽

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}  

\begin{document} 

\tikzset{myrad/.style 2 args={circle,inner sep=0pt,minimum width=(2*(sqrt(#1)*1 cm ) - \pgflinewidth,fill=#2,draw=#2,fill opacity=.5,opacity=.8}}    

\begin{tikzpicture} 
  \def\lw{2mm}
  \draw[help lines,step=0.1,,draw=orange] (0,0) grid (8,1); 
   \draw[help lines] (0,0) grid (8,1);     
  \node[line width=\lw,myrad={1}{blue!20}]  at (0,0) {1}; 
  \node[line width=\lw,myrad={2}{red!20}]  at (3,0) {2};
  \node[line width=\lw, myrad={3}{green!20}]  at (7,0) {3};   
\end{tikzpicture}  

\end{document}

在此处输入图片描述

最后,如果您想要面积等于 1 cm^2、2 cm^2 和 3 cm^2 的节点:我会更改第二组节点的线宽

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}  

\begin{document} 

\tikzset{myrad/.style 2 args={circle,inner sep=0pt,minimum width=(2*(sqrt(#1/3.1415)*1 cm ) - \pgflinewidth,fill=#2,draw=#2,fill opacity=.5,opacity=.8}}    

\begin{tikzpicture} 
  \def\lw{2mm}
  \draw[help lines,step=0.1,,draw=orange] (0,0) grid (8,1); 
   \draw[help lines] (0,0) grid (8,1);     
  \node[line width=\lw,myrad={1}{blue!20}]  at (0,0) {1}; 
  \node[line width=\lw,myrad={2}{red!20}]  at (3,0) {2};
  \node[line width=\lw, myrad={3}{green!20}]  at (7,0) {3};   
\end{tikzpicture}    

\begin{tikzpicture} 
  \def\lw{5mm}
  \draw[help lines,step=0.1,,draw=orange] (0,0) grid (8,1); 
   \draw[help lines] (0,0) grid (8,1);     
  \node[line width=\lw,myrad={1}{blue!20}]  at (0,0) {1}; 
  \node[line width=\lw,myrad={2}{red!20}]  at (3,0) {2};
  \node[line width=\lw, myrad={3}{green!20}]  at (7,0) {3};   
\end{tikzpicture} 

\end{document} 

在此处输入图片描述

我的第一个答案的更新

为了避免这种问题,我们可以使用圆圈代替圆形节点。但我们需要使用 pgflinewidth 调整半径。在下一个示例中,我想要半径 = 2cm,因此我需要使用:radius=2cm-0.5\pgflinewidth。然后我需要创建一个具有相同尺寸的节点。

就像关于节点和矩形的问题这里,我们可以将一个节点与形状关联起来主要问题:我们不能使用scale但放置标签更容易。

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}  

\begin{document} 

\tikzset{set node/.style={insert path={% 
     \pgfextra{% 
     \node[inner sep=0pt,outer sep = 0pt,draw=black, % draw= none only to show what I do
          circle,
          minimum width=2*\pgfkeysvalueof{/tikz/x radius}+0.5\pgflinewidth](#1) {};
     }}}}

\begin{tikzpicture}
  \draw[help lines] (-3,-3) grid (3,3);
  \draw[blue,line width=5mm,opacity=.2] (0,0) circle [radius=2cm-0.5\pgflinewidth,set node=C1]  ; 
  \draw[thick,->] (3,-3) -- (C1.east);
\end{tikzpicture}
\end{document} 

在此处输入图片描述

相关内容