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?
Asked
Active
Viewed 1,839 times
2
einpoklum
- 12,311
2 Answers
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}
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.
\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


\scalebox{}environment. – Matsmath Jul 05 '16 at 09:59\saveboxas you can see in by @AndrewSwann given link. Than you can calculate scale factor. – Zarko Jul 05 '16 at 10:12\textwidthand the result is the factor, the size of the images must be devided by. – MaestroGlanz Jul 05 '16 at 10:30ftppackage mechanisms? – einpoklum Jul 05 '16 at 10:31\dimen255or 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\tempDimAand\divide\tempDimC by\tempDimA. Now you insert\tempDimBand\tempDimCas 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