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?

einpoklum
  • 12,311
  • 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 05 '16 at 09:59
  • 2
    Does http://tex.stackexchange.com/q/312196/15925 help? – Andrew Swann Jul 05 '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 05 '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 05 '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 05 '16 at 10:14
  • un classico is, to use a savebox, measure it's size(s), put them in relation to \textwidth and the result is the factor, the size of the images must be devided by. – MaestroGlanz Jul 05 '16 at 10:30
  • @MaestroGlanz: What do I use for floating-point arithmetic though? Should it be the ftp package mechanisms? – einpoklum Jul 05 '16 at 10:31
  • I dont use floating point, bus do calculations direct on i.e. \dimen255 or a custom temporary length i.e. \newdimen\tempDimA. I.e. \tempDimA=\boxAlength, \advance\tempDimA by \boxBlength, \divide\tempDimA by\textwidth. Now you have the "factor" saved in tempDimA. Next step, you \divide\tempDimB by\tempDimA and \divide\tempDimC by\tempDimA. Now you insert \tempDimB and \tempDimC as the sizes for the images. I cant guarantee that this is working, but I succeeded with similar versions. – MaestroGlanz Jul 05 '16 at 10:40
  • Have a look at this: http://tex.stackexchange.com/questions/53497/how-do-i-get-the-exact-dimension-of-a-picture-in-latex – MaestroGlanz Jul 05 '16 at 10:42

2 Answers2

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}
Heiko Oberdiek
  • 271,626
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}
Andrew Swann
  • 95,762
  • 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 05 '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 05 '16 at 11:29