56

I want to create a table with multiple rows of the following form: a cell that contains text and then multiple cells that contain images. The text should be vertically centered and the contents of every column should also be centered horizontally. I am aware of the m{} column type but somehow this seems to fail if an image is contained in the row and determines the row height. Any ideas?

Minimal example:

\documentclass{article}
\usepackage{graphicx}
\usepackage{array}
\begin{document}
\begin{figure}
\begin{tabular}{m{3cm}c}
(a) & \includegraphics{some_picture.pdf}\\
\end{tabular}
\end{figure} 
\end{document}
1
  • Welcome to tex.sx! A tip: you can use backticks ` to mark your inline code as I did in my edit. Also, it isn't really a custom here to sign with a "thanks". Finally a suggestion: Maybe change your username to something more telling than "user5506"?
    – doncherry
    May 26, 2011 at 9:15

8 Answers 8

89

The baseline of an image is at the bottom. You could use \raisebox to shift it up. Use half of \height, which stands for the height of the box to be raised.

\raisebox{-.5\height}{\includegraphics{some_picture}}

For example:

\documentclass{article}
\usepackage{mwe}
\begin{document}
\begin{tabular}{lll}
\raisebox{-.5\height}{\includegraphics[scale=0.25]{example-image}} & text & text\\
\end{tabular}
\end{document}

yields:

enter image description here

0
57

The new graphbox package introduces a new (vertical) align key which can be used in \includegraphics options. Using it there's no need for \raisebox, \parbox or taking care of distinguishing between c or m columns in tabulars.

\documentclass{article}
\usepackage{mwe}
\usepackage{graphbox} %loads graphicx package
\begin{document}
\begin{tabular}{ccc}
text & \includegraphics[scale=0.25]{example-image} & text\\
text & \includegraphics[align=c,scale=0.25]{example-image} & text\\
text & \includegraphics[align=t,scale=0.25]{example-image} & text\\
\end{tabular}
\end{document}

enter image description here

As egreg reminded me, adjustbox package provides a similar funcionality. Loading it with export option, a new key valign={t|m|b|T|M|B} can be used as \includegraphics option.

\documentclass{article}
\usepackage{mwe}
\usepackage[export]{adjustbox}
\begin{document}
\begin{tabular}{ccc}
text & \includegraphics[valign=m,scale=0.25]{example-image} & text \\
\end{tabular}
\end{document}
4
  • 3
    The same functionality (with the key valign) is available with \usepackage[export]{adjustbox}.
    – egreg
    Sep 5, 2014 at 10:41
  • @egreg Thank you for reminding me about adjustbox.
    – Ignasi
    Sep 5, 2014 at 12:03
  • And thank you for advertising a new package!
    – egreg
    Sep 5, 2014 at 12:08
  • adjustbox vetical-aligns more accurately than graphbox. Try the code of this answer tex.stackexchange.com/a/564995/2288 with graphbox and notice the difference.
    – Diaa
    Jan 16, 2021 at 16:00
7

use the m column type for the image, eg:

\begin{tabular}{c m{4cm}}
3

Based on the answer of Stefan Kottwitz I wrote this little snippet to make it a little easier to implement:

\newcommand{\tabfigure}[2]{\raisebox{-.5\height}{\includegraphics[#1]{#2}}}

To be used as:

\documentclass{article}
\usepackage{mwe}
\newcommand{\tabfigure}[2]{\raisebox{-.5\height}{\includegraphics[#1]{#2}}}
\begin{document}

    \begin{tabular}{cc}
    a bit of text &    \tabfigure{width=0.20\textwidth}{example-image}
    \end{tabular}

\end{document}

enter image description here

PS: wanted to write is as comment because it's basically the same answer, but I don't have enough rep yet...

3

An alternative solution with tblr environment of the new LaTeX3 package tabularray: use stretch=0 and set head alignment (h) for images.

\documentclass{article}

\usepackage{graphicx}
\usepackage{mwe}
\usepackage{tabularray}

\begin{document}

\begin{tblr}{
  stretch = 0,
  colspec = {Q[h,l]ll},
  hlines,
}
  \includegraphics[scale=0.25]{example-image} & Alpha & Sigma \\
\end{tblr}

\end{document}

enter image description here

3

Here is a solution for:

  • Wrapped text in text cells
  • Manual linebreaks within text cells
  • Images and text both vertically centered

Uses makecell and tabularx.

\documentclass{article}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{makecell}

\begin{document}

\begin{table}[h]
    \begin{tabularx}{\textwidth}{m{3cm} c}
        \includegraphics[width=\linewidth]{image.png} & 
        \makecell[X]{
            Here is \newline
            some manually broken text \newline
            And here is some text that has been automatically wrapped by the use of X in the makecell argument
        }
    \end{tabularx}
\end{table} 

\end{document}

Image of working example

Explanation:

\begin{tabularx}{\textwidth}{m{3cm} X}

The tabularx m argument creates a column of the paragraph (text-wrap) type with the specified width, with the contents vertically aligned in the middle. This solves the vertical centering problem for the image.

The X argument to tabularx and makecell makes the cell or column stretch to fill any remaining space.

\makecell[X]

Makecell creates a cell that we can manipulate as a whole, which makes alignment easier.

1
  • This is exactly what I needed for text that has manual line breaks and also wraps over lines, thank you!
    – mxbi
    Mar 19, 2022 at 22:41
0

A variant of @Herbert's answer that gives vertically and horizontally centered column alignment is the following. (This is my preferred method when not using \includegraphics.)

\begin{tabular}{
  >{\centering\arraybackslash}m{.4\linewidth} % col width
  >{\centering\arraybackslash}m{.4\linewidth} % col width
}
% an image, tikz picture, etc.
&
% another image, tikz picture, etc.
\end{tabular}
0

Wrapping the image in the \parbox environment also seems to work.

\begin{tabular}{cc}
xxx & yyy\parbox{0.6\textwidth}{\includegraphics[scale = 0.6]{example_image}} \\
\end{tabular}

This will vertically center both xxx and yyy with the image.

2
  • 2
    HI and welcome. Please, develop your answer by giving an example of use or an MWE. How to make a “minimum example”
    – AndréC
    Aug 9, 2020 at 18:23
  • 1
    It would be preferable to give fully compilable code starting with documentclass{} and ending with end{document} so that everyone can easily test your solution.
    – AndréC
    Aug 9, 2020 at 18:31

You must log in to answer this question.

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