1

I would like to center text and images both vertically and horizontally in a longtable. However I can't seem to get it to work unfortunately. A example is below:

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{array,longtable,ragged2e}
\usepackage{graphicx}

\begin{document}

\begin{longtable}{| l | c | c |} \hline & Here is a Text & Here is a Text \\hline Here is a Text & \includegraphics[height=2cm]{img/image.png} & \includegraphics[height=2cm]{img/image.png} \\hline Here is a Text & \raisebox{-\height}{\includegraphics[height=2cm]{img/image.png}} & \raisebox{-\height}{\includegraphics[height=2cm]{img/image.png}} \\hline
\caption{My Caption} \label{tab:FirstTab} \end{longtable} \mbox{}\

\end{document}

I also tried the changes below, but it didn't help:

Here is a Text & \raisebox{-\height}{\includegraphics[height=2cm]{img/image.png}} & \raisebox{-\height}{\includegraphics[height=2cm]{img/image.png}} \\\hline   
leandriis
  • 62,593
Vipr0
  • 13

1 Answers1

2

One way is to use valign macro from the adjusbox package:

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage{ragged2e}
\usepackage{babel}
\usepackage{array,longtable,makecell}
\usepackage[demo,               % in real document delete 'demo'
            export]{adjustbox}  % it load graphicx too

\begin{document}

\begingroup \setcellgapes{3pt} \makegapedcells % add vertical space/above/below cell's content \begin{longtable}{| l | c | c |} \hline & Here is a Text & Here is a Text \\hline Here is a Text & \includegraphics[height=2cm,valign=c]{img/image.png} & \includegraphics[height=2cm,valign=c]{img/image.png} \\hline Here is a Text & \includegraphics[height=2cm,valign=c]{img/image.png} & \includegraphics[height=2cm,valign=c]{img/image.png} \\hline \caption{My Caption} \label{tab:FirstTab} \end{longtable} \endgroup

\end{document}

enter image description here

At using adjustbox, we can replace includegraphics with \adjustimage add determine positions of images in cells with settings in \adjustboxset:

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage{ragged2e}
\usepackage{babel}
\usepackage{array,longtable}
\usepackage[demo,               % in real document delete 'demo'
            export]{adjustbox}  % it load `graphicx` too

\begin{document}

\begingroup \adjustboxset{height=20mm,valign=c, margin=0pt 3pt 0pt 3pt} % <--- \begin{longtable}{| l | c | c |} \hline & Here is a Text & Here is a Text \ \hline Here is a Text & \adjustimage{}{img/image} & \adjustimage{}{img/image} \ \hline Here is a Text & \adjustimage{}{img/image} & \adjustimage{}{img/image} \ \hline \caption{My Caption} \label{tab:FirstTab} \end{longtable} \endgroup

\end{document}

which gives the same result as the first proposed solution. Note, in above MWE are omitted graphics file extensions png. They are necessary if there are available the same images' files with different extensions and among them you want to use only selected one.

Zarko
  • 296,517