Graphics, Figures & TablesAutomatically generate a grid of images

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
daviddoria
Posts: 60
Joined: Tue Sep 30, 2008 9:24 pm

Automatically generate a grid of images

Post by daviddoria »

I have 25 images (00.jpg, 01.jpg, .... 24.jpg) that I want to display in a 5x5 grid. Is there a way to do this automatically? Or do I have to make a table and write out all of the file names?

Thanks,

Dave

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Automatically generate a grid of images

Post by gmedina »

Hi, Dave

you could rename your images as 1-1.jpg, 1-2.jpg, ... , 1-5.jpg (those who will appear in the first row); 2-1.jpg, 2-2.jpg, ... , 2-5.jpg (those who will appear in the second row); ... 5-1.jpg, 5-2.jpg, ... , 5-5.jpg (those who will appear in the fifth row) and then use, for example, two nested \foreach constructs (of the PGF/TikZ package) to achieve the desired result. The following code illustrates schematically this approach:

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\begin{document}

\foreach \i in {1,...,5}{
    \foreach \j in {1,...,5}{
    \noindent\includegraphics{\i-\j}
  }\\
}

\end{document}
Edit: here's the same idea implemented with \whiledo commands from the ifthen package:

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\usepackage{ifthen}

\begin{document}

\newcounter{myrow}
\newcounter{mycolumn}
\setcounter{myrow}{1}

\whiledo{\themyrow<6}{
    \whiledo{\themycolumn<5}{
    \stepcounter{mycolumn}
    \noindent\includegraphics{\themyrow-\themycolumn}
  }\setcounter{mycolumn}{0}\\\stepcounter{myrow}
}

\end{document}  
1,1,2,3,5,8,13,21,34,55,89,144,233,...
daviddoria
Posts: 60
Joined: Tue Sep 30, 2008 9:24 pm

Re: Automatically generate a grid of images

Post by daviddoria »

Yes, perfect! Exactly what I was looking for, thanks!

Dave
Post Reply