How to put a matrix of images?

So long as the images all have the same dimensions, you can do with a tabular; I defined an auxiliary \subf command for the picture and the subcaption, but just for keeping things properly segregated. You can add any number of \\ commands in the caption.

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{graphicx}

\newcommand{\subf}[2]{%
  {\small\begin{tabular}[t]{@{}c@{}}
  #1\\#2
  \end{tabular}}%
}


\begin{document}
\begin{figure}
\centering
\begin{tabular}{|c|c|}
\hline
\subf{\includegraphics[width=60mm]{example-image-4x3.pdf}}
     {``iteraciones máximas \\ de BT''$=20$}
&
\subf{\includegraphics[width=60mm]{example-image-4x3.pdf}}
     {``Periodo de Tenencia \\ en Lista Tabú''$=2$}
\\
\hline
\subf{\includegraphics[width=60mm]{example-image-4x3.pdf}}
     {``iteraciones máximas \\ de BT''$=20$}
&
\subf{\includegraphics[width=60mm]{example-image-4x3.pdf}}
     {``Periodo de Tenencia \\ en Lista Tabú''$=2$}
\\
\hline
\end{tabular}
\end{figure}
\end{document} 

enter image description here

If some space is wanted between the tabular rule and the image, here's a possible way: create a fake first line and back up vertically.

The code is exactly the same as before, but the definition of \subf changes into

\newcommand{\subf}[2]{%
  {\small\begin{tabular}[t]{@{}c@{}}
   \mbox{}\\[-\ht\strutbox]
   #1\\#2
   \end{tabular}}%
}

enter image description here


There is a small problem that the image touches the upper line. Some PDF readers does not show the upper line above the images depending on the view scale settings.

The following example defines macro \addheight that increases the height of the image box (adds some white space above the image). The amount can be configured via the optional argument.

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}

\newcommand*{\addheight}[2][.5ex]{%
  \raisebox{0pt}[\dimexpr\height+(#1)\relax]{#2}%
}

\begin{document}
\noindent
\begin{tabular}{|c|c|}
      \hline
      \addheight{\includegraphics[width=60mm]{simu.jpg}} &
      \addheight{\includegraphics[width=60mm]{simu.jpg}} \\
      \small ``row 2, column 1'' &  ``row 2, column 2'' \\
      \hline
\end{tabular}
\end{document}

Result


Part 1 (figures):

Have you tried something like:

\documentclass{whatever}
...
\newsavebox{<boxname>}
\setbox\<boxname>=\hbox{
\includegraphics[<options>]{<filename}}
} 

\begin{document}
...
\begin{tabular}{...}
...
{\usebox{\<boxname>}}
...
\end{tabular}
...
\end{document}

You can resize boxes using the \resizebox or manually with arguments of includegraphics.


Part 2 (text):

You can also use \hbox (this is in the standard LaTeX engine, no package required), or \pbox{<size of box>}{<text, argument, figure, you name it>} (this will require \usepackage{pbox} to function, and perhaps a small update of your TeX/package distribution).

\pbox also allows you to use linebreaks within the environment:

\pbox{<size of box>}{<text, argument, figure, you name it> \\
                     <more text, arguments, figures, etc.>    
                    }

(If you decide this is prettier.)


These are pretty robust and impractical solutions to put figure (option 1) and text (option 2) into floats (floats are figures, tikzpictures, and I'm pretty sure this works too for tables).


MWE

\documentclass[12pt,a4paper]{article}
\usepackage{mwe}
\usepackage{graphicx}
\usepackage{pbox}

\begin{document}
\begin{tabular}{|c|c|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  \pbox{6cm}{\vspace{5ex} \includegraphics[width=60mm]{simu.jpg}} & \pbox{6cm}    {\vspace{5ex} \includegraphics[width=60mm]{simu.jpg}} \\
  {\pbox{6cm}{\vspace{.25ex}\small ''iteraciones máximas de BT''=20\vspace{5ex}}}     &       {\pbox{6cm}{\vspace{5ex}\small ''Periodo de Tenencia en Lista Tabú''=2\vspace{5ex}}} \\
 &  \\
  \hline
  \pbox{6cm}{\vspace{5ex} \includegraphics[width=60mm]{simu.jpg}} & \pbox{6cm}    {\vspace{5ex} \includegraphics[width=60mm]{simu.jpg}} \\
  {\pbox{6cm}{\vspace{5ex}\small ''iteraciones máximas de BT''=20\vspace{5ex}}} &   {\pbox{6cm}{\vspace{.25ex}\small ''Periodo de Tenencia en Lista Tabú''=2 \vspace{5ex}}} \\
\hline
\end{tabular}
\end{document}

Here you can see that you can control the height to whatever length you want (using vspace) and the width, using pbox. Is this what you want?

Tags:

Graphics