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. Commented Nov 7, 2020 at 3:17
  • Welcome to TeX.SE!
    – Zarko
    Commented Nov 7, 2020 at 3:30
  • See also: tex.stackexchange.com/q/19080/134144
    – leandriis
    Commented Nov 7, 2020 at 9:13

1 Answer 1

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! Commented Nov 7, 2020 at 17:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .