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   
3
  • 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!
    – Zarko
    Nov 7 '20 at 3:30
  • See also: tex.stackexchange.com/q/19080/134144
    – leandriis
    Nov 7 '20 at 9:13
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.

1
  • And to think of all the time I spent going through the graphicx manual looking for valign! Nov 7 '20 at 17:43

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.