24

I'm trying to align the tops of two images using the subfigure environment from the subcaption package. Unless I'm misinterpreting the other questions, I believe I have read multiple questions asking the same, Align by the top borders of figures when using subcaption and How to prevent offset in subcaption / subfigure are two examples. The problem is, those answers don't seem to work as I expected, and I still can't get the tops of the images to be aligned.

example:

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{mwe}
\begin{document}
    \begin{figure}
        \centering
        \begin{subfigure}[t]{0.45\textwidth}
            \includegraphics[width=\linewidth, height=20em]{example-image-a}
            \caption{taller image}
        \end{subfigure}
        \begin{subfigure}[t]{0.45\textwidth}
            \includegraphics[width=\linewidth, height=15em]{example-image-b}
            \caption{shorter image}
        \end{subfigure}
        \caption{two images}
    \end{figure}
\end{document}

For me this generates the following: Result Image

I would have expected it to align the tops of the images. How do I do this?

8
  • 4
    Add \vskip 0pt at the very beginning of each subfigure environment.
    – jub0bs
    Commented Jul 1, 2014 at 17:36
  • @Jubobs that did it. Why is that necessary to get what I assume to be the expected behavior of the [t] option?
    – Matt
    Commented Jul 1, 2014 at 17:49
  • 1
    The subfigure environment is based on the minipage environment, so the following question and answers are relevant to your problem: tex.stackexchange.com/questions/34166/…
    – jub0bs
    Commented Jul 1, 2014 at 17:50
  • This seems a bug of the subcaption package - Submmit to @axel-sommerfeldt
    – skpblack
    Commented Jul 1, 2014 at 18:07
  • Put \caption{...} before of \includegraphics{...} but caption appears above of figures. I didn't find an easy solution
    – skpblack
    Commented Jul 1, 2014 at 19:11

2 Answers 2

22

The problem is that [t] aligns to the first baseline ([b] to the last baseline) of the included paragraph, not the top of the content. The baseline of images lies on there lower edge, so [t] has basically no effect with two images. Lower the images to include the baseline on top or add an empty line above them (using e.g. \vskip 0pt) to make it work.

Further information on this problem can be found in Understanding minipages - aligning at top. Note that subfigure uses a minipage (or the equivialent?) internally.

2
  • 2
    Nowadays I would recommend the adjustbox package for this. With it the images can be align with multiple different ways: adding valign=T for both images for example or adding raise=5em in this case to the second image. Commented Feb 6, 2019 at 20:17
  • adjustbox works for me, while vskip doesn't.
    – Dmitry
    Commented Dec 8, 2021 at 9:40
12

Newer solution

For anyone who come across this issue with subcaption package >= v1.2, there is a built-in argument for that:

\begin{subcaptionblock}[⟨outer-pos⟩][⟨height⟩][⟨inner-pos⟩]{⟨width⟩} ... Beside the ⟨outer-pos⟩ values of ‘c’, ‘t’, and ‘b’, the subcaption package also offers the values ‘T’ and ‘B’ additionally which align the subfigure at the very top resp. bottom. (In contrast ‘t’ and ‘b’ align the subfigure at the top resp. bottom baseline.)

Basically \begin{subfigure}[t]{0.45\textwidth} aligns the top of the two caption text boxes. So not effective as both of your subcaption texts are of the same height. \begin{subfigure}[T]{0.45\textwidth} aligns the very top of the two outer box (the entirety within subcaption), which suits your need.

1
  • @Matt please consider making this solution the accepted answer. :) Simple and yet solves exactly the problem! Commented Mar 21 at 9:40

You must log in to answer this question.

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