# conditional inside a pgfplotsinvokeforeach loop

I'm using \pgfplotsinvokeforeach to do mathematical computations inside the \addplot options. Now I need a conditional to decide whether to plot a line or not depending on the current index of the loop, but I can't do it by means of an \ifthenelse command. Here is just a "pseudo"-example:

\documentclass{standalone}
\usepackage{ifthen}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotsinvokeforeach {0.1,0.2,...,2}
{\ifthenelse{#1<1}{\addplot {0};}{"do nothing"}}
\end{axis}
\end{tikzpicture}
\end{document}


I get "missing =" and "missing number" errors.

The original (misleading) posted code was:

\documentclass{standalone}
\usepackage{ifthen}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotsinvokeforeach {0,...5}
{\ifthenelse{#1<3}{\addplot {0};}{\addplot {1};}}
\end{axis}
\end{tikzpicture}
\end{document}


Thank you

-
Welcome to TeX.SX! Would saying {0,1,...,5} instead of the wrong {0,...5} help? – egreg May 4 '12 at 9:37
egeg, I'm sorry but I don't know how to answer directly to your comment. You're right (I did a mistake in writing the code), but try with \pgfplotsinvokeforeach{0.1,0.2}. I get "missing =" and "missing number" errors. I need to handle both natural and rational numbers. Thank you – Emily May 4 '12 at 9:51
@Emily I've converted your 'answer' to a comment, and also merged your accounts. Please consider registering: that way you will have better control over your messages here. – Joseph Wright May 4 '12 at 9:53
@Joseph Wright, thank you. percusse showed me that my piece of code was a bit misleading. Shoud I modify it (and correct the mistake), or shoud I leave it as it is? – Emily May 4 '12 at 10:11
@Emily Please edit your question - the site works best if changes are edited into the questions/answers when appropriate. – Joseph Wright May 4 '12 at 10:12

## 1 Answer

As egreg spotted, the coordinate list is missing a comma. Moreover, you can also use PGF ifthenelse for your task.

    \documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotsinvokeforeach {0,...,5}
{
\pgfmathparse{ifthenelse(#1<3,0,1)}
\addplot coordinates {(#1,\pgfmathresult)};
}
\end{axis}
\end{tikzpicture}
\end{document}


Another possibility is to use the following for conditional plotting

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotsinvokeforeach {0.1,0.2,...,1}
{
\pgfmathparse{( #1 < 0.5 ? int(1) : int(0))}
\ifnum\pgfmathresult>0
\addplot{#1};
\else
\fi
}
\end{axis}
\end{tikzpicture}
\end{document}


Notice how 0.5 slipped away from the if clause (only gets caught after x<0.499975). So please keep in mind that numerical accuracy is not that reliable.

-
You're right, but those plot commands were only example codes. The real question is how to plot a line or not (at all) depending on the loop index. Something like "if k<1 plot the line, else don't plot it". Thank you – Emily May 4 '12 at 10:00
Thank you for your answer. It works well with natural numbers, but not with rational ones (try \pgfplotsinvokeforeach{0.1,0.2} instead). I get the "missing =" and "missing number" errors. – Emily May 4 '12 at 10:18
@Emily I think this time it's getting there. – percusse May 4 '12 at 10:40
percusse you're right. Thank you very much (even for the explanation on accuracy) – Emily May 4 '12 at 10:52