3

I have a table with two columns. The first column contains text, the second a image. Short example:

\documentclass{article}
\begin{document}

\begin{table}[htbp]
    \centering
    \caption{My caption}
    \label{tab:mytab}
    \begin{tabular}{|p{1.0cm}|c|}
        \hline
        \textbf{Number}   & \textbf{Images} \\ \hline
        Nr. 1 & \raisebox{.5\totalheight}{\includegraphics[width=0.8\textwidth]{foo}}       \\ \hline
        Nr. 2 & \raisebox{.5\totalheight}{\includegraphics[width=0.8\textwidth]{foo}}       \\ \hline
    \end{tabular}
\end{table}

\end{document}

I want to center the text and image vertically and horizontally. Does anyone know how to do this?

  • You left off the minus, \raisebox{-.5\totalheight}{} – Steven B. Segletes Jun 15 '16 at 9:56
2

With help of package adjustbox you can obtain:

enter image description here

In the first row the margin above and below picture is added, in the second row the image is without added margins. The code:

\documentclass{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}

\begin{document}
\begin{table}[htbp]
    \centering
    \caption{My caption}
    \label{tab:mytab}
    \begin{tabular}{|p{1.5cm}|c|}
        \hline
\textbf{Number}   
        &   \textbf{Images} \\ 
        \hline
Nr. 1   &   \includegraphics[width=0.8\textwidth,
                             margin=0pt 1ex 0pt 1ex,valign=m]{example-image}  \\
        \hline
Nr. 2   &   \includegraphics[width=0.8\textwidth,valign=m]{example-image}  \\
        \hline
    \end{tabular}
\end{table}
\end{document}

Addendum: If you like to have horizontally centered contents also in the first table's column, than the following changes are necessary:

  • in preamble to be add \usepackage{array}
  • in table parameters the column type p{...} should be replaced by >{\centering}p{...}
2

In the provided example, a minus sign was left off the length .5\totalheight. Graphical images are set above the baseline, when using \includegraphics, whereas tabular cells are centered. Thus, one must move the graphical image down by half its height to compensate.

\documentclass{article}
\usepackage{graphicx}
\begin{document}

\begin{table}[htbp]
    \centering
    \caption{My caption}
    \label{tab:mytab}
    \begin{tabular}{|p{1.0cm}|c|}
        \hline
        \textbf{Number}   & \textbf{Images} \\ \hline
        Nr. 1 & \raisebox{-.5\totalheight}{\includegraphics[width=0.8\textwidth]{example-image}}       \\ \hline
        Nr. 2 & \raisebox{-.5\totalheight}{\includegraphics[width=0.8\textwidth]{example-grid-100x100pt}}       \\ \hline
    \end{tabular}
\end{table}

\end{document}

enter image description here

To get even more precise centering, the downward shift of the image should be compensated by 1/2 the height of a line of text, thus, the shift should proceed as \raisebox{-.5\dimexpr\totalheight-\ht\strutbox}{...}.

Note I also widened the first column to 1.3cm, so as to allow the word Number to fit.

\documentclass{article}
\usepackage{graphicx}
\begin{document}

\begin{table}[htbp]
    \centering
    \caption{My caption}
    \label{tab:mytab}
    \begin{tabular}{|p{1.3cm}|c|}
        \hline
        \textbf{Number}   & \textbf{Images} \\ \hline
        Nr. 1 & \raisebox{-.5\dimexpr\totalheight-\ht\strutbox}{%
          \includegraphics[width=0.8\textwidth]{example-image}}       \\ \hline
        Nr. 2 & \raisebox{-.5\dimexpr\totalheight-\ht\strutbox}{%
          \includegraphics[width=0.8\textwidth]{example-grid-100x100pt}}\\ \hline
    \end{tabular}
\end{table}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.