条件语句不起作用

条件语句不起作用

条件语句不起作用,我收到一条错误消息“缺失数字被视为零”

\documentclass[border=10pt,tikz]{standalone}
\usepackage{xcolor}
\definecolor{lava}{rgb}{0.81, 0.06, 0.13}
\definecolor{myblue}{rgb}{0.0, 0.30, 0.60}   
\usepackage    {ifthen}
\usepackage    {tikz}
\usetikzlibrary{calc}
\definecolor{metallicBlue}{RGB}{44,88,128}
\tikzstyle arrowstyle=[scale=1]
\usetikzlibrary{arrows,shapes,positioning}
\usetikzlibrary{angles, arrows.meta,quotes} 
\usepackage{pgfplots,tikz} 
\usetikzlibrary{angles, arrows.meta,quotes} 
\usepackage{pgfplots,tikz}
\usetikzlibrary{decorations.markings}


\begin{document}
\def\t{1}
    \foreach \angle in {0,0.5,...,11}
    {
    
       \begin{tikzpicture}[dot/.style = {circle, fill, minimum size=4pt,inner sep=0pt},arr/.style = {thick,cap=round, -{Straight Barb[angle=45:4pt 3]}}]
       \pgfmathtruncatemacro{\ang}{\angle+1};
       \pgfmathtruncatemacro{\angg}{\angle+2};
       \ifnum\value{\angle} <12{
       \fill[metallicBlue] (0,0) rectangle (10.5,\t);
       \fill[myblue!20!white] (0,\t) rectangle (10.5,4.25);
       \draw plot[samples at={0,.5,...,\angle}] (\x,{2.625+1.6*sin(deg(\x*pi))});
       \draw[dashed] plot[samples at={0,1,...,\ang}] (\x,{2.625+1.6*sin(deg(\x*pi/2))});
       \draw[lava] (0,2.625)--(\angg,2.625);
       \fill[metallicBlue] (0,4.25) rectangle (10.5,4.25+\t);
       \node(d)[]at(11.5,0){};
       \node[] at(5.5,0.5){$n_2$};
       \node[] at(5.5,2.5){$n_1$};}
       else
      { \node (n3) [dot, color=myblue] at(\angle,{2.625+1.6*sin(deg(\angle*pi))}){};
       \node (n3) [dot, color=myblue] at(\angle,{2.625+1.6*sin(deg(\angle*pi/2))}){};
       \node (n3) [dot, color=myblue] at(\angg,2.625){};}
        \fi  
       \end{tikzpicture}}

\end{document}

答案1

ifnum仅用于整数,对于浮点数ifdim也可以使用。

\documentclass[border=10pt,tikz]{standalone}
\usepackage{xcolor}
\definecolor{lava}{rgb}{0.81, 0.06, 0.13}
\definecolor{myblue}{rgb}{0.0, 0.30, 0.60}   
\usepackage    {ifthen}
\usepackage    {tikz}
\usetikzlibrary{calc}
\definecolor{metallicBlue}{RGB}{44,88,128}
\tikzstyle arrowstyle=[scale=1]
\usetikzlibrary{arrows,shapes,positioning}
\usetikzlibrary{angles, arrows.meta,quotes} 
\usepackage{pgfplots,tikz} 
\usetikzlibrary{angles, arrows.meta,quotes} 
\usepackage{pgfplots,tikz}
\usetikzlibrary{decorations.markings}


\begin{document}
\def\t{1}
    \foreach \angle in {0,0.5,...,11}
    {
    
       \begin{tikzpicture}[dot/.style = {circle, fill, minimum size=4pt,inner sep=0pt},arr/.style = {thick,cap=round, -{Straight Barb[angle=45:4pt 3]}}]
       \pgfmathtruncatemacro{\ang}{\angle+1};
       \pgfmathtruncatemacro{\angg}{\angle+2};
       \ifdim\angle pt <12.0pt{
       \fill[metallicBlue] (0,0) rectangle (10.5,\t);
       \fill[myblue!20!white] (0,\t) rectangle (10.5,4.25);
       \draw plot[samples at={0,.5,...,\angle}] (\x,{2.625+1.6*sin(deg(\x*pi))});
       \draw[dashed] plot[samples at={0,1,...,\ang}] (\x,{2.625+1.6*sin(deg(\x*pi/2))});
       \draw[lava] (0,2.625)--(\angg,2.625);
       \fill[metallicBlue] (0,4.25) rectangle (10.5,4.25+\t);
       \node(d)[]at(11.5,0){};
       \node[] at(5.5,0.5){$n_2$};
       \node[] at(5.5,2.5){$n_1$};}
       else
      { \node (n3) [dot, color=myblue] at(\angle,{2.625+1.6*sin(deg(\angle*pi))}){};
       \node (n3) [dot, color=myblue] at(\angle,{2.625+1.6*sin(deg(\angle*pi/2))}){};
       \node (n3) [dot, color=myblue] at(\angg,2.625){};}
        \fi  
       \end{tikzpicture}}

\end{document}

答案2

该命令\value仅当其参数是 LaTeX 计数器的名称时才有用;因此\value{\angle}肯定是错误的。

此外,\ifnum只能比较整数,不能比较浮点数。

你可以使用解决方法(可能有更好的方法)

\pgfmathparse{ifthenelse(\angle<12,1,0)}
\ifodd\pgfmathresult\relax
  <true text>
\else
  <false text>
\fi

注意真文与假文之间不要加括号。

由于\pgfmathresult当测试返回真时产生 1,否则产生 0,因此条件\ifodd将选择所需的分支。

不过,我不确定你为什么要测试\angle小于 12。

提示:不要\def在外层使用;\foreach可以在代码内部使用,因为每次循环结束时都会恢复以前的含义。

完整代码:

\documentclass[border=10pt,tikz]{standalone}
\usepackage{xcolor}
\usepackage{ifthen}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{calc}
\usetikzlibrary{arrows,shapes,positioning}
\usetikzlibrary{angles, arrows.meta,quotes} 
\usetikzlibrary{decorations.markings}

\definecolor{lava}{rgb}{0.81, 0.06, 0.13}
\definecolor{myblue}{rgb}{0.0, 0.30, 0.60}   
\definecolor{metallicBlue}{RGB}{44,88,128}

\tikzset{
  arrowstyle/.style={scale=1},
}

\begin{document}

\foreach \angle in {0,0.5,...,11}{
  \def\t{1}
  \begin{tikzpicture}[
    dot/.style = {circle, fill, minimum size=4pt,inner sep=0pt},
    arr/.style = {thick,cap=round, -{Straight Barb[angle=45:4pt 3]}}
  ]
  \pgfmathtruncatemacro{\ang}{\angle+1};
  \pgfmathtruncatemacro{\angg}{\angle+2};
  \pgfmathparse{ifthenelse(\angle<12,1,0)}
  \ifodd\pgfmathresult\relax
    \fill[metallicBlue] (0,0) rectangle (10.5,\t);
    \fill[myblue!20!white] (0,\t) rectangle (10.5,4.25);
    \draw plot[samples at={0,.5,...,\angle}] (\x,{2.625+1.6*sin(deg(\x*pi))});
    \draw[dashed] plot[samples at={0,1,...,\ang}] (\x,{2.625+1.6*sin(deg(\x*pi/2))});
    \draw[lava] (0,2.625)--(\angg,2.625);
    \fill[metallicBlue] (0,4.25) rectangle (10.5,4.25+\t);
    \node(d)[]at(11.5,0){};
    \node[] at(5.5,0.5){$n_2$};
    \node[] at(5.5,2.5){$n_1$};
  \else
    \node (n3) [dot, color=myblue] at(\angle,{2.625+1.6*sin(deg(\angle*pi))}){};
    \node (n3) [dot, color=myblue] at(\angle,{2.625+1.6*sin(deg(\angle*pi/2))}){};
    \node (n3) [dot, color=myblue] at(\angg,2.625){};
  \fi  
  \end{tikzpicture}
}

\end{document}

相关内容