5

I have the following figure:

\begin{figure}[ht!]
    \centering
    \begin{tabular}{|c|c|}
        \includegraphics{combo} & \includegraphics{combomenu}
    \end{tabular}
\caption{A combo box with and without showing the menu.}
\label{fig:combos}
\end{figure}

which produces:

enter image description here

The left image is significantly shorther than the right image. How do I vertically align the left image to the top of the cell?

I would already overheat Google with my searches on how to do it, if it would be possible. Nothing I do just seems to have any effect on it:

  • \includegraphics{combo}\hfill does nothing
  • \begin{tabular}{|p{3cm}|c|} does nothing, apart from setting the columnt width
  • \raisebox{4.5\height}{\includegraphics{combo}} indeed moves the image up and down, but I just cannot seem to get the parameter right. Something between 5.5 and 6 it seems. But that's really not what I want, if I decide to change the second image, I would have to go through the painstaking alignment process again.
  • Using minipages aligns the image to the top, but also adds more space to the right from both images and maybe pushes the images too far up, so that the vertical lines begin slightly off:

enter image description here

I specified the images' width in pixels, they are both 82 pixels wide.

7

The simplest way is to use the adjustbox package:

\documentclass{article}
\usepackage[demo]{graphicx} % just to provide mock figures
\usepackage{adjustbox}

\begin{document}

\begin{tabular}{|c|c|}
\adjustbox{valign=t}{\includegraphics[width=1cm,height=.5cm]{x}} &
\adjustbox{valign=t}{\includegraphics[width=1cm,height=3cm]{x}} \\
\end{tabular}

\end{document}

enter image description here

Without any package,

\begin{tabular}{|c|c|}
\raisebox{\dimexpr-\height+\ht\strutbox}{\includegraphics[width=1cm,height=.5cm]{x}} &
\raisebox{\dimexpr-\height+\ht\strutbox}{\includegraphics[width=1cm,height=3cm]{x}} \\
\end{tabular}

would do the same.

  • @JakubZaverka: If you load adjustbox (v0.8 or later) with the export option you can use the valign key directly with \includegraphics, after the size keys. – Martin Scharrer Feb 5 '13 at 22:51
  • This works, but a note about something I needed some time to discover: you have to apply adjustbox to all cells in a row even if you only want to change the vertical alignment of one of them. – rumtscho Jan 13 '17 at 16:50
2

Another minipage solution with a few more optional arguments.

\documentclass{article}
\usepackage{graphicx}
\usepackage{mwe} %< -- For dummy images

\begin{document}%
\begin{figure}
    \centering
    \begin{tabular}{|c|c|}
        \begin{minipage}[b][6cm][t]{2cm} %[minipage place][height][content align]{width}
        \includegraphics[width=2cm,height=1cm]{example-image-a}%
        \end{minipage}
            & \includegraphics[width=2cm,height=6cm]{example-image-b}
    \end{tabular}
\caption{A combo box with and without showing the menu.}
\label{fig:combos}
\end{figure}
\end{document}

enter image description here

1

I would put both \includegraphics commands inside minipages as follows:

\begin{minipage}[t]{<width-of-image>}
    \vspace{-\baselineskip}\par
    \includegraphics{combo}
\end{minipage}
&
\begin{minipage}[t]{<width-of-image>}
    \vspace{-\baselineskip}\par
    \includegraphics{combomenu}
\end{minipage}

Though I might be wrong.

0

Variation of egreg answer:

\documentclass{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}% tested with version 1.0

\begin{document}%
    \begin{figure}
\centering
    \begin{tabular}{cc}
\includegraphics[width=2cm,height=1cm,valign=t]{example-image-a}%
    &   \includegraphics[width=2cm,height=6cm,valign=t]{example-image-b}
    \end{tabular}
\caption{A combo box with and without showing the menu.}
\label{fig:combos}
    \end{figure}
\end{document}

gives the same result as shown in egreg answer

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.