# Numbers as markers with \foreach and additional macro based on counter

First issue

I would like to plot several curves using the \foreach command and have their number in the sequence as markers. Here is the code I came up with :

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
\begin{tikzpicture}
1 4   0.2  0.1
2 4.2 0.1  0.5
3 3.1 0.3  0.4
4 2.5 0.25 0.35
}\table

\begin{axis}
\foreach \i in {1,2,3}
\addplot+[text mark={\i}, mark=text] table[x index=0, y index=\i]{\table};
\end{axis}
\end{tikzpicture}
\end{document}


and here is the result it raises :

My problem is that for some reason the text mark={\i} is stuck to 1.

A solution is to use the \pgfplotsinvokeforeach command (see my answer).

Second issue

But now if I need some other index computed from the counter using for instance

\pgfmathsetmacro{\j}{\i+1}% with the \foreach command


I would write

\pgfmathsetmacro{\j}{##1+1}% with the \pgfplotsinvokeforeach command


but this last one does not work. I think the problem is that the #1 is interpreted as if it is supposed to represend an argument of \pgfmathsetmacro. My attempt of doubling the hash did not solve this second issue.

-
Have you tried a counter and the stepcounter function? –  azetina Jul 26 '12 at 16:16

For the updated problem you can use the math engine of PGF.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
\begin{tikzpicture}
1 4   0.2  0.1
2 4.2 0.1  0.5
3 3.1 0.3  0.4
4 2.5 0.25 0.35
}\table

\begin{axis}
\pgfplotsinvokeforeach{1,2,3}{%
\addplot+[text mark={\pgfmathparse{int(#1+1)}\pgfmathresult}, mark=text] table[x index=0, y index=#1]{\table};};
\end{axis}
\end{tikzpicture}
\end{document}

-
It works, thanks. Do you know a general method to avoid getting ! Illegal parameter number in definition of \pgfmath@expression. errors? –  Alfred M. Jul 26 '12 at 18:09
@AlfredM. Unfortunately the cause depends on the context however usually due to a wrong argument number and/or using #1 in a non argument expecting macro. –  percusse Jul 26 '12 at 18:56
What I meant is : is there a way to prevent the #1 inside a nested macro definition to be intrepreted as an argument of the inside macro instead of the argument of the outside one? I thought doubling the hash (##1) was doing this but apparently not. –  Alfred M. Jul 27 '12 at 10:57
@AlfredM. It might be more complicated than that since expansion inside foreach problematic enough and partially solved via \pgfplotsinvokeforeach. I don't know what the actual problem you have is but you can always ask a question specific to this such that you have more space to fully explain it. Follow-ups are more than welcome. –  percusse Jul 27 '12 at 12:37

The problem can be solved by using \pgfplotsforeach instead of \foreach

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
\begin{tikzpicture}
1 4   0.2  0.1
2 4.2 0.1  0.5
3 3.1 0.3  0.4
4 2.5 0.25 0.35
}\table

\begin{axis}
\pgfplotsinvokeforeach{1,2,3}{%
\addplot+[text mark={#1}, mark=text] table[x index=0, y index=#1]{\table};};
\end{axis}
\end{tikzpicture}
\end{document}


I found it thanks to this question pgfplots: setlength inside foreach.

-