9

If I have to pictures of different height and the same width, how can I dock them to the top that both pictures are vertically aligned. This is my code so far:

\begin{figure}[h]
    \centering
       \subfloat[Part 1]{%
          \includegraphics[width=0.4\textwidth]{pic1.png}%
          \label{fig:left}%
       }
       \subfloat[Part 2]{%
          \includegraphics[width=0.4\textwidth]{pic2.png}%
          \label{fig:middle}%
       }
       \caption{Stuff will be here.}
       \label{fig:default}
\end{figure}
14

Sub-float package assumes that it is important to vertically align the captions, and therefore typically relies on a baseline alignment of the sub-float content.

Using adjustbox's valign=t option, the image's (the sub-float content) regular baseline alignment could be altered to now be at the top. However, you may also want to insert an appropriately-sized companion for the shorter image... do that by using \vphantom{<larger image>}:

enter image description here

\documentclass{article}

\usepackage[export]{adjustbox}
\usepackage{subfig}

\begin{document}

\begin{figure}
  \centering

  \subfloat[Part 1]{%
    \includegraphics[width=0.2\textwidth,valign=t]{example-image-a}%
  } \quad
  \subfloat[Part 2]{%
    \includegraphics[width=0.2\textwidth,valign=t]{example-image-10x16}%
  }

  \subfloat[Part 1]{%
    \includegraphics[width=0.2\textwidth,valign=t]{example-image-a}%
    \vphantom{\includegraphics[width=0.2\textwidth,valign=t]{example-image-10x16}}%
  } \quad
  \subfloat[Part 2]{%
    \includegraphics[width=0.2\textwidth,valign=t]{example-image-10x16}%
  }

  \caption{Stuff will be here.}
\end{figure}

\end{document}
  • 4
    For those who are looking for center alignment, make all valign=c – Jo Kachikaran Nov 6 '16 at 11:48
  • I spent an afternoon and a morning struggling with that. That's exactly what I needed, I couldn't thank you enough. – Right leg Jul 20 '17 at 9:22
  • did anyone get? Package keyval Error: valign undefined. ...xtwidth,valign=t]{figures/circuitth.png}} – qrtLs May 29 at 11:36
  • 1
    @ItsmeJulian: You need \usepackage[export]{adjustbox} in your preamble. Did you add that? – Werner May 29 at 13:58

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.