9

I have a table with four images.

\begin{table}[ht]
\caption{table caption...}
\centering
\begin{tabular}{cc}
\includegraphics[width=45mm]{w.png} &\includegraphics[width=45mm]{x.png}\\
\newline
\includegraphics[width=45mm]{y.png}&\includegraphics[width=45mm]{z.png}\\
\end{tabular}
\end{table}

I want to caption each of the images. Normally I would do this by placing includegraphics within a \begin{figure} block using \caption{...}. But when I do this, the images disappear.

What is the proper way to do this, so that I can caption each of the images.

4 Answers 4

10

Or you could use the subfig package:

\documentclass{article}

\usepackage{subfig}
\usepackage{graphicx}

\begin{document}

\begin{figure}
    \centering
    \subfloat[Caption 1]{
        \label{ref_label1}
        \includegraphics[width=0.5\textwidth]{example-image-a}
    }
    \subfloat[Caption 2]{
        \label{ref_label2}
        \includegraphics[width=0.5\textwidth]{example-image-b}
    }
    \caption{Overall caption}
    \label{ref_label_overall}
\end{figure}

\end{document}
2
  • 2
    What if i want to add 4 images in two rows, how can i mdify this code?
    – Fadwa
    Jul 28, 2016 at 15:27
  • 1
    \hfill is suggested between two subfloats to have them seperated
    – sai
    Nov 15, 2020 at 15:00
5

You can use the subcaption package for this but I think you don't need table for such use. You can embed everything into a general figure environment and place them as you wish. See the manual for that. Here is the version with Table.

\documentclass{article}
\usepackage{mwe} % For dummy images
\usepackage{subcaption}
\begin{document}
{\centering
\begin{table}[ht]
\begin{tabular}{cc}
\begin{subfigure}{0.4\textwidth}\centering\includegraphics[width=0.3\columnwidth]{example-image-a}\caption{Figure A}\label{fig:taba}\end{subfigure}&
\begin{subfigure}{0.4\textwidth}\centering\includegraphics[width=0.3\columnwidth]{example-image-b}\caption{Figure B}\label{fig:tabb}\end{subfigure}\\
\newline
\begin{subfigure}{0.4\textwidth}\centering\includegraphics[width=0.3\columnwidth]{example-image-c}\caption{Figure C}\label{fig:tabc}\end{subfigure}&
\begin{subfigure}{0.4\textwidth}\centering\includegraphics[width=0.3\columnwidth]{example-image-a}\caption{Figure A again}\label{fig:taba2}\end{subfigure}\\
\end{tabular}
\caption{A table with figures}
\label{tab:mytable}
\end{table}
}

We can see that Figure \ref{fig:taba} and Figure \ref{fig:taba2} is the same. Also the table counter value is used for the reference.
\end{document}

enter image description here

1
  • How do you control the vertical distance between the rows of figures?
    – skan
    Nov 16, 2017 at 10:33
4

You could simply use capt-of which provides stand-alone captionof command

\documentclass{book}
\usepackage[demo]{graphicx}
\usepackage{capt-of}
\usepackage{tabu}

\begin{document}

\begin{table}[ht]
\caption{Simple but not clever}
\centering
\begin{tabu}to \textwidth {X[c]X[c]}
  \includegraphics[width=45mm]{w.png}\captionof{figure}{fd} &\includegraphics[width=45mm]{x.png}\captionof{figure}{fd} \\
  \includegraphics[width=45mm]{y.png}\captionof{figure}{fd} &\includegraphics[width=45mm]{z.png}\captionof{figure}{fd} \\
\end{tabu}
\end{table}

\end{document}

enter image description here

This has two disadvantages: first it's not elegant and the table caption is much to close to the first picture. In my opinion one of the subfig or subcaption solutions is much better. When using a KOMA class there is no need to load capt-of.

1

Building off of a previous answer, if you want to use the subfig package with multiple rows:

\usepackage{subfig}

...

\begin{figure}
  \centering
  \subfloat[Caption 1]{\label{ref_label1}\includegraphics[width=0.5\textwidth]{path/to/figure1}}
  \subfloat[Caption 2]{\label{ref_label2}\includegraphics[width=0.5\textwidth]{path/to/figure1}}
  % leave a blank space for a new row

  \subfloat[Caption 3]{\label{ref_label3}\includegraphics[width=0.5\textwidth]{path/to/figure3}}
  \subfloat[Caption 4]{\label{ref_label4}\includegraphics[width=0.5\textwidth]{path/to/figure4}}
  \caption{\label{ref_label_overall}Overall caption}
\end{figure}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.