1

Conditional statement is not working, I'm getting an error that "Missing number treated as zero"

\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}

Diana
  • 1,285
  • 7
  • 12
  • Do the provided answers solve the problem? If yes, then consider to accept one of the answers. If not, then leave a comment that describes the remaining problem. – Dr. Manuel Kuehner Jan 29 '21 at 16:48

2 Answers2

2

ifnum is meant to be used with integer numbers only, for floating point numbers you can use ifdim instead.

\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}

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
2

The command \value is useful only if its argument is the name of a LaTeX counter; thus \value{\angle} is surely wrong.

Besides, \ifnum can only compare integers, not floating point numbers.

You can use a workaround (possibly there are better ones)

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

Note that the true text and false text should not be braced.

Since \pgfmathresult yields 1 when the test returns true and 0 otherwise, the conditional \ifodd will choose the desired branch.

I'm not sure why you're testing for \angle being less than 12, though.

A tip: don't use \def at the outer level; inside the code for \foreach it can be used, because the previous meaning will be restored at the end of each cycle.

Full code:

\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.6sin(deg(\xpi))}); \draw[dashed] plot[samples at={0,1,...,\ang}] (\x,{2.625+1.6sin(deg(\xpi/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.6sin(deg(\anglepi))}){}; \node (n3) [dot, color=myblue] at(\angle,{2.625+1.6sin(deg(\anglepi/2))}){}; \node (n3) [dot, color=myblue] at(\angg,2.625){}; \fi
\end{tikzpicture} }

\end{document}

egreg
  • 1,121,712