Ifnum 操作

Ifnum 操作

我已经在我的代码中写入

\newcommand \no {#1} % #1 is a number
\ifnum\i<\no....etc...\else....\fi.

但我需要将 #1 乘以 3,然后对另一个数字求和。我试过了,\ifnum\i < {2*#2+5}但没有成功。正确的语法是什么?提前谢谢。

\begin{tikzpicture} %not working
\foreach \i in {1,...,21}
\fill[\ifnum\i<{3*#2-1}\acommand\else\anothercommand\fi] (90-360/21*\i:.55cm) circle [x radius=1mm, y radius=.5mm, rotate=90-360/21*\i];
\node (0,0) {\LARGE\sffamily\bf \textsf{#2}};
\end{tikzpicture}

答案1

您需要使用\numexpr <expression> \relax来评估上下文中的整数表达式\ifnum。但请注意,\numexpr当涉及到除法时,它实际上并不执行整数算术。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\newcommand\acommand{blue}
\newcommand\anothercommand{red}
\newcommand\WhyWouldAnybodyUseATikzpictureInsideACommand[2]{
  \begin{tikzpicture}
    \foreach \i in {1,...,21} {
      \fill[\ifnum\i<\numexpr3*#2-1\relax \acommand\else\anothercommand\fi] (90-360/21*\i:.55cm) circle [x radius=1mm, y radius=.5mm, rotate=90-360/21*\i];
    }
    \node (0,0) {\LARGE\sffamily\bf \textsf{#2}};
  \end{tikzpicture}
}
\WhyWouldAnybodyUseATikzpictureInsideACommand{unused}{5}
\end{document}

在此处输入图片描述

相关内容