I have a large number of figures (~300) in a folder called 'allimages'. The figures are numbered fig1.eps, fig2.eps,.....fig300.eps. I want to put all of them in latex such that 6 images appear on one page. What I know to do is something like this

\caption{Write the caption here.}
\vspace{0.0cm} \centering

But doing like this will be time consuming. Is it possible to have some looping statement that makes it easier and help me skip long listing of figures. Thanks!

  • I would leave this task to my editor... Thus, what editor do you use?
    – Jan
    Feb 17, 2013 at 12:09
  • I use texshop on mac platform. I use Xcode.
    – user26101
    Feb 17, 2013 at 12:20
  • Related: tex.stackexchange.com/questions/53458/… Feb 18, 2013 at 15:30
  • 4
    If you have coding skills, just write a little program in your favorite language to generate the LaTeX source code.
    – user6853
    Feb 18, 2013 at 16:14

3 Answers 3


This looks easiest to do using \foreach from the pgf bundle. To get everything on separate pages, you probably need a couple of loops:

\foreach\x in {0,1,...,49}{%
    \foreach\y in {1,2,...,6}{%
      \includegraphics[height=5.4cm]{allimages/fig\numexpr 6 * \x + \y\relax}
    \captionof{figure}{Images \number\numexpr 6 * \x + 1\relax\space to
      \number\numexpr 6 * \x + 6\relax.}

Doing all of this with floats will run out of space quite quickly (no intervening text), so I've used the \captionof command from the caption package to generate the appropriate text.

  • PDF file only has 1 page. Only a few images that are displayed. Other images are not displayed because it exceeds the page limit.
    – Edy Jo
    Jun 7, 2014 at 13:21
  • 1
    @EdyJo True: I've modified to use two loops and thus ensure that each page is 'just right'.
    – Joseph Wright
    Jun 7, 2014 at 13:36
  • For me, it was necessary to put \the in front of numexp, after which this worked.
    – Zach Boyd
    May 27, 2019 at 19:29
  • @ZachBoyd The example works exactly as given above
    – Joseph Wright
    May 27, 2019 at 19:30
  • Great answer! Is it possible to generalize the solution in order to get the images even if they have random names? They'll be sorted, let's say, for example in alphabetical order...
    – Carlo
    Aug 2, 2020 at 22:51

I used this shell snippet to generate the lines I needed.

for f in ./images/*.png; do echo "\\includegraphics[width=0.9\\linewidth]{images/$f}\\\\"; done;

Inspired by @BenCrowell, I solved this by writing a short MATLAB script to do this. In the hope that some future readers find it useful, I post it here.

fileID = fopen('./incl_img_latex.txt', 'w');
fprintf(fileID, '\\begin{figure}[h!]\n\\centering\n');

files = dir('./*.png');
for file = files'
    str = file.name;
    fprintf(fileID, '\\begin{subfigure}[b]{0.2\\textwidth}\n\\includegraphics[trim=45 180 70 210,clip,width=\\textwidth]{%s}\n\\end{subfigure}\n', str);

fprintf(fileID, '\\caption{C.}\n\\label{fig:}\n\\end{figure}');

Your Answer

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