在分隔圈内书写

在分隔圈内书写

在这个地址在 tikz 中标记圆圈内的点 你可以看到将一个圆分成x个单元格的代码。如何在被划分的圆的每个单元格中写入一个短语?

答案1

如果我们采用汤姆针对您所链接的问题的解决方案,我们可以轻松地扩展它来实现您想要的效果。

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{intersections, calc, fpu, decorations.pathreplacing}

\newcommand{\TikZFractionalCake}[5]{% Num, Denom, Color, Borders, Size
    \pgfmathsetmacro{\angle}{360/#2};%
    \foreach \x in {1,...,#1}%
    {   \pgfmathsetmacro{\lox}{\x-1}%
        \filldraw[draw=#4,fill=#3] (0,0) -- (\angle*\lox:#5) arc (\angle*\lox:\angle*\x:#5) -- cycle;%
        \pgfmathsetmacro{\mix}{\x-0.5}%
        \node[rotate=\mix*\angle] at (\mix*\angle:#5*0.5+.3) {phrase};
        \node[rotate=\mix*\angle] at (\mix*\angle:#5+0.3) {\x};
    }
}   

\begin{document}

\begin{tikzpicture}
\TikZFractionalCake{20}{20}{white}{black}{3}
\end{tikzpicture}

\end{document}

我们使用完全相同的结构将数字放在末尾,以便在切片中绘制一些文本,因此我们将节点放置在圆弧的末尾,而不是圆心和圆弧之间的中间位置。只需添加这一行:

\node[rotate=\mix*\angle] at (\mix*\angle:#5*0.5+.3) {phrase};

您可以尝试旋转和精确定位来获得您想要的结果。

输出如下所示:

带有短语的饼

更新 如果要添加不同的短语,则需要对代码进行一些修改,因为现在需要在 for 循环中跟踪两个内容(计数和短语)。您可以按如下方式执行此操作:

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{intersections, calc, fpu, decorations.pathreplacing}

\newcommand{\TikZFractionalCake}[5]{% Num, Denom, Color, Borders, Size
    \pgfmathsetmacro{\angle}{360/#2};%
    \foreach [count=\x] \p in {list, of, different, words, that, should, be, enough, to, fill, the, pieces, of, the, pie, else, it, stops, early, {:(}}%
    {   \pgfmathsetmacro{\lox}{\x-1}%
        \filldraw[draw=#4,fill=#3] (0,0) -- (\angle*\lox:#5) arc (\angle*\lox:\angle*\x:#5) -- cycle;%
        \pgfmathsetmacro{\mix}{\x-0.5}%
        \node[rotate=\mix*\angle] at (\mix*\angle:#5*0.5+.3) {phrase};
        \node[rotate=\mix*\angle] at (\mix*\angle:#5+0.3) {\x};
    }
}   

\begin{document}

\begin{tikzpicture}
\TikZFractionalCake{20}{20}{white}{black}{3}
\end{tikzpicture}

\end{document}

看起来像这样:

带有不同短语的馅饼

答案2

以下是修改后的输出我的答案在每个圆段中打印一个项目。以下代码使用纯pstricks解决方案与psforeachpstricks提供一个内部列表计数器\psLoopIndex,用于识别当前索引项:

在此处输入图片描述

\documentclass{article}
\usepackage{pst-node}% http://ctan.org/pkg/pst-node
\usepackage{multido}% http://ctan.org/pkg/multido
\begin{document}

\begin{pspicture}(10,10)
  \SpecialCoor
  \psset{unit=3cm,runit=3cm}% Scaling of x,y and r units
  \pnode(3,0){O}% Circle origin
  \pscircle(O){1}% Outer circle
  \degrees[20]% 20 angles per 360 degrees (each angle is 18 degrees)
  \rput(O){\multido{\i=1+1}{20}{% Cycle through 20 angles and relocate relative to circle origin
    \pcline(O)(1;\i)% Print line from origin to circle edge
    \uput{5pt}[\i]{\i}(1;\i){\i}% Print label with rotation
    }%
    \psforeach{\nA}{here,is,some,text,that,is,placed,around,the,circle,%
      in,a,clockwise,fashion,right,to,the,very,last,segment}{%
        \rput{0.5}{\rput[cC]{-\the\psLoopIndex}(0.7;-\the\psLoopIndex){\nA}}%
    }% Okay, actually it is counter-clockwise, but that's too large a word...
  }
\end{pspicture}
\end{document}

\docsvlist下面的代码使用了etoolbox包裹。它需要重新定义列表项解析宏\do,但产生相同的输出。

\documentclass{article}
\usepackage{pst-node}% http://ctan.org/pkg/pst-node
\usepackage{multido}% http://ctan.org/pkg/multido
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\begin{document}

\begin{pspicture}(10,10)
  \SpecialCoor
  \psset{unit=3cm,runit=3cm}% Scaling of x,y and r units
  \pnode(3,0){O}% Circle origin
  \pscircle(O){1}% Outer circle
  \degrees[20]% 20 angles per 360 degrees (each angle is 18 degrees)
  \rput(O){\multido{\i=1+1}{20}{% Cycle through 20 angles and relocate relative to circle origin
    \pcline(O)(1;\i)% Print line from origin to circle edge
    \uput{5pt}[\i]{\i}(1;\i){\i}% Print label with rotation
    }%
    \newcounter{itemlist}%
    \renewcommand*{\do}[1]{%
      \rput{0.5}{\rput[cC]{-\theitemlist}(0.7;-\theitemlist){#1}}%
      \stepcounter{itemlist}% move to next item
    }
    \docsvlist{here,is,some,text,that,is,placed,around,the,circle,%
      in,a,clockwise,fashion,right,to,the,very,last,segment}% Okay, actually it is counter-clockwise, but that's too large a word...
  }
\end{pspicture}
\end{document}

答案3

@wh1t3 肯定更快(一点点);)并且我的解决方案非常相似(我也基于问题链接中发布的代码构建),但我的不同之处在于你可以将文本数组作为参数传递:

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{intersections, calc, fpu, decorations.pathreplacing}

\newcommand{\TikZFractionalCake}[6]{% Num, Denom, Color, Borders, Size, Text array
\pgfmathsetmacro{\angle}{360/#2};%
\foreach[count=\x] \mytext in {#6}%
{   \pgfmathsetmacro{\lox}{\x-1}%
    \filldraw[draw=#4,fill=#3] (0,0) -- (\angle*\lox:#5) arc (\angle*\lox:\angle*\x:#5) -- cycle;%
    \pgfmathsetmacro{\mix}{\x-0.5}%
    \node[rotate=\mix*\angle] at (\mix*\angle:#5*0.5+0.3) {\mytext};
}
}   

\begin{document}

\begin{tikzpicture}
\TikZFractionalCake{10}{10}{white}{black}{3}{{some},{text},{you},{wanted},{to},{insert},{7},{8},{9},{10}}
\end{tikzpicture}

\end{document}

您可能不认为括号之间必须包含文本数组元素(以逗号分隔),除非它们包含逗号。此外,如果文本元素的数量小于实际的饼图切片数量,则不会绘制“空”切片。

在此处输入图片描述

编辑:为了根据饼图的大小换行文本,\foreach可以将循环的最后一行修改为:

\node[text width=#5cm*.9,align=center,rotate=\mix*\angle] at (\mix*\angle:#5*0.5+0.3) {\mytext};

插入乘数*.9是为了避免文本挤入饼图切片的中心。(这当然不会纠正切片物理上太小而无法挤入文本的情况。)

相关内容