# Positioning of images/graphics in tables

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:

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?

• What about the vertical alignment? Nov 23 '13 at 20:24
• I'm not familiar with it, care to elaborate? Nov 23 '13 at 21:04
• I mean the relative vertical alignment of the items in a row; should the text be vertically centered with respect to the picture? Nov 23 '13 at 21:23
• You could put the \includegraphics inside a \parbox[c]{0.4\linewidth}. Also, do you need the height specification, because you REALLY don't want it to be \textheight. Nov 24 '13 at 2:50

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:

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

• It is much better to use \graphicspath{{../Img/05Core/}} here to reduce the number of consumed keystrokes. Nov 24 '13 at 11:29

More advanced scenario with tabu (not taboo).

\documentclass[preview,border=12pt,varwidth]{standalone}% change it back to your own document class

\usepackage[demo]{graphicx} % the option demo is used for the example, remove it
\graphicspath{{../Img/05Core/}}
\usepackage{tabu,mathtools,tikz}

\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
$\int_a^b f(x)\,\textrm{d}x=F(b)-F(a)$ & 1E-27                     & 1             & TF1            \\ \hline
\includegraphics[height=4cm,keepaspectratio]{Motif2} & 1E-25                     & 2             & TF2            \\ \hline
$\begin{pmatrix} 1& 2& 3\\ 4 & 5 & 6\\ 7 &8 &9 \end{pmatrix}$ & 1E-23                     & 3             & TF3            \\ \hline
\begin{aligned} a+b+c &= d+e+f\\ pV &= nRT\\ \gamma +\alpha-\beta &=0 \end{aligned} & 1E-23                     & 3             & TF3            \\ \hline
\begin{tikzpicture} \draw[red] (0,0) circle (2) ;\end{tikzpicture}& 1E-23                     & 3             & TF3            \\ \hline
\end{tabu}
\caption{Significantly enriched motifs retrieved.}
\label{tab:Motifs}
\end{table}
\end{document}