# TikZ-ifthen: decision making in a \foreach statement

## Disclaimer

I am not a programmer so I may not use the proper terminology to express myself. Please correct me if need be.

## Preamble

I'm trying to draw a sinusoidal function and some arrows at specific points (positioned on the reference abscissa for the function) whose length is given by the value of the function at that point. At each point I would also draw a small circle to highlight the point itself.

## Problem Statement

In order to implement that, I would like to use a \foreach statement. However, if I draw the arrows at points in which the function has zero value, I still get an arrowhead -- just to be clear, I don't want that.

To avoid this, I tried to use the \ifthen package, imposing something like

"if sin(x) is not 0, then draw the arrow..."

but in all cases draw the (filled) circle

but I'm getting an error.

Could someone help me?

\documentclass{article}
\usepackage{tikz,ifthen}
\begin{document}
What I have now:

\begin{tikzpicture}[>=latex]
\draw [thick,dashed] plot [domain=-2*pi:2*pi,samples=100] (\x,{sin(\x r)});
\foreach [evaluate={\y=\x*pi;\z=sin(\y r);}] \x in {-2,-1.5,...,2} {
\fill (\y,0) circle (1.5pt);
%\ifthenelse{\NOT \z=0}{\draw [->,thick] (\y,0) -- +({-sin(\y r)},0);}{};
\draw [->,thick] (\y,0) -- +({-sin(\y r)},0);
}
\end{tikzpicture}

What I am trying to achieve:

\begin{tikzpicture}[>=latex]
\draw [thick,dashed] plot [domain=-2*pi:2*pi,samples=100] (\x,{sin(\x r)});
\foreach [evaluate={\y=\x*pi;}] \x in {-1.5,-0.5,...,1.5} {
\fill (\y,0) circle (1.5pt);
\draw [->,thick] (\y,0) -- +({-sin(\y r)},0);
}
\foreach [evaluate={\y=\x*pi;}] \x in {-2,...,2}
\fill (\y,0) circle (1.5pt);
\end{tikzpicture}
\end{document}

The problem is basically the same as in \ifnum and pgfmath: error I think. \z will not be an integer, so the comparison fails. You can make \z an integer by using \z=int(ceil(abs(sin(\y r)))). int makes it an integer. ceil and abs is necessary to avoid rounding to zero.

\documentclass{article}
\usepackage{tikz,ifthen}
\begin{document}
What I have now:

\begin{tikzpicture}[>=latex]
\draw [thick,dashed] plot [domain=-2*pi:2*pi,samples=100] (\x,{sin(\x r)});
\foreach [evaluate={\y=\x*pi;\z=int(ceil(abs(sin(\y r))));}] \x in {-2,-1.5,...,2} {
\fill (\y,0) circle (1.5pt);
\ifthenelse{\NOT\z=0}{\draw [->,thick] (\y,0) -- +({-sin(\y r)},0);}{};
}
\end{tikzpicture}

What I am trying to achieve:

\begin{tikzpicture}[>=latex]
\draw [thick,dashed] plot [domain=-2*pi:2*pi,samples=100] (\x,{sin(\x r)});
\foreach [evaluate={\y=\x*pi;}] \x in {-1.5,-0.5,...,1.5} {
\fill (\y,0) circle (1.5pt);
\draw [->,thick] (\y,0) -- +({-sin(\y r)},0);
}
\foreach [evaluate={\y=\x*pi;}] \x in {-2,...,2}
\fill (\y,0) circle (1.5pt);
\end{tikzpicture}
\end{document}