[Tex/LaTex] Positioning of images/graphics in tables

graphicspositioningspacingtablestabu

I need to create a table where cells in one columns are images, not text.

This is the code I'm using:

\begin{table}[hbtp]
\centering
\begin{tabu} to \textwidth {|c|c|c|c|}
    \hline
    \textbf{Motif}                                                                                                             & \textbf{\textit{P}-value} & \textbf{Rank} & \textbf{Match} \\ \hline
    \includegraphics[width=0.4\linewidth,height=\textheight,keepaspectratio]{../Img/05Core/Motif1} & 1E-27                     & 1             & TF1                    \\ \hline
    \includegraphics[width=0.4\linewidth,height=\textheight,keepaspectratio]{../Img/05Core/Motif2} & 1E-25                     & 2             & TF2                    \\ \hline
    \includegraphics[width=0.4\linewidth,height=\textheight,keepaspectratio]{../Img/05Core/Motif3} & 1E-23                     & 3             & TF3                    \\ \hline
\end{tabu}
\caption{Significantly enriched motifs retrieved.}
\label{tab:Motifs}
\end{table}

Now, the images I'm using are cropped PDF files so I would need to have some white margin around them inside each cell.
It turns out the code above automatically creates some padding left, right and below the picture, but the boundary at the top of it is overlapping with the upper limit of the cell, like so:

enter image description here

How do I center the picture in the cell so that it has the same margin from the top and the bottom of the cell?

Also, how do I align the text in the other cells to the middle of the cell?

Best Answer

Try something like this:

\documentclass{article}

\usepackage[demo]{graphicx} % the option demo is used for the example, remove it
\usepackage{tabu}

\begin{document}
\begin{table}[hbtp]
\centering
\tabulinesep=\tabcolsep
\begin{tabu} to \textwidth {|X[4,c,m]|X[1,c,m]|X[0.75,c,m]|X[1.25,c,m]|}
    \hline
    \textbf{Motif}                                                           & \textbf{\textit{P}-value} & \textbf{Rank} & \textbf{Match} \\ \hline
    \includegraphics[width=\linewidth,keepaspectratio]{../Img/05Core/Motif1} & 1E-27                     & 1             & TF1            \\ \hline
    \includegraphics[width=\linewidth,keepaspectratio]{../Img/05Core/Motif2} & 1E-25                     & 2             & TF2            \\ \hline
    \includegraphics[width=\linewidth,keepaspectratio]{../Img/05Core/Motif3} & 1E-23                     & 3             & TF3            \\ \hline
\end{tabu}
\caption{Significantly enriched motifs retrieved.}
\label{tab:Motifs}
\end{table}
\end{document} 

Output:

enter image description here

The vertical space in cells is set through \tabulinesep (in this case to be the same as \tabcolsep, the space for columns separation).

If you need more room, you can add some issuing

\addtolength{\tabcolsep}{5pt}

just before

\tabulinesep=\tabcolsep
Related Question