3

Assumed we have two images with different heights. Both images are aligned to the top via valign=t.


Minimum working example (MWE):

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

\begin{document}

\begin{figure}[h]
    \centering
    \subfloat[This is image A]{\includegraphics[width=0.45\textwidth, valign=t]{example-image-16x10}}\qquad
    \subfloat[This is image B]{\includegraphics[width=0.45\textwidth, valign=t]{example-image-10x16}}\\
    \caption{Caption for images A and B}
\end{figure}

\end{document}

Screenshot of the result:

Screenshot of the result


Description of the issue:

Unfortunately the corresponding caption also moves up to the top with this command.

While both images should remain at the top, the left image caption should move down to the red dashed elevation line of the other caption for better appearance.

How to do so?

Dave
  • 3,758
  • Related questions using the subcaption package: https://tex.stackexchange.com/q/152818/134144 and the floatrow package: https://tex.stackexchange.com/a/389970/134144 – leandriis Mar 02 '19 at 12:36

3 Answers3

3

Alignment generally involves using baselines, but in this case you need to add a gap between the image and the caption. You will also need to compute the difference between the heights of the two images.

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}

\begin{document}

\begin{figure}[htp]
    \centering
    \sbox0{\includegraphics[width=0.45\textwidth]{example-image-16x10}}% measure image
    \sbox1{\includegraphics[width=0.45\textwidth]{example-image-10x16}}% measure image
    \begin{subfigure}{\wd0}
      \usebox0%
      \vspace{\dimexpr \ht1-\ht0}% fill space
      \caption{This is image A}
    \end{subfigure}\qquad% I prefer \hfil
    \begin{subfigure}{\wd1}
      \usebox1
      \caption{This is image B}
    \end{subfigure}
    \caption{Caption for images A and B}
\end{figure}

\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
2

floatrow is recommended by Overleaf "for advanced caption managing of floating elements (tables and figures)".

It requires changing the environment a bit, but don't be put off by the apparent added complexity because it works very well.

Example:

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{floatrow}

\begin{document}

\begin{figure}[h!] \centering \floatsetup{heightadjust=all, valign=t} \ffigbox{ \begin{subfloatrow} \ffigbox[\FBwidth]{ {\includegraphics[width=0.45\textwidth]{example-image-16x10}} {\caption{This is image A} } \ffigbox[\FBwidth]{ {\includegraphics[width=0.45\textwidth]{example-image-10x16}} {\caption{This is image B} } \end{subfloatrow} } {\caption{Caption for images A and B}} \end{figure}

\end{document}

Image of working example

Some features of this method:

  • You can swap valign=t for valign=c if you want the images vertically centred
  • The subcaptions will be centred on the relevant image, thanks to \FBwidth. If the subcaptions are longer than the image width, they'll wrap
Bernard
  • 271,350
1

A solution with minipages and subcaption package:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage[export]{adjustbox}
\usepackage{lipsum}

\begin{document}
\lipsum[1]    
\begin{figure}[h]
    \centering
    \begin{minipage}[t]{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth, valign=t]{example-image-16x10}
    \end{minipage}\hspace{\fill}
    \begin{minipage}[t]{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth, valign=t]{example-image-10x16}
    \end{minipage}
    \begin{minipage}[t]{0.45\textwidth}
    \captionof{subfigure}{This is image A}
    \end{minipage}\hspace{\fill}
    \begin{minipage}[t]{0.45\textwidth}
    \captionof{subfigure}{This is image B}
    \end{minipage}
    \caption{Caption for images A and B}
\end{figure}

\end{document}

enter image description here

Edit: Adding parentheses in subcaptions' label and a vertical space between images and captions:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\captionsetup[subfigure]{labelformat = parens}
\usepackage[export]{adjustbox}
\usepackage{lipsum}

\begin{document}
\lipsum[1]    
\begin{figure}[h]
    \centering
    \begin{minipage}[t]{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth, valign=t]{example-image-16x10}
    \end{minipage}\hspace{\fill}
    \begin{minipage}[t]{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth, valign=t]{example-image-10x16}
    \end{minipage}
    \vspace{5pt}

    \begin{minipage}[t]{0.45\textwidth}
    \captionof{subfigure}{This is image A}
    \end{minipage}\hspace{\fill}
    \begin{minipage}[t]{0.45\textwidth}
    \captionof{subfigure}{This is image B}
    \end{minipage}
    \caption{Caption for images A and B}
\end{figure}

\end{document}
koleygr
  • 20,105
  • Thanks a lot for your help! Unfortunately the subcaption of the right image is aligned very close to the image now. How to repair this? – Dave Mar 02 '19 at 12:53
  • Welcome... See edit. The advantage of minipages in figures and tables (with the help of subcaption package) is the full control of the placements and sizes – koleygr Mar 02 '19 at 13:00