2

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
2

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}
| improve this answer | |
1

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

\documentclass{article}

\usepackage{xparse,graphicx,etoolbox,caption,floatrow}

\ExplSyntaxOn
  \cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

\makeatletter
\newcommand{\scalefactor}[3]{% image1, image2, marco-name-for-result
  \settowidth{\@tempdima}{\includegraphics{#1}}%
  \settowidth{\@tempdimb}{\includegraphics{#2}}%
  \setlength{\@tempdima}{\dimexpr\@tempdima+\@tempdimb+2em\relax}
  \csedef{#3}{\calc{\strip@pt\textwidth/\strip@pt\@tempdima}}
}
\makeatother

\begin{document}

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

\includegraphics{example-image-1x1}

\includegraphics{example-image-16x10}

% 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

\scalefactor{example-image-1x1}{example-image-16x10}{figscale}

Calculated figure scale: \figscale

\begin{figure}
  \begin{floatrow}
    \ffigbox[\FBwidth]{\caption{First figrue}\label{fig:1}}%
    {\includegraphics[scale=\figscale]{example-image-1x1}}
    \ffigbox[\FBwidth]{\caption{Second figure}\label{fig:2}}%
    {\includegraphics[scale=\figscale]{example-image-16x10}}
  \end{floatrow}
\end{figure}

\end{document}
| 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.