[Tex/LaTex] Print iteration counter in for loop

articlecountersforeachloops

I am new using for loops in latex, and not sure if the following is possible. This is basically what I am trying to achieve:

\documentclass{article}

\begin{document}

\foreach \x in {a,b,c}
{
   \includegraphics[scale=.8]{fig\x.pdf}
   \caption{So, three figures displayed, and this one has the number \x of the letter position (or iteration count)} % ie. 1 for a, 2 for b, 3 for c
   \clearpage
}

\end{document}

So, is it possible to just display (eg. save in variable?) the iteration step based on the code above?

Any help is much appreciated,

EDIT 1:

This works:

\foreach \x in {{s01.2015.04.09_2015.04.16}, {s02.2015.04.17_2015.04.24}, {s03.2015.04.25_2015.04.30}, {s04.2015.05.01_2015.05.08}, {s05.2015.05.09_2015.05.16}, {s06.2015.05.17_2015.05.24}, {s07.2015.05.25_2015.05.31}, {s08.2015.06.01_2015.06.08}, {s09.2015.06.09_2015.06.16}, {s10.2015.06.17_2015.06.24}, {s11.2015.06.25_2015.06.30}, {s12.2015.07.01_2015.07.08}, {s13.2015.07.09_2015.07.16}, {s14.2015.07.17_2015.07.24}, {s15.2015.07.25_2015.07.31}}
            {
                \begin{figure}[p]
                    \centering
                    \includegraphics[scale=.7,angle=-90]{../figuras/anexo2/chl.\x.aqua-color.pdf}
                    \caption{Distribución espacial de la flota pesquera industrial sobre la concentración de clorofila-a superficial del mar en el Mar Peruano durante la semana de la primera temporada de pesca 2015.}
                \end{figure}
                \clearpage
            }

and this one below throws this error message: Undefined control sequence \foreach \x[count=\y]
Undefined control sequence }
:

\foreach \x[count=\y] in {{s01.2015.04.09_2015.04.16}, {s02.2015.04.17_2015.04.24}, {s03.2015.04.25_2015.04.30}, {s04.2015.05.01_2015.05.08}, {s05.2015.05.09_2015.05.16}, {s06.2015.05.17_2015.05.24}, {s07.2015.05.25_2015.05.31}, {s08.2015.06.01_2015.06.08}, {s09.2015.06.09_2015.06.16}, {s10.2015.06.17_2015.06.24}, {s11.2015.06.25_2015.06.30}, {s12.2015.07.01_2015.07.08}, {s13.2015.07.09_2015.07.16}, {s14.2015.07.17_2015.07.24}, {s15.2015.07.25_2015.07.31}}
            {
                \begin{figure}[p]
                    \centering
                    \includegraphics[scale=.7,angle=-90]{../figuras/anexo1/sst.\x.aqua-color.pdf}
                    \caption{Distribución espacial de la flota pesquera industrial sobre la temperatura superficial del mar en el Mar Peruano durante la semana \y de la primera temporada de pesca 2015.}
                \end{figure}
                \clearpage
            }

Best Answer

Like this?

\documentclass{article}
\usepackage{pgffor}
\usepackage{graphicx}
\begin{document}

\foreach \x in {a,b,c}
{
   \begin{figure}[p]
   \includegraphics[scale=.5]{example-image-\x}
   \caption{So, three figures displayed, and this one has the number \x\ of the letter position (or iteration count)} % ie. 1 for a, 2 for b, 3 for c
  \end{figure}
   \clearpage
}

\end{document}

enter image description here

Or simply,

\documentclass{article}
\usepackage{pgffor}
\begin{document}

\foreach \x in {1,...,10}
{
   This is step \x, \par
}

\end{document}

enter image description here

This one is to address the comments.

\documentclass{article}
\usepackage{pgffor}
\begin{document}

\foreach \x in {{s01.2015.37.09\_2015.09.24},{s01.2015.37.09\_2015.09.25},{s01.2015.37.09\_2015.09‌​.23}}
{
   This is step \x, \par
}

\end{document}

enter image description here

BTW the underscores need to be handled carefully here.

\documentclass{article}
\usepackage{pgffor}
\begin{document}

\foreach \x[count=\y] in {{s01.2015.37.09_2015.09.24},{s01.2015.37.09_2015.09.25},{s01.2015.37.09_2015.09‌​.23}}
{
   This is step \y, \par
}

\end{document}

enter image description here

The following works too:

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage[demo]{graphicx}
\usepackage{pgffor}
\begin{document}

\foreach \x[count=\y] in {{s01.2015.04.09_2015.04.16}, {s02.2015.04.17_2015.04.24}, {s03.2015.04.25_2015.04.30}, {s04.2015.05.01_2015.05.08}, {s05.2015.05.09_2015.05.16}, {s06.2015.05.17_2015.05.24}, {s07.2015.05.25_2015.05.31}, {s08.2015.06.01_2015.06.08}, {s09.2015.06.09_2015.06.16}, {s10.2015.06.17_2015.06.24}, {s11.2015.06.25_2015.06.30}, {s12.2015.07.01_2015.07.08}, {s13.2015.07.09_2015.07.16}, {s14.2015.07.17_2015.07.24}, {s15.2015.07.25_2015.07.31}}
            {
                \begin{figure}[p]
                    \centering
                    \includegraphics[scale=.7,angle=-90]{../figuras/anexo1/sst.\x.aqua-color.pdf}
                    \caption{Distribución espacial de la flota pesquera industrial sobre la temperatura superficial del mar en el Mar Peruano durante la semana {\LARGE\bfseries \y} de la primera temporada de pesca 2015.}
                \end{figure}
                \clearpage
            }

\end{document}

enter image description here

Related Question