Search code examples
latexpdflatex

PdfLaTeX: Auto Wrap in a Table with Images


Sorry to bother everyone again here! I'm trying to make a table, where it contains several images and texts, however, it cannot wrap automatically, and the table becomes a mess......

The related code is:

\begin{table*}[width=\textwidth,cols=3,pos=h]
    \caption{This is a caption.}
    \begin{tabular*}{\tblwidth}{@{} LLL@{} }
    \toprule
        Iterations & Samples & Comments \\
    \midrule
        0    & $\includegraphics[width=.4\textwidth]{000000.png}$ & Training started \\
        8    & $\includegraphics[width=.4\textwidth]{000008.png}$ & The text is toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo long that not all the contents can be shown \\
        16   & $\includegraphics[width=.4\textwidth]{000016.png}$ & \\
        32   & $\includegraphics[width=.4\textwidth]{000032.png}$ & \\
        64   & $\includegraphics[width=.4\textwidth]{000064.png}$ & \\
        128  & $\includegraphics[width=.4\textwidth]{000128.png}$ & \\
        192  & $\includegraphics[width=.4\textwidth]{000192.png}$ & \\
    \bottomrule
    \end{tabular*}
    \label{tab4}
\end{table*}

And the results:

enter image description here

Btw, is there any solution to put the words and the images (the same row) align on the same centerline? I tried to use \raisebox but then images from different rows come together......


Solution

  • You could either use a column of fixed width, e.g. m{3cm} or a tabularx which will automatically choose the column width:

    \documentclass{article}
    
    \usepackage[export]{adjustbox}
    \usepackage{array}
    \usepackage{tabularx}
    \usepackage{booktabs}
    \usepackage{graphicx}
    \def\tabularxcolumn#1{m{#1}}
    
    \begin{document}
    
    \begin{table*}[htbp]
        \caption{This is a caption.}
        \begin{tabularx}{\textwidth}{ @{} l l X @{} }
        \toprule
            Iterations & Samples & Comments \\
        \midrule
            0    & $\includegraphics[width=.4\textwidth,valign=M]{example-image-duck}$ & Training started \\
            8    & $\includegraphics[width=.4\textwidth,valign=M]{example-image-duck}$ & The text is too oooo oooo ooo ooo oooo oooo oooo oooo oooo oooo oooo oooo oooo ooooo ooo  long that not all the contents can be shown \\
            16   & $\includegraphics[width=.4\textwidth,valign=M]{example-image-duck}$ & \\
            32   & $\includegraphics[width=.4\textwidth,valign=M]{example-image-duck}$ & \\
            64   & $\includegraphics[width=.4\textwidth,valign=M]{example-image-duck}$ & \\
            128  & $\includegraphics[width=.4\textwidth,valign=M]{example-image-duck}$ & \\
            192  & $\includegraphics[width=.4\textwidth,valign=M]{example-image-duck}$ & \\
        \bottomrule
        \end{tabularx}
        \label{tab4}
    \end{table*}
    
    
    \end{document}
    

    enter image description here