3

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
            }
  • Am I being naive in thinking that just putting \x in, say, the caption, would be a solution? – Seamus Aug 28 '15 at 1:39
4

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

  • thanks for the tip, but I need to display the actual value and not the letter in the first code example. In the second example it works as I want, but the loop has numbers and in my case I use strings (eg. s01.2015.37.09_2015.09.23). That's why I need to show that e.g., s01.2015.37.09_2015.09.23 is displayed in the iteration 1, and so forth. – Gery Aug 28 '15 at 18:59
  • @Gery: It is the actual value displayed, not the counter. Replace with your string and enclose each of them inside braces like {s01.2015.37.09_2015.09.23},{s01.2015.37.09_2015.09.23},{s01.2015.37.09_2015.09.23} in the list of \foreach. – user11232 Aug 28 '15 at 23:15
  • thanks again for the additional answer, but what I need is the counter and not the actual value; so it should be e.g. This is step 01 instead of This is step s01.2015.37.09_2015.09.24. I made a mistake in my previous comment, I meant the actual counter number (instead of the actual value), how could I get the actual counter number (iteration count)? Thanks also for the tip about the braces! – Gery Aug 29 '15 at 0:31
  • @Gery See the edit at the end. – user11232 Aug 29 '15 at 0:35
  • thanks for the edit, but there is a problem with your last suggestion, this new error is: Undefined control sequence \foreach \x[count=\y] Undefined control sequence }. Please see my edit to show what works and what does not work (ie. new error message above). – Gery Sep 1 '15 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.