# Center text and images both vertically and horizontally in longtable

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

• You should use \raisebox{\dimexpr 0.5\ht\strutbox-0.5\dp\strutbox-0.5\height}{...}. The \strutbox stuff locates the center of text, not the baseline. Nov 7 '20 at 3:17
• Welcome to TeX.SE! Nov 7 '20 at 3:30

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'

\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}


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
\hline

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.