以下代码打印标签富,酒吧,巴兹, 和積極在圆的交替两侧。这是通过对 中指定的多个变量进行 for 循环来实现的\myInput
。负责这些标签的垂直放置(即它们与中心 [0,0] 的距离)的变量是varC
。
\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\def \CircleIntervals {0,90,...,270}
\def \myInput {
foo/45/2/red,
bar/135/4/green,
baz/225/2/blue,
qux/315/4/brown}
\begin{document}
\begin{tikzpicture}[line cap=rect, line width=3pt]
\draw[] (0,0) circle [radius=3cm];
\foreach \angle [count=\xi] in \CircleIntervals
{
\draw[line width=1.5pt] (\angle:2.9cm) -- (\angle:3.1cm);
\node[] at (\angle:3.5cm) {\xi};
}
\foreach \varA/\varB/\varC/\varD in \myInput {
\node[color=\varD] at (\varB:\varC cm) {\Large{\varA}};
}
\end{tikzpicture}
\end{document}
由于varC
只提供重复模式,我不希望它成为的一部分\myInput
,而是将其硬编码为循环的一部分。
我如何修改第二个循环,使得整数2和4是按连续重复的顺序提供的吗?
答案1
一种或多或少“基本”的方法
\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\def \CircleIntervals {0,90,...,270}
\def \myInput {
foo/45/red,
bar/135/green,
baz/225/blue,
qux/315/brown}
\newif\ifdothis
\def\foostate{%
2%
}
\newcommand{\togglefoostate}{%
\ifdothis
\gdef\foostate{2}
\global\dothisfalse
\else
\gdef\foostate{4}
\global\dothistrue
\fi
}
\begin{document}
\begin{tikzpicture}[line cap=rect, line width=3pt]
\draw[] (0,0) circle [radius=3cm];
\foreach \angle [count=\xi] in \CircleIntervals
{
\draw[line width=1.5pt] (\angle:2.9cm) -- (\angle:3.1cm);
\node[] at (\angle:3.5cm) {\xi};
}
\foreach \varA/\varB/\varD in \myInput {%
\node[color=\varD] at (\varB:\foostate cm) {\Large{\varA}};
\togglefoostate%
}
\end{tikzpicture}
\end{document}
答案2
evaluate
您可以使用 Tikz提供的内置功能count
。因此,我们从开始计数0
,然后我们可以执行
evaluate=\xx as \mymod using {mod(\xx,2)},
结果将在哪里0,1,0,1,0,1,0,...
。之后我们可以做
evaluate=\xx as \varC using int(2*(\mymod+1))
结果将是
int(2*(0+1)) == 2
int(2*(1+1)) == 4
代码
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\def \CircleIntervals {0,90,...,270}
\def \myInput {
foo/45/red,
bar/135/green,
baz/225/blue,
qux/315/brown}
\begin{document}
\begin{tikzpicture}[line cap=rect, line width=3pt]
\draw[] (0,0) circle [radius=3cm];
\foreach \angle [count=\xi] in \CircleIntervals{
\draw[line width=1.5pt] (\angle:2.9cm) -- (\angle:3.1cm);
\node[] at (\angle:3.5cm) {\xi};
}
\foreach \varA/\varB/\varD [
count=\xx starting from 0,
evaluate=\xx as \mymod using {mod(\xx,2)},
evaluate=\xx as \varC using int(2*(\mymod+1))
] in \myInput {%
\node[color=\varD] at (\varB:\varC cm) {\Large{\varA}};
}
\end{tikzpicture}
\end{document}
答案3
在第二个循环中也使用计数,\yy
如果计数为奇数则定义为 2,如果计数为偶数则定义为 4。
\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\newcommand\CircleIntervals {0,90,180,270}
\newcommand\myInput {
foo/45/red,
bar/135/green,
baz/225/blue,
qux/315/brown% <--- or a space would be seen
}
\begin{document}
\begin{tikzpicture}[line cap=rect, line width=3pt]
\draw[] (0,0) circle [radius=3cm];
\foreach \angle [count=\xi] in \CircleIntervals
{
\draw[line width=1.5pt] (\angle:2.9cm) -- (\angle:3.1cm);
\node[] at (\angle:3.5cm) {\xi};
}
\foreach \varA/\varB/\varC [count=\y] in \myInput
{
\edef\yy{\ifodd\y\space 2\else 4\fi}
\node[color=\varC] at (\varB:\yy cm) {\Large\varA};
}
\end{tikzpicture}
\end{document}
奇怪的\space
是用来终止的数字\ifodd
。