[Tex/LaTex] Two images with different size, side by side, automatic computation

calculationsgraphics

The following Latex macro is taken from this answer. In this macro both the image width and the minipage width are hard-coded to .45. The issue with that is that the resulting two images are not exactly taking up the whole \textwidth but are a little bit smaller. The actual result depends on the aspect ratios of both images.

It should instead be possible to just specify the padding that we want between the images and then have Latex compute the optimal width of each image using the following calculation:

height = maximum of the height of both images
ratio1 = aspect ratio of image1
ratio2 = aspect ratio of image2
scaleFactor = (textwidth - padding) / 
   ((height*ratio1) + (height*ratio2))

width1 = (scaleFactor * height) * ratio1
width2 = (scaleFactor * height) * ratio2

How can I adjust the following macro to achieve that?

\newsavebox\IBoxA \newsavebox\IBoxB \newlength\IHeight
\newcommand\TwoFig[6]{% Im1 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}%
}

Update
Here are two example images of different size that can be used to demonstrate my problem.

Picture 1:
Khan Academy Dummy Picture 1

Picture 2:
Khan Academy Dummy Picture 2

Best Answer

Here is an example with lualatex which makes it easier to use mathematical operations. The horizontal lines are only for some demonstration:

\documentclass{article}
\usepackage{graphicx}
\newsavebox\IBoxA \newsavebox\IBoxB \newlength\Padding
\setlength\Padding{10pt}
\makeatletter
\newcommand\TwoFig[6]{% Image1 Caption1 Label1 Im2 Cap2 Lab2
  \sbox\IBoxA{\includegraphics{#1}}\sbox\IBoxB{\includegraphics{#4}}%
  \ifdim\ht\IBoxA>\ht\IBoxB
    \savebox\IBoxB{\includegraphics[height=\ht\IBoxA,keepaspectratio]{#4}}%
  \else
    \savebox\IBoxA{\includegraphics[height=\ht\IBoxB,keepaspectratio]{#1}}%
  \fi
  \edef\TotalWidth{\strip@pt\dimexpr\wd\IBoxA+\wd\IBoxB}%
  \edef\LineScale{\directlua{tex.sprint(\strip@pt\linewidth/\TotalWidth)}}%
%
  \begin{figure}[!htb]\centering
    \minipage[t]{\dimexpr\LineScale\wd\IBoxA-0.5\Padding}\centering
      \frame{\includegraphics[width=\linewidth]{#1}}
      \caption{#2}\label{#3}
    \endminipage\hfill
    \minipage[t]{\dimexpr\linewidth-\LineScale\wd\IBoxA-0.5\Padding}\centering
      \frame{\includegraphics[width=\linewidth]{#4}}
      \caption{#5}\label{#6}
    \endminipage 
  \end{figure}%
}
\makeatother
\begin{document}
\noindent\rule{0.5\linewidth}{1pt}\rule{0.5\linewidth}{2pt}

\bigskip
\TwoFig{/tmp/pic0}{Image 1}{fg:im1}%
            {/tmp/pic1}{Image 2}{fg:im2}  

\noindent\hrule

\centering\frame{\includegraphics[scale=0.25]{/tmp/pic0}}\hspace{\Padding}%
\frame{\includegraphics[scale=0.25]{/tmp/pic1}}

\end{document}

enter image description here