[Tex/LaTex] text and image wrapping in latex tables

graphicstables

I was trying to insert pictures with captions in a table in latex. I have read some posts here about adding pictures in the table using \raisebox. I tried something like

\begin{table}[ht]
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
 Query Structure & The most similar structure & The second most similar structure & The    third most similar structure \\
\hline
\raisebox{-.5\height}{\includegraphics[scale=0.2]{GS_Ade_PathwayA_I1A_B3LYP_631Gd}} &     \raisebox{-.5\height}{\includegraphics[scale=0.2]{GS_Ade_PathwayA_I1A_B3LYP_631Gd}} & \raisebox{-.5\height}{\includegraphics[scale=0.2]{GS_Ade_PathwayA_I1A_G3MP2B3}} &\raisebox{-.5\height}{\includegraphics[scale=0.2]{GS_Ade_PathwayA_I1A_G3B3}}\\
\hline
\end{tabular}
\end{center}
\end{table}

It worked. But I want these pictures with captions. At first, I tried to add caption{} in raisebox directly like:

\raisebox{-.5\height}{\includegraphics[scale=0.2]{GS_Ade_PathwayA_I1A_B3LYP_631Gd} \caption{structure GS\_Ade\_PathwayA\_I1A\_B3LYP\_631Gd}}     

I got error msg for writing latex like this. So I tired to put the captions in the next row with no \hline in between to work around.

\begin{table}[ht]
\label{table2}
\begin{center} %Note American spelling
\begin{tabular}{| p{0.25\textwidth} | p{0.25\textwidth} | p{0.25\textwidth} |   p{0.25\textwidth} |}
\hline
Query Structure & The most similar structure & The second most similar structure & The    third most similar structure \\
\hline
\raisebox{-.5\height}{\includegraphics[scale=0.2]{GS_Ade_PathwayA_I1A_B3LYP_631Gd}} & raisebox{-.5\height}{\includegraphics[scale=0.2]{GS_Ade_PathwayA_I1A_B3LYP_631Gd}} & \raisebox{-.5\height}{\includegraphics[scale=0.2]{GS_Ade_PathwayA_I1A_G3MP2B3}} &\raisebox{-.5\height}{\includegraphics[scale=0.2]{GS_Ade_PathwayA_I1A_G3B3}} \\
GS\_Ade\_PathwayA\_I1A\_B3LYP\_631Gd.out1.ar &GS\_Ade\_PathwayA\_I1A\_B3LYP\_631Gd.out1.ar &GS\_Ade\_PathwayA\_I1A\_G3MP2B3.out1.ar &GS\_Ade\_PathwayA\_I1A\_G3B3.out1.ar \\
\hline
\caption{Results Table}
\end{tabular}
\end{center}
\end{table}

By doing above, I got two problems with the result.

  1. The pictures are not centered in each cell. Although I have set the height of the raisbox, the pictures are still closely attached to the upper cell edge.
  2. I have set the width of each column to be 1/4 width of the \textwidth. It worked for the texts in the header. Some long text such as "The second most similar structure" was splited into two rows. However, for the text under the pictures, they were all overlapped. I think Latex consider it as a whole word, so it didn't split into multiple rows.

Can anyone suggest how to solve these problems?

Thank you.

Best Answer

This solution only places unnumbered subtext below the figures, just using a separate figure row. If there is no text on the same row with the figures, then a \raisebox approach is not required. Since you didn't provide a minimum working example (MWE), I assumed that the figures were on a row by themselves. I omitted \hline between the titles, the figures, and the subtext, so as to group them more nicely. By using p columns, text wraps automatically.

As David pointed out, one must leave room for column padding separation, so in this case, I only made each column width 0.21\textwidth to avoid overfilling the column width.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{url}
\usepackage{caption}
\begin{document}
\begin{table}[ht]
\begin{center}
\begin{tabular}{|p{.21\textwidth}|p{.21\textwidth}|p{.21\textwidth}|p{.21\textwidth}|}
\hline
 Query Structure & The most similar structure & 
The second most similar structure & The    third most similar structure \\
\includegraphics[width=.21\textwidth]{GS_Ade_PathwayA_I1A_B3LYP_631Gd} &
\includegraphics[width=.21\textwidth]{GS_Ade_PathwayA_I1A_B3LYP_631Gd} & 
\includegraphics[width=.21\textwidth]{GS_Ade_PathwayA_I1A_G3MP2B3} &
\includegraphics[width=.21\textwidth]{GS_Ade_PathwayA_I1A_G3B3}\\
\scriptsize \url{GS_Ade_PathwayA_I1A_B3LYP_631Gd.out1.ar}
& \scriptsize next caption
& \scriptsize third caption
& \scriptsize last caption\\
\hline
\end{tabular}
\end{center}
\end{table}
\end{document}

enter image description here


To answer the OP's follow-up, on how to add gap between an \hline and a subsequent row of \includegraphics images, which are otherwise flush with the \hline, there are many approaches. The one I adopt is to use the \addvbuffer[top_gap bottom_gap]{...} macro from my verbatimbox package. One merely need apply this to the tallest image of the row. Here, I re-present the MWE with the \hlines added, and an \addvbuffer in place.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{url}
\usepackage{caption}
\usepackage{verbatimbox}
\begin{document}
\begin{table}[ht]
\begin{center}
\begin{tabular}{|p{.21\textwidth}|p{.21\textwidth}|p{.21\textwidth}|p{.21\textwidth}|}
\hline
Query Structure & The most similar structure & 
The second most similar structure & The    third most similar structure \\
\hline
\addvbuffer[4pt 0pt]{\includegraphics[width=.21\textwidth]{GS_Ade_PathwayA_I1A_B3LYP_631Gd}} &
\includegraphics[width=.21\textwidth]{GS_Ade_PathwayA_I1A_B3LYP_631Gd} &
\includegraphics[width=.21\textwidth]{GS_Ade_PathwayA_I1A_G3MP2B3} &
\includegraphics[width=.21\textwidth]{GS_Ade_PathwayA_I1A_G3B3}\\
\hline
\scriptsize \url{GS_Ade_PathwayA_I1A_B3LYP_631Gd.out1.ar}
& \scriptsize next caption
& \scriptsize third caption
& \scriptsize last caption\\
\hline
\end{tabular}
\end{center}
\end{table}
\end{document}

enter image description here

Related Question