我正在尝试用红色绘制每 12 个圆圈。目前我有以下代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (\textwidth,0);
\foreach \x in {0,...,24} %
{ %
\if ??
\draw[red](\x\textwidth/10,0) circle (1);
\else
\draw (\x\textwidth/10,0) circle (0.5);
\fi
}
\end{tikzpicture}
\end{document}
我不确定如何测试\x % 12 == 0
。如何在 Latex 中执行此操作?
答案1
另一个版本:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\newcommand\Ncirc{24} % number of circles
\draw (0,0) -- (\textwidth,0);
\foreach [count=\i from 0,evaluate=\i as \y using {int(mod(\i,\Ncirc/2))}] \x in {0,...,\Ncirc} %
{ %
\ifnum\y=0
\draw[red](\x\textwidth/\Ncirc,0) circle (1);
\else
\draw (\x\textwidth/\Ncirc,0) circle (0.5);
\fi
}
\end{tikzpicture}
\end{document}
答案2
正如我在评论中所说,\ifnum \x=12\relax...\else...\fi
可用于确定第 12 次循环。但如果您需要查找 的倍数12
,我在这里使用一个单独的索引cindex
,它会跟踪,但只要达到 ,\x
就会重置为。0
12
\documentclass{article}
\usepackage{tikz}
\newcounter{cindex}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (\textwidth,0);
\setcounter{cindex}{0};
\foreach \x in {0,...,24} %
{%
\stepcounter{cindex};
\ifnum \thecindex=12\relax
\setcounter{cindex}{0};
\draw[red](\x\textwidth/30,0) circle (1);
\else
\draw (\x\textwidth/30,0) circle (0.5);
\fi%
}
\end{tikzpicture}
\end{document}