作为 tex 和 tikz 的初学者,我坚持使用 foreach 进行数组处理。我想沿圆周绘制两个带有文本的圆圈。
\documentclass[tikz]{standalone}
\usepackage[utf8]{inputenx}
\usetikzlibrary{decorations.text}
\begin{document}
\sffamily
\begin{tikzpicture}[
black,
ultra thick,
planet/.style = {draw,fill,circle,inner sep=#1},
circle label/.style = {
postaction={
decoration={
text along path,
text = {#1},
text align=center,
text color=black,
reverse path,
},
decorate,
}
}
]
\filldraw[white] (-8,-8) rectangle (9,10);
\path[circle label={|\large|Circle one}] (0,-8-.2) arc (-90:360-90:8+.2);
\draw[color=black,fill=gray!15] (0,0) circle (8);
\path[circle label={|\large|Inner Circle}] (0,-4-.2) arc (-90:360-90:4+.2);
\draw[color=black,fill=black!20] (0,0) circle (4);
% Values
\pgfkeys{/90/.code=Point A,
/0/.code=Point B,
/180/.code=Point C,
/270/.code=Point D}
\foreach \angle in {0,90,180,270}
\draw (\angle:2.75) -- (\angle:2.75cm) node {\pgfkeys{\angle}};
\end{tikzpicture}
\end{document}
我知道如何以正确的角度绘制标签。但我想创建一个包含其点的列表,然后自动执行其余操作。
举个例子: 我在 pgfkeys 上添加了一个额外的点(没有角度),并将 360 除以项目数。对于每个循环,下一个项目都会从列表中删除并沿着路径放置。
答案1
下一段代码展示了一个解决方案foreach
。该命令placetext
有两个参数,第一个是要分发的列表labels
,第二个是圆radius
。第一个foreach
计算项目总数,第二个沿圆绘制它们。
\documentclass[tikz]{standalone}
\usepackage[utf8]{inputenx}
\usetikzlibrary{decorations.text}
\newcommand{\placetext}[2]{
\foreach \item [count=\totalitems] in {#1};
\foreach \item [count=\numitem, evaluate=\numitem as \angle using {(\numitem-1)*360/\totalitems}] in {#1}
\draw (\angle:#2) node {\item};
}
\begin{document}
\sffamily
\begin{tikzpicture}[
black,
ultra thick,
planet/.style = {draw,fill,circle,inner sep=#1},
circle label/.style = {
postaction={
decoration={
text along path,
text = {#1},
text align=center,
text color=black,
reverse path,
},
decorate,
}
}
]
\filldraw[white] (-8,-8) rectangle (9,10);
\path[circle label={|\large|Circle one}] (0,-8-.2) arc (-90:360-90:8+.2);
\draw[color=black,fill=gray!15] (0,0) circle (8);
\path[circle label={|\large|Inner Circle}] (0,-4-.2) arc (-90:360-90:4+.2);
\draw[color=black,fill=black!20] (0,0) circle (4);
% Values
%\pgfkeys{/90/.code=Point A,
% /0/.code=Point B,
% /180/.code=Point C,
% /270/.code=Point D}
%
%\foreach \angle in {0,90,180,270}
%\draw (\angle:2.75) -- (\angle:2.75cm) node {\pgfkeys{\angle}};
\placetext{Point A, Point B, Point C, Point D, Point E}{2.75}
\placetext{Point A, Point B, Point C}{6.75}
\end{tikzpicture}
\end{document}