变量的 \ifnum

变量的 \ifnum

我想在 tikzpicture 环境中检查变量是否小于 0。我读到可以用 来完成\ifnum。我的代码简化为

\begin{center}
\begin{animateinline}[loop, poster = first, controls]{30} % frames/minut ?
 %
\whiledo{\theangle<120}{
 %
\begin{tikzpicture}
% Axis
\draw[thick,->] (0, 0)--(-1.5, 2.598076211) node[above] {$q_2$}; % q_2
\draw[thick,->] (0,0)--(3,0) node[right] {$q_0$}; % q_0 
\draw[thick, dashed] (2.5, 0) -- (120:2.5cm); % q_0 

% Circle
\draw [thick,domain=-15:135] plot ({2.5*cos(\x)}, {2.5*sin(\x)});
% \node[red,below] at (2.65,0) {1};

% Arcs
% \draw[ultra thick,cyan] (0,0) -- (0,0 |- \theangle:2.5cm); % UpOn x axis
% \draw[ultra thick,orange] (0,0) -- (\theangle:2.5cm |- 0,0); % UpOn y axis
\draw[red] (0.5,0) arc (0:\theangle:0.5cm) node [right] {$s \theta$}; % arc s \theta
\draw[] (120:0.5cm) arc (120:\theangle:0.5cm) node [right] {}; % $(1-s) \theta$

% SLERP
% \draw[densely dotted,orange] (\theangle:2.5cm) -- (\theangle:2.5cm |- 0,0); % vertical line
% \draw[densely dotted,cyan] (\theangle:2.5cm) -- (0,0 |- \theangle:2.5cm); % horizontal line
\draw[very thick,red,->,rotate=\theangle] (0,0) -- (2.5,0) node[left] {\scriptsize $q_{slerp}$}; 

% LERP
\def\pi{3.14159265535898};
\def\d{\theangle*5*sqrt(3)/(2*120)};
\draw[very thick, blue, ->] (0,0) -- ({2.5 - \d*cos(180*\pi/(6*\pi))},{\d*sin(180*\pi/(6*\pi)))}) node[right] {\scriptsize $q_{lerp}$}; 

\def\lerpx{(2.5 - \d*cos(30))};
\def\lerpy{(\d*sin(30))};
\def\lerpu{(sqrt(\lerpx^2 + \lerpy^2))};   


\def\du{((\theangle-1)*5*sqrt(3)/(2*120))};
\def\lerpxu{(2.5 - \du*cos(30))};
\def\lerpyu{(\du*0.5)};


\node[right] at (-3,-3) 
        {\footnotesize$v_{angle} = \pgfmathparse{\theangle}\pgfmathresult$};  


\newcommand{\auxiliar}{\pgfmathparse{\lerpxu}\pgfmathresult}

\ifnum\lerpxu<0
\node[right] at (0,-3) {\footnotesize$v_{atan} = \pgfmathparse{(atan((\lerpyu)/((\lerpxu)+0.01))+180)}\pgfmathresult$};
\else
\node[right] at (-2,-3) {\footnotesize$v_{atan} = \pgfmathparse{(atan((\lerpyu)/((\lerpxu)+0.01)))}\pgfmathresult$};
\fi



\node[right] at (0,0) 
        {\footnotesize$v_{lerpyu} = \pgfmathparse{\lerpyu}\pgfmathresult$}; 
            \node[right] at (0,-1) 
        {\footnotesize$v_{lerpxu} = \pgfmathparse{\lerpxu}\pgfmathresult$}; 

\end{tikzpicture}
%
\stepcounter{angle}
\ifthenelse{\theangle<120}{
        \newframe
}{
        \end{animateinline}
}
}
\end{center}

这会导致“缺失数字,视为零”错误。如果我插入数字而不是变量,条件会正常工作。我该如何解决这个问题?

答案1

如果没有充分最小工作示例很难说你在做什么,但是,\ifnum不会起作用,因为它比较整数\lerpxu通常不是整数。相反,你可以\pgfmathparse与一起使用\ifnum

\pgfmathparse{\lerpxu<0} %\pgfmathresult is now 0 or 1
\ifnum\pgfmathresult=0 % \lerpxu \ge0
   ...
\else % \lerpxu <0
  ...
\fi

我已将您的(原始) MWE 缩减为以下内容,以展示其工作原理:

\documentclass{article}
\usepackage{tikz}
\newcommand\atan{\mathop{\textrm{atan}}}% define an \atan operator
\begin{document}

  \def\du{((\theangle-1)*5*sqrt(3)/(2*120))}
  \def\lerpxu{(2.5 - \du*cos(30))}
  \def\lerpyu{(\du*0.5)}

  \begin{tikzpicture}
    \foreach \theangle  in {-30,30} {
        \pgfmathparse{\theangle<0}
        \typeout{\pgfmathresult}
        \ifnum\pgfmathresult=0
            \node[right] at (0,0) {\footnotesize$v_{\atan} = \pgfmathparse{(atan((\lerpyu)/((\lerpxu)+0.01))+180)}\pgfmathresult$};
        \else
            \node[right] at (0,1) {\footnotesize$v_{\atan} = \pgfmathparse{(atan((\lerpyu)/((\lerpxu)+0.01)))}\pgfmathresult$};
        \fi
    }
  \end{tikzpicture}

\end{document}

相关内容