0

I have a problem with including graphics in tabularx. When I simply use \includegraphics, the image is right at the top of the cell, covering the separator line. Howeverer, if I use a trick and add e.g. \vspace{-0.2cm}, the image will be moved lower. This seems strange for me. What is a good workaround to vertically center the image? What is an explanation of this effect?

Here is the first code:

\documentclass{beamer}
\usepackage{tabularx}
\usepackage{graphicx}
\setlength\floatsep{0pt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\begin{document}

\begin{frame}

\begin{tabularx}{\textwidth}{|X|X|X|X|}\hline
Kép&Helyszín&Vezető&Vezetett\\\hline
\includegraphics[width=0.2\textwidth]{example-image}&&&\\\hline
\end{tabularx}

\end{frame}
\end{document}

enter image description here

And here is the second:

\documentclass{beamer}
\usepackage{tabularx}
\usepackage{graphicx}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\begin{document}

\begin{frame}

\begin{tabularx}{\textwidth}{|X|X|X|X|}\hline
Kép&Helyszín&Vezető&Vezetett\\\hline
\vspace{-0.2cm}{\includegraphics[width=0.2\textwidth]{example-image}}&&&\\\hline
\end{tabularx}

\end{frame}
\end{document}

enter image description here

1

by default X columns are p columns which are like \parbox[t] so the baseline is placed on the baseline of the first item of the cell.

If there is just an image then the alignment is on the base of the image. If you add a space, even \vspace{0pt} then that puts an item (a zero length glue node) above the image, and so the alignment of the table row is then above the image.

You may prefer X to use m columns which are like \parbox[c] and place the baseline in the vertical centre of the content.

\renewcommand\tabularxcolumn[1]{m{#1}}
3

Easy with the cellspace package, which lets you define minimal vertical padding at the top and bottom of cells in columns with specifier prefixed with the letter S:

\documentclass{beamer}
\usepackage{tabularx}
\usepackage{graphicx, adjustbox}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{4pt}
\setlength{\cellspacebottomlimit}{4pt}
\addparagraphcolumntypes{X}

\begin{document}

\begin{frame}
\setlength{\extrarowheight}{2pt}
\begin{tabularx}{\textwidth}{|S{X}|X|X|X|}\hline
Kép&Helyszín&Vezető&Vezetett\\\hline
\includegraphics[width=0.2\textwidth]{pepe-le-pew2}&&&\\\hline
\end{tabularx}

\end{frame}

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