[Tex/LaTex] How to align two images side by side if one label has more lines

captionsgraphicsminipagevertical alignment

I have two same sized images and want them side by side with a caption under them.

My code looks like this:

\begin{figure}[H]
\centering
\begin{minipage}{.5\textwidth}
  \centering
    \includegraphics[width=\textwidth]{3}
    \caption{label1}
  \label{fig:test1}
\end{minipage}%
\begin{minipage}{.5\textwidth}
  \centering
    \includegraphics[width=\textwidth]{4}
    \caption{label2}
  \label{fig:test2}
\end{minipage}
\end{figure}

which works fine until one of the labels has 2 lines, than it looks like this:

enter image description here

and I woud like to prevent it to look something like this:

enter image description here

How can it be done?

Thanks

Best Answer

You need to specify the [t] position specifier for both minipage environments.

A separate observation: All three \centering instructions are redundant.

If you want a bit of separation between the minipage environments, specify their widths as 0.475\textwidth instead of 0.5\textwidth -- and maximize their horizontal separation by inserting an \hfill directive.

enter image description here

\documentclass{article}
\usepackage[demo]{graphicx} % remove 'demo' option in real document

\begin{document}
\begin{figure}
\begin{minipage}[t]{.475\textwidth} % not "0.5\textwidth"
    \includegraphics[width=\textwidth]{3}
    \caption{label1}
    \label{fig:test1}
\end{minipage}%
\hfill % <-- new
\begin{minipage}[t]{.475\textwidth}
    \includegraphics[width=\textwidth]{4}
    \caption{label2 is quite a bit longer than label 1}
    \label{fig:test2}
\end{minipage}
\end{figure}
\end{document}