[Tex/LaTex] How to set a picture with caption in table

captionsfloatstables

enter image description here

How to set caption for images in table? like this image.

Best Answer

You haven't provided your requirements for quite a few design aspects, so I have to make some assumptions. In particular, in the examples below I'll assume that the two side-by-side figures should jointly span the entire width of the text block.

You also haven't indicated whether or not the side-by-side figures should be able to float, in the LaTeX sense of the word. I'll provide suggestions for both cases:

  • If you want the two side-by-side figures to be able to float (while staying next to each other, of course), set up a figure environment that encases two minipage environments. Each minipage could be as wide as 0.48\textwidth, say. Inside each minipage, use an \includegraphics instruction (with the width of the graph set to 1\linewidth) to load the images of interest. As usual, use \caption and \label statements, but now within a minipage, to create the captions and set up a means for cross-referencing the figures.

  • If you do not want the side-by-side figures to float, change the preceding setup as follows:

    • Remove the \begin{figure} and \end{figure} statements, but do keep the minipage-related statements. If you want the two minipages to span the width of the text block, be sure to provide a \noindent statement right before the first one.

    • Replace the \caption{...} statements with \captionof{figure}{...} statements. You need to load the caption package to get access to the \captionof macro. Keep using \label statements as before.

    That's all there is to it. :-)

enter image description here

\documentclass{article}
\usepackage{caption} % for \captionof macro
\usepackage[demo]{graphicx} % omit 'demo' option in real document

\begin{document}
Here's a cross-reference to Figure~\ref{fig:star2}.

\hrule % just to illustrate the width of the text block

%% a floating version
\begin{figure}[h!]
\begin{minipage}{0.48\textwidth}
\includegraphics[width=1\linewidth]{first.pdf}
\caption{A cloud} \label{fig:cloud1}
\end{minipage}
\hspace{\fill}
\begin{minipage}{0.48\textwidth}
\includegraphics[width=1\linewidth]{second.pdf}
\caption{A star} \label{fig:star1}
\end{minipage}
\end{figure}

%% a non-floating version
\noindent %  override any \parindent effect
\begin{minipage}{0.48\textwidth}
\includegraphics[width=1\linewidth]{third.pdf}
\captionof{figure}{Another cloud} \label{fig:cloud2}
\end{minipage}
\hspace{\fill}
\begin{minipage}{0.48\textwidth}
\includegraphics[width=1\linewidth]{fourth.pdf}
\captionof{figure}{Another star} \label{fig:star2}
\end{minipage}
\end{document}

The title of your posting actually was, "How to set a picture with caption in table?" (emphasis added) You may be wondering why I have so far not mentioned a method that involves a table-like structure. It turns out that one could -- but I would strongly argue that one should not -- lift the code that generates figures 3 and 4 above and place it inside a tabular* environment (and replace \hspace{\fill} with &):

\noindent
\begin{tabular*}{\textwidth}{@{} l @{\extracolsep{\fill}} r @{}}
\begin{minipage}{0.48\textwidth}
\includegraphics[width=1\linewidth]{fifth.pdf}
\captionof{figure}{Still another cloud} \label{fig:cloud3}
\end{minipage}
&
\begin{minipage}{0.48\textwidth}
\includegraphics[width=1\linewidth]{sixth.pdf}
\captionof{figure}{Still another star} \label{fig:star3}
\end{minipage}
\end{tabular*}

You may verify for yourself that this code produces the exact same output as the earlier, also non-floating, code. I hope you can convince yourself that incurring the extra overhead generated by encasing the two minipage environments inside a tabular* environment is pointless and undesirable.