# How to repeat a block of code and increment a variable by one each time?

I have many images which I would like to include in a latex document.

\begin{figure}[h]
\centering
\includegraphics[scale=0.15]{DSC_0001.jpg}
\end{figure}


This block of code only differs in the image number 0002 or 0003 etc. Is there an easy way to loop through, repeating this block of code but increasing the variable number by 1 each time. But I would also like a different bit of text between each image. So giving something like,

\begin{figure}[h]
\centering
\includegraphics[scale=0.15]{DSC_0002.jpg}
\end{figure}
This is a pic of blah.
\begin{figure}[h]
\centering
\includegraphics[scale=0.15]{DSC_0003.jpg}
\end{figure}
And this pic is about ...
\begin{figure}[h]
\centering
\includegraphics[scale=0.15]{DSC_0004.jpg}
\end{figure}

• Welcome to the site! Your question looks very similar to "For loop" in newcommand Could you take a look at it and let us know if it resolves the issue? – cmhughes Jul 16 '13 at 22:50
• Do you know the start and end of the range? – egreg Jul 16 '13 at 22:54
• Your edit changes the question from something that is predictable to something that is not. I would suggest accepting the current answer as-is, since it solves your original problem and ask a new follow-up question. In that follow-up, please indicate the required interface to accommodate the dual constraint of predictable (filename/image) and unpredictable text (inserts between images). – Werner Jul 22 '13 at 12:56

A modification of my answer to Create a document looping through images

It's not completely trivial, so I guess it deserves an answer of its own.

Note that you don't need the figure environment, just an overall \centering. Using many \begin{figure}[h] just stretches LaTeX to its limits and in this case it's really not needed (if you don't want to caption separately each image).

\documentclass{article}
\usepackage[demo]{graphicx} % the demo option is just for the example

\newcounter{image}
\newcommand{\placeimages}[2]{%
\par{\centering
\loop\ifnum\value{image}<#2\relax
\stepcounter{image}%
\vspace*{\fill}
\edef\current{DSC\string_\fourdigits{image}}%
\includegraphics{\current}%
\\[12pt]\texttt{\current.jpg} % comment this line if you don't want the file name here
\vfill
\repeat
\par}
}
\newcommand{\fourdigits}[1]{%
\ifnum\value{#1}<1000 0\fi
\ifnum\value{#1}<100 0\fi
\ifnum\value{#1}<10 0\fi
\arabic{#1}%
}
\begin{document}

\placeimages{1}{123} % start-end

\end{document}


If you have to add text to each image, such a shortcut won't work. So I suggest to define a different macro:

\documentclass{article}
\usepackage[demo]{graphicx} % the demo option is just for the example

\newcommand{\placeimage}[2]{% #1 = number, #2 = text
\par{\centering
\vspace*{\fill}
\edef\current{DSC\string_\fourdigits{image}}%
\includegraphics{\current}\\*[12pt]
#2\par}
\vfill
}
\newcommand{\fourdigits}[1]{%
\ifnum\value{#1}<1000 0\fi
\ifnum\value{#1}<100 0\fi
\ifnum\value{#1}<10 0\fi
\arabic{#1}%
}
\begin{document}

\placeimage{1}{This is some text}
\placeimage{2}{This is some different text}
\placeimage{3}{Other text}
\placeimage{4}{Again different}

\end{document}


A simple loop cannot work, if you have different text for each image. Somewhere you have to type this text, so adding in front of it \placeimage and the image number should not be so difficult.

• Hi egreg,Thanks for your response and great answer. This is almost what I want and it is working. What if I want a bit of text between each pic, not a caption, is that possible? Thanks for your help. – Shane G Jul 22 '13 at 12:12
• @user33760 It depends on the text: if it's just the file name, then this solution can be easily adapted; if it's different text for each image, then a different approach is needed. Can you add something about this to your question? – egreg Jul 22 '13 at 12:20
• Yes it's different text for each image. – Shane G Jul 22 '13 at 12:47