[Tex/LaTex] How to force the same resizing factor for two \includegraphics

floatsgraphicsresizewidth

I have two images which I want to place side-by-side as subfigures (with different captions). The images are not of the same size, but they have elements whose size must be kept equal (e.g. text). I want the total width of the figure to be the \textwidth, and to resize both images by the same factor to achieve this. However, from what I know, size is set individually for \includegraphics… what do I do?

Best Answer

The scale factor for the images can be calculated:

\documentclass[a5paper]{article}
\usepackage{graphicx}

% Space between images
\newdimen\imgsep
\setlength{\imgsep}{1em}


\begin{document}

\makeatletter
\sbox0{\includegraphics[page=1]{img}}
\sbox2{\includegraphics[page=2]{img}}
\edef\ImgScaleFactor{%
   \strip@pt
   \dimexpr 1pt
     * \number\dimexpr \linewidth - \imgsep \relax
     / \number\dimexpr \wd0 + \wd2 \relax
   \relax
   % Uses the higher precision of eTeX's scaled operation
   % (multiplication followed by division)
}
\typeout{Image scale factor: \ImgScaleFactor}
\makeatother

\hrule % shows text/line width
\vspace{1ex}

\noindent
\includegraphics[scale=\ImgScaleFactor, page=1]{img}\hfill
\includegraphics[scale=\ImgScaleFactor, page=2]{img}

\vspace{1ex}
\hrule

\end{document}

Result

The two-page image file img.pdf was generated by pdflatex with the following source:

\documentclass{article}
\usepackage{xcolor}
\pagecolor{black!15!white}
\setlength{\pdfhorigin}{0pt}
\setlength{\pdfvorigin}{0pt}
\setlength{\fboxrule}{0pt}
\begin{document}
\newcommand*{\x}[1]{%
  \sbox0{\fbox{#1}}%
  \setlength{\pdfpagewidth}{\wd0}%
  \setlength{\pdfpageheight}{\dimexpr\ht0+\dp0\relax}%
  \shipout\copy0 %
}
\x{First image}
\x{Second graphics}
\end{document}
Related Question