Skip to content

Instantly share code, notes, and snippets.

@lanoxx

lanoxx/twoimages.tex

Created Dec 15, 2016
Embed
What would you like to do?
\documentclass{article}
\usepackage{graphicx,calc,lipsum}
\makeatletter
\newcommand*{\DivideLengths}[2]{%
\strip@pt\dimexpr\number\numexpr\number\dimexpr#1\relax*65536/\number\dimexpr#2\relax\relax sp\relax
}
\makeatother
\newsavebox\IBoxA \newsavebox\IBoxB \newlength\IHeight
\newlength\RatioA \newlength\RatioB \newlength\ScaleFactor
\newdimen\Width \newdimen\RatioSum \newdimen\RatioSumInverse \newdimen\TextWidth \newdimen\IWidthA \newdimen\IWidthB
\def\Padding{5pt}
\newcommand\TwoFig[6]{% Image1 Caption1 Label1 Im2 Cap2 Lab2
\sbox\IBoxA{\includegraphics{#1}}\sbox\IBoxB{\includegraphics{#4}}%
\ifdim\ht\IBoxA>\ht\IBoxB\setlength\IHeight{\ht\IBoxA}\else\setlength\IHeight{\ht\IBoxB}\fi
\setlength\RatioA{\DivideLengths{\wd\IBoxA}{\ht\IBoxA}pt}
\setlength\RatioB{\DivideLengths{\wd\IBoxB}{\ht\IBoxB}pt}
\RatioSum=\dimexpr(\RatioA+\RatioB)
\RatioSumInverse=\DivideLengths{1.0pt}{\RatioSum}pt
\Width=\DivideLengths{\IHeight}{\RatioSumInverse}pt
\TextWidth=\dimexpr(\textwidth-\Padding)
\setlength\ScaleFactor{\DivideLengths{\TextWidth}{\Width}pt}%
\IWidthA=\dimexpr(\DivideLengths{\RatioA}{\DivideLengths{1.0pt}{\DivideLengths{\ScaleFactor}{\DivideLengths{1.0pt}{\IHeight}pt}pt}pt}pt-1pt)
\IWidthB=\dimexpr(\DivideLengths{\RatioB}{\DivideLengths{1.0pt}{\DivideLengths{\ScaleFactor}{\DivideLengths{1.0pt}{\IHeight}pt}pt}pt}pt-1pt)
%
\begin{figure}[!htb]\centering%
\minipage[t]{\IWidthA}\centering%
\frame{\includegraphics[width=\IWidthA]{#1}}%
\caption{#2}\label{#3}%
\endminipage\hspace{\Padding}%
\minipage[t]{\IWidthB}\centering%
\frame{\includegraphics[width=\IWidthB]{#4}}%
\caption{#5}\label{#6}%
\endminipage%
\end{figure}%
% Uncomment to see actual values:
% textwidth: \the\textwidth\\
% Width: \the\Width\\
% TextWidth: \the\TextWidth\\
% RatioSum: \the\RatioSum\\
% RatioSumInverse: \the\RatioSumInverse\\
% ScaleFactor: \the\ScaleFactor\\
% Width BoxA: \the\wd\IBoxA\\
% Height BoxA: \the\ht\IBoxA\\
% RatioA: \the\RatioA\\
% Width BoxB: \the\wd\IBoxB\\
% Height BoxB: \the\ht\IBoxA\\
% RatioB: \the\RatioB\\
% IHeight: \the\IHeight\\
% IWidthA: \the\IWidthA\\
% IWidthB: \the\IWidthB\\
}
\begin{document}
\lipsum[1]
\TwoFig{images/image1}{Image 2 Description}{fig:image1}%
{images/image2}{Image 1 Description}{fig:image2}
\lipsum[2]
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment