9

I want to show 50 pictures with numbers from 1 to 50 beneath on one page (or only a few pages). So basically a list of images with a number to identify them.

I tried:

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

\begin{document}
\foreach \x in {1,...,\numimages}{
    \begin{figure}
            \includegraphics[width=.2\columnwidht]{./img/img_\x}
            \caption{\x}
    \end{figure}
\fi
\end{document}

The images are named "img_1" to "img_50". They are in the folder img.

Problems

  1. The name is "Figure 1: 1" and it should only be "1"
  2. The images start as a new line and do not appear side by side

How it looks like now:

Enter image description here

How it should look like (just the arrangement and the caption, not the paper):

Enter image description here

7
  • Ten rows of pictures each with a caption? The maximum height for each can be about 1.2cm with a standard text height. Maybe a bit more if you increase the text height.
    – egreg
    Commented Dec 18, 2022 at 13:47
  • The caption should just be "1" or "2" (so the image number). The idea is to give each image a number and print it, so I have an overview over which image has which number.
    – user286935
    Commented Dec 18, 2022 at 13:50
  • show the error you got, the posted code will give undefined command error for \columnwidht is that what you are asking about? Commented Dec 18, 2022 at 14:08
  • @DavidCarlisle thx for your comment ... I have edited my post to show how the outcome is now and how it should look like ... I hope you can help
    – user286935
    Commented Dec 18, 2022 at 14:44
  • 1
    I am surprised you get output at all with \columnwidht for \columnwidth Commented Dec 18, 2022 at 15:40

2 Answers 2

17

The only advantages of using the figure environment is that it will float to a good position in your document and the ability to add a proper caption with "Figure 42: ..." - two things which seem undesired in your case, so don't use a figure environment.

Without it, you can place the images besides each other like normal letters in a text:

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

\begin{document}

{%
\noindent%
\raggedright%
\foreach \x in {1,...,50}{%
  \parbox{.18\textwidth}{\centering\includegraphics[width=\linewidth]{example-image-duck}\par \x}\space%
}%

\end{document}

enter image description here

4
  • 1
    purrfect, thank you <3
    – user286935
    Commented Dec 18, 2022 at 15:00
  • @Curious You're welcome! Commented Dec 18, 2022 at 15:03
  • 2
    +1: How does the counter affect the file name (in {example-image-duck})? Commented Dec 18, 2022 at 22:05
  • 4
    @Dr.ManuelKuehner In this example not at all, but the OP had already solved the of problem of the filename in their MWE. The screenshot in my answer is a bit fake, as I was also loading the duckuments package to get random images instead of showing 50 times the same one. Commented Dec 18, 2022 at 22:13
7

An abstraction seems the best strategy. I define the \multipleimages command with the syntax

\multipleimages
  {<file name pattern>}
  {<number of images per line>}[<correction>]
  {<total number>}

In your case the call should be

\multipleimages{./img/img_#1}{5}{50}

or maybe

\multipleimages{./img/img_#1}{5}[0.8]{50}

The optional correction factor can be determined by trying to typeset reducing it until the float can find place in a page. In the example below I get

LaTeX Warning: Float too large for page by 11.14352pt on input line 45.

when the correction factor is set to 0.8, but no such warning with 0.79. The right factor depends on the vertical size of the images, which are supposed to have the same height.

The second example shows how to change the number of images per line.

There is no #1 in the first argument in examples 1 and 2 because I didn't want to create 50 different names. So, for the third example, I made four copies of example-image.pdf named img1.pdf, img2.pdf, img3.pdf and img4.pdf in order to show that the first argument correctly interprets the given pattern.

\documentclass{article}
\usepackage{graphicx}

\ExplSyntaxOn

\NewDocumentCommand{\multipleimages}{mmO{1}m}
 {% #1 = pattern for the image names
  % #2 = number of images per line
  % #3 = reduction factor (default 1)
  % #4 = total number of images
  \curious_multimg:nnnn { #1 } { #2 } { #3 } { #4 }
 }

\int_new:N \l__curious_multimg_idx_int

\cs_new_protected:Nn \curious_multimg:nnnn
 {
  \cs_set:Nn \__curious_multimg_name:n { #1 }
  \int_zero:N \l__curious_multimg_idx_int
  \int_step_inline:nn { #4 }
   {
    \group_begin:
    \renewcommand{\arraystretch}{0}
    \begin{tabular}[t]{@{}c@{}}
    \includegraphics[width=\fpeval{(#3)/(#2)}\columnwidth]{\__curious_multimg_name:n { ##1 }}
    \\
    \footnotesize\strut ##1
    \end{tabular}
    \group_end:
    \int_compare:nT{\int_mod:nn{##1}{#2}==0}{\par\vspace{0.3ex}}
   }
 }

\ExplSyntaxOff

\begin{document}

\begin{figure}[p]
\centering

\multipleimages{example-image}{5}[0.79]{50}

\caption{Fifty images}

\end{figure}

\begin{figure}[p]
\centering

\multipleimages{example-image}{7}{50}

\caption{Fifty images}

\end{figure}

\begin{figure}[p]
\centering

\multipleimages{img#1}{2}{4}

\caption{Four images}

\end{figure}

\end{document}

enter image description here

enter image description here

enter image description here

2
  • Is package xfp needed for \fpeval command?
    – Cicada
    Commented Dec 19, 2022 at 13:24
  • @Cicada Not with LaTeX released October 2021 or later.
    – egreg
    Commented Dec 19, 2022 at 13:28

You must log in to answer this question.