[Tex/LaTex] How to display two pictures that have not the same size with the same height

graphics

I use this code to display two pictures:

\begin{figure}[htbp]
\minipage{0.45\textwidth}
\begin{center}
\includegraphics[width=1\textwidth]{methods.png}
\caption{Methods}
\label{fg:methods}
\end{center}
\endminipage\hfill
\minipage{0.1\textwidth}
\begin{center}
\end{center}
\endminipage\hfill
\minipage{0.45\textwidth}
\begin{center}
\includegraphics[width=1\textwidth]{method_detail.png}
\caption{Method detail information}
\label{fg:method_detail}
\end{center}
\endminipage\hfill
\end{figure}

Now I want assure that both pictures are not wider than \textwidth. This I have already achieved. But I'd like that the picture with the higher height is reduced to the height of the smaller image (while preserving the aspect ratio). Then the whole width isn't needed any more for the higher picture, so I'd like to use this width for the smaller picture to make it wider.

I think I could do this by trying to use different width parameters in the includegraphics command, but isn't there a way to do that automatically?

Best Answer

\documentclass{article}
\usepackage{graphicx}
\newsavebox\IBoxA \newsavebox\IBoxB \newlength\IHeight
\newcommand\TwoFig[6]{% Image1 Caption1 Label1 Im2 Cap2 Lab2
  \sbox\IBoxA{\includegraphics[width=0.45\textwidth]{#1}}
  \sbox\IBoxB{\includegraphics[width=0.45\textwidth]{#4}}%
  \ifdim\ht\IBoxA>\ht\IBoxB
    \setlength\IHeight{\ht\IBoxB}%
  \else\setlength\IHeight{\ht\IBoxA}\fi
  \begin{figure}[!htb]
  \minipage[t]{0.45\textwidth}\centering
  \includegraphics[height=\IHeight]{#1}
  \caption{#2}\label{#3}
  \endminipage\hfill
  \minipage[t]{0.45\textwidth}\centering
  \includegraphics[height=\IHeight]{#4}
  \caption{#5}\label{#6}
  \endminipage 
  \end{figure}%
}

This is how you use it:

\begin{document}

  \TwoFig{methods} % image 1
         {Methods} % caption 1
         {fg:methods} % label 1
         {method_detail} % image 2
         {Method detail information} % caption 2
         {fg:method_detail} % label 2

\end{document}

enter image description here