# text and image wrapping in latex tables

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
\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
\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.

• Do these figures need to be numbered like a normal figure, possibly cited elsewhere in the doc and/or showing up in a list of figures, or are you merely content with getting unnumbered subtext that functions like a caption? Jun 2, 2014 at 18:54
• If would be better if these pictures can be cited elsewhere, but it also worked for me just getting these contents as unnumbered subtext. Jun 2, 2014 at 19:11

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 \\
& \scriptsize next caption
& \scriptsize third caption
& \scriptsize last caption\\
\hline
\end{tabular}
\end{center}
\end{table}
\end{document}


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
\hline
& \scriptsize next caption
& \scriptsize third caption
& \scriptsize last caption\\
\hline
\end{tabular}
\end{center}
\end{table}
\end{document}


• }{|p{.25\textwidth}|p{.25\textwidth}|p{.25\textwidth}|p{.25\textwidth}|} is overfull by 8\tabcolsep + 5\arrayrulewidth Jun 2, 2014 at 19:25
• @DavidCarlisle Yeah, I knew that... Will adjust Jun 2, 2014 at 19:27
• I guess I didn't explain my problem clearly. I knew by using p columns, texts will wraps automatically. But for my case, the text I want to put under the pictures is like "GS_Ade_PathwayA_I1A_B3LYP_631Gd.out1.ar".(some cases may even longer) It won't warps automatically. I think Latex consider it as a whole word, so it doesn't wrap it. They were all in one line and overlaps. Jun 2, 2014 at 19:35
• @user3293338 See revision. You need the url package. Jun 2, 2014 at 19:37
• @user3293338 put \linebreak[0] after each _ or anywhere else you want to allow a break (or url package as Steven just said, but that's optimised for breaking on / and . so you might need to change some of its defaults) Jun 2, 2014 at 19:38