0

I am looking to achieve a grid layout of same-sized figures, each with its own caption underneath. My initial thought was to put a \figure environment inside a \tabularx like this

\begin{tabularx}{\linewidth}{llll}
  \begin{figure}[h]
    \caption{Example of a parametric plot ($\sin (x), \cos(x), x$)}
    \centering
    \includegraphics[width=0.5\textwidth]{link.pdf}
  \end{figure}
  &
  \begin{figure}[h]
    \caption{Example of a parametric plot ($\sin (x), \cos(x), x$)}
    \centering
    \includegraphics[width=0.5\textwidth]{link.pdf}
  \end{figure}
  \\
  \begin{figure}[h]
    \caption{Example of a parametric plot ($\sin (x), \cos(x), x$)}
    \centering
    \includegraphics[width=0.5\textwidth]{link.pdf}
  \end{figure}
  &
  \begin{figure}[h]
    \caption{Example of a parametric plot ($\sin (x), \cos(x), x$)}
    \centering
    \includegraphics[width=0.5\textwidth]{link.pdf}
  \end{figure}
  \\
\end{tabularx}

but it fails with

! LaTeX Error: Not in outer par mode.

What would the right way to do this? images will all have the same size, while I can't guarantee that for captions (which might have to be multi-line).

I have seen this and this but they seem overly complex, and they kind of separate logical structure from document structure (i.e.: captions separate from image).

6
  • 1
    the onlly reason to use figure is to allow the content to be floated to another position to help with page breaking, so just use \includegraphics without figure and if you need captions put the table inside a figure so it all floats together Commented Aug 29, 2023 at 11:12
  • 2
    tabularx is wrong here (if used it must always have X columns), just usetabular Commented Aug 29, 2023 at 11:13
  • @DavidCarlisle will it work if the table stretches over multiple pages?
    – simone
    Commented Aug 29, 2023 at 11:15
  • @cabohah no, but neither would your tabularx you could use longtable but you don't really need a table at all. It's not clear how many images per row you want, seems to be 2 but you show a table with 4 columns. I'd just use pairs of minipages no need for a tabular. Commented Aug 29, 2023 at 11:32
  • You need at least one X column for tabularx to work (see also tabular*). You could put the entire tabular inside a figure, but \caption starts and ends with a \par, so you need to use p or X columns. Commented Aug 29, 2023 at 15:24

2 Answers 2

0

Putting images with the same size but different lengths of caption text side by side is problematic with caption above the image. Because of this, I would recommend to use caption below the image. With this, I would put each row into one figure, so they still would be able to float row per row:

\documentclass{article}

\usepackage{mwe}

\begin{document}
\blindtext

\begin{figure}
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-a}
    \caption{Example of a short caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-b}
    \caption{Example of a longer caption needing more than one line and
      therefore having linebreaks.}
  \end{minipage}
\end{figure}
\begin{figure}
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-c}
    \caption{Example of a short caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image}
    \caption{Example of a longer caption needing more than one line and
      therefore having linebreaks.}
  \end{minipage}
\end{figure}

\blindtext

\begin{figure}
  \begin{minipage}[t]{.2\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-a}
    \caption{Example of a short caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{.2\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-b}
    \caption{Example of a longer caption needing more than one line and
      therefore having linebreaks.}
  \end{minipage}\hfill
  \begin{minipage}[t]{.2\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-c}
    \caption{Example of a short caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{.2\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image}
    \caption{Example of a longer caption needing more than one line and
      therefore having linebreaks.}
  \end{minipage}%
\end{figure}
\begin{figure}
  \begin{minipage}[t]{.2\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-a}
    \caption{Example of a short caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{.2\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-b}
    \caption{Example of a longer caption needing more than one line and
      therefore having linebreaks.}
  \end{minipage}\hfill
  \begin{minipage}[t]{.2\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-c}
    \caption{Example of a short caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{.2\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image}
    \caption{Example of a longer caption needing more than one line and
      therefore having linebreaks.}
  \end{minipage}%
\end{figure}

\blindtext

\end{document}

enter image description here

With caption above the image, it is difficult to decide, how the vertical position should be, maybe:

\documentclass{article}
\usepackage[position=top]{caption}

\usepackage{mwe}

\begin{document}
\blindtext

\begin{figure}
  \begin{minipage}[t]{.45\textwidth}
    \caption{Example of a short caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{.45\textwidth}
    \caption{Example of a longer caption needing more than one line and
      therefore having linebreaks.}
  \end{minipage}
  
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-a}
  \end{minipage}\hfill
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-b}
  \end{minipage}
\end{figure}

\begin{figure}
  \begin{minipage}[t]{.45\textwidth}
    \caption{Example of a short caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{.45\textwidth}
    \caption{Example of a longer caption needing more than one line and
      therefore having linebreaks.}
  \end{minipage}
  
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-a}
  \end{minipage}\hfill
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-b}
  \end{minipage}
\end{figure}

\blindtext

\end{document}

enter image description here

And if you don't want the figures to float, you could either use package float and add option [H] to all the figure environments in my examples, or just don't use any figure environment:

\documentclass{article}

\usepackage{caption}
\usepackage{mwe}

\begin{document}
\blindtext[3]

\begin{flushleft}
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-a}
    \captionof{figure}{Example of a short caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-b}
    \captionof{figure}{Example of a longer caption needing more than one line and
      therefore having linebreaks.}
  \end{minipage}
  \bigskip
  
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image-c}
    \captionof{figure}{Example of a short caption}
  \end{minipage}\hfill
  \begin{minipage}[t]{.45\textwidth}
    \centering
    \includegraphics[width=.75\linewidth]{example-image}
    \captionof{figure}{Example of a longer caption needing more than one line and
      therefore having linebreaks.}
  \end{minipage}
\end{flushleft}

\blindtext

\end{document}

enter image description here

3
  • Nice answer, thorough and clear! However I am sorry it doesn't work as the number of images is too large - Too many unprocessed floats: the number of images is in the thousands, and I need to fit about 16 per page
    – simone
    Commented Aug 29, 2023 at 13:31
  • 2
    @simone if you have that many images you certainly don't want a float, you could use [H] then it won't use up the float stack at all. (or don't use figure and use \captionof for captions. Commented Aug 29, 2023 at 15:23
  • @simone It seems, you've not seen my last example, that does not use floats and is mentioned exactly for such situations.
    – cabohah
    Commented Aug 29, 2023 at 16:54
2

From comments you do not want to float the images, so figure is the wrong construct (although you could use [H] to make figure a non-float) here I just use parboxes, 3 per row but you could use more or less if you adjustthe width suitably.:

enter image description here

\parbox[b]{.3\textwidth}{\captionof{figure}{\raggedright#5}\includegraphics[width=\linewidth]{#6}}\par
\medskip
}

\begin{document}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\imgrow
{Example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-a}
{Another example of a parametric plot ($\sin (x), \cos(x), x$)}{example-image-b}
{Example of a parametric plot ($\sin (x), \cos(x), x$) with a much longer caption}{example-image-c}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .