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?

  • Here are two ideas: 1) use an image editor (e.g. mspaint) and put the two pictures next to each other, to have a single image; 2) put the two images next to each other in the same \scalebox{} environment. – Matsmath Jul 5 '16 at 9:59
  • 2
    Does tex.stackexchange.com/q/312196/15925 help? – Andrew Swann Jul 5 '16 at 10:01
  • @Matsmath: See edit... I'd like to have different captions. I was actually thinking I could use TeX itself to obtain the images' original widths, then calculate the necessary scaling factor. – einpoklum Jul 5 '16 at 10:02
  • You can measure width of images with use of \savebox as you can see in by @AndrewSwann given link. Than you can calculate scale factor. – Zarko Jul 5 '16 at 10:12
  • @AndrewSwann: I'll try to hack something together based on the answers at the link and Zarko's suggestion. But can you tell me which of the answers you think I should base my attempt on? – einpoklum Jul 5 '16 at 10:14

The scale factor for the images can be calculated:


% Space between images


   \dimexpr 1pt
     * \number\dimexpr \linewidth - \imgsep \relax
     / \number\dimexpr \wd0 + \wd2 \relax
   % Uses the higher precision of eTeX's scaled operation
   % (multiplication followed by division)
\typeout{Image scale factor: \ImgScaleFactor}

\hrule % shows text/line width

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




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

  \shipout\copy0 %
\x{First image}
\x{Second graphics}
| improve this answer | |

Here is a solution based mostly on Werner's answer to Store and reproduce effective graphics scaling.

We use the fp library to do the calculations, storing the widths of the images in temporary variables.

Sample output



  \cs_new_eq:NN \calc \fp_eval:n

\newcommand{\scalefactor}[3]{% image1, image2, marco-name-for-result


% The next two lines print the original images for demonstration purposes
% and so should be omitted in final code



% Here we compute the scale factor, display it (again just for 
% demonstration purposes) and then use the resulting 
% value to produce the correctly scaled images


Calculated figure scale: \figscale

    \ffigbox[\FBwidth]{\caption{First figrue}\label{fig:1}}%
    \ffigbox[\FBwidth]{\caption{Second figure}\label{fig:2}}%

| improve this answer | |
  • Wait, doesn't your source typeset the images twice? ... oh, ok, never mind, it wasn't supposed to produce just the example above, nevermind. – einpoklum Jul 5 '16 at 11:13
  • Yes the source typesets them twice, once at the original size just for demonstration purposes, and then in the way you requested. I'll add some comments to the code for clarity. – Andrew Swann Jul 5 '16 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.