ifnum 没有正确评估变量

ifnum 没有正确评估变量

我正在尝试创建一个 ifnum 语句来绘制多条线段,并且最后一条线段必须是不同的样式。这是我的 MWE:

\documentclass[border=3pt,tikz]{standalone}
\let\oldvec\vec
\usepackage{amsmath} % for \text
\usepackage{tikz}
\usepackage{comment}
\tikzset{>=latex} % for LaTeX arrow head
\usepackage{xcolor}
\colorlet{myblue}{black!40!blue}
\colorlet{myred}{black!40!red}

\providecommand{\sin}{} \renewcommand{\sin}{\hspace{2pt}\mathrm{sen}}

\begin{document}

% Adaptado de: https://wiki.physik.uzh.ch/cms/latex:tikz:electromagnetic_wave
% Electromagnetic wave - colored
\begin{tikzpicture}[x=(-15:1.0), y=(90:1.0), z=(-150:1.0), % rotações/distorções nos eixos, na forma polar
                    line cap=round, line join=round,
                    axis/.style={black, thick,->},
                    vector/.style={>=stealth,->}]
  \large
  \def\A{2} % Amplitude
  \def\nNodes{5} % Número de semi-ciclos
  \def\nVectorsPerNode{8} % Quantidade de vetores por semi-ciclo
  \def\N{\nNodes*40} % Quantidade de pontos a serem calculados para cada semi-ciclo
  \def\xmax{\nNodes*pi/2*1.01} % Valor máximo do eixo x
  \pgfmathsetmacro\nVectors{(\nVectorsPerNode+1)*\nNodes}

\def\drawXaxis{
  \ifnum \value{\iOffset}=\value{\nNodes - 1}
    \draw[axis, red] ({(\iOffset)*pi/2},0,0) -- ({(\iOffset+1)*pi/2},0,0);
  \else
    \draw[thick, red] ({(\iOffset)*pi/2},0,0) -- ({(\iOffset+1)*pi/2},0,0);
  \fi
}

% Desenha os eixos principais
  %\draw (\xmax,0,0) node[right] {$x$};
  %\draw[axis] (0,0,0) -- ++(\xmax*1.1,0,0) node[right] {$x$};
  \draw[axis] (0,-\A*1.4,0) -- (0,\A*1.4,0) node[right] {$y$};
  \draw[axis] (0,0,-\A*1.4) -- (0,0,\A*1.4) node[above left] {$z$};

  \draw[axis] (\xmax+4 , -\A*1.4 , 0) -- (\xmax+4 , \A*1.4 , 0) node[right] {$y$};
  \draw[axis] (\xmax+4 , 0 , -\A*1.4) -- (\xmax+4 , 0 , \A*1.4) node[above left] {$z$};


\def\iOffset{0}
\drawXaxis
\def\iOffset{2}
\drawXaxis
\def\iOffset{4}
\drawXaxis

\end{tikzpicture}

\end{document}

问题是,它永远无法检测何时发生\iOffset} = \nNodes - 1,并且绘图总是采用最后一段的风格。有问题的片段是红色的。结果如下: 在此处输入图片描述

前两个不应该有箭头,只有最后一个才应该有箭头。有人能解释一下这个问题吗?

如果没有 \ifnum,循环可以正常工作,但它只显示一种样式。

编辑:

非常感谢 Steven B. Segletes!

因此,我设法将其添加到我的代码中,该代码围绕在波形上方或下方绘制轴。如下所示:

\foreach \iNode [evaluate={\iOffset=\iNode-1;}] in {1,...,\nNodes}{
    \ifodd\iNode \drawENode \drawXaxis
    \else        \drawXaxis \drawENode
    \fi
}

但是,使用 \drawXaxis 如下:

\def\drawXaxis{
  \ifnum \iOffset=\numexpr\nNodes - 1\relax
    \draw[axis] ({(\iOffset)*pi/2},0,0) -- ({(\iOffset+1)*pi/2},0,0) node[right] {$x$};
  \else
    \draw[thick] ({(\iOffset)*pi/2},0,0) -- ({(\iOffset+1)*pi/2},0,0);
  \fi
}

我得到了这个结果,箭头位于第一段上: 在此处输入图片描述

如果我将 \drawXaxis 更改为:

\def\drawXaxis{
  \ifnum \iNode=\numexpr\nNodes\relax
    \draw[axis] ({(\iOffset)*pi/2},0,0) -- ({(\iOffset+1)*pi/2},0,0) node[right] {$x$};
  \else
    \draw[thick] ({(\iOffset)*pi/2},0,0) -- ({(\iOffset+1)*pi/2},0,0);
  \fi
}

我按照我想要的方式获得图像: 在此处输入图片描述

这是为什么?

我尝试通过创建循环来“调试”它:

\foreach \iNode [evaluate={\iOffset=\iNode-1;}] in {1,...,\nNodes}{$\iNode | \iOffset |$}

它给了我:1|0.0|2|1.0|3|2.0|4|3.0|5|4.0| 我注意到 \iOffset 已更改,至少有小数位,而 \nNodes 是一个整数。它现在是浮点数吗?我不明白他们\ifnum \iOffset=\numexpr\nNodes - 1\relax是如何说 0.0 等于 4 的。

答案1

\value不是您想要的。它适用于您的表达式不包含的计数器。要执行所需的整数运算,请使用\numexpr...\relax

请参阅下面的评论以了解更多警告/免责声明。

\documentclass[border=3pt,tikz]{standalone}
\let\oldvec\vec
\usepackage{amsmath} % for \text
\usepackage{tikz}
\usepackage{comment}
\tikzset{>=latex} % for LaTeX arrow head
\usepackage{xcolor}
\colorlet{myblue}{black!40!blue}
\colorlet{myred}{black!40!red}

\providecommand{\sin}{} \renewcommand{\sin}{\hspace{2pt}\mathrm{sen}}

\begin{document}

% Adaptado de: https://wiki.physik.uzh.ch/cms/latex:tikz:electromagnetic_wave
% Electromagnetic wave - colored
\begin{tikzpicture}[x=(-15:1.0), y=(90:1.0), z=(-150:1.0), % rotações/distorções nos eixos, na forma polar
                    line cap=round, line join=round,
                    axis/.style={black, thick,->},
                    vector/.style={>=stealth,->}]
  \large
  \def\A{2} % Amplitude
  \def\nNodes{5} % Número de semi-ciclos
  \def\nVectorsPerNode{8} % Quantidade de vetores por semi-ciclo
  \def\N{\nNodes*40} % Quantidade de pontos a serem calculados para cada semi-ciclo
  \def\xmax{\nNodes*pi/2*1.01} % Valor máximo do eixo x
  \pgfmathsetmacro\nVectors{(\nVectorsPerNode+1)*\nNodes}

\def\drawXaxis{
  \ifnum \iOffset=\numexpr\nNodes - 1\relax
    \draw[axis, red] ({(\iOffset)*pi/2},0,0) -- ({(\iOffset+1)*pi/2},0,0);
  \else
    \draw[thick, red] ({(\iOffset)*pi/2},0,0) -- ({(\iOffset+1)*pi/2},0,0);
  \fi
}

% Desenha os eixos principais
  %\draw (\xmax,0,0) node[right] {$x$};
  %\draw[axis] (0,0,0) -- ++(\xmax*1.1,0,0) node[right] {$x$};
  \draw[axis] (0,-\A*1.4,0) -- (0,\A*1.4,0) node[right] {$y$};
  \draw[axis] (0,0,-\A*1.4) -- (0,0,\A*1.4) node[above left] {$z$};

  \draw[axis] (\xmax+4 , -\A*1.4 , 0) -- (\xmax+4 , \A*1.4 , 0) node[right] {$y$};
  \draw[axis] (\xmax+4 , 0 , -\A*1.4) -- (\xmax+4 , 0 , \A*1.4) node[above left] {$z$};


\def\iOffset{0}
\drawXaxis
\def\iOffset{2}
\drawXaxis
\def\iOffset{4}
\drawXaxis

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容