# Scale image based on the size of another image

I'm currently writing a LaTeX document which describes a technical installation and configuration process of a piece of software. I took two screenshots I want to include as figures in my document. These screenshots have been cropped to emphasise and keep focused on what's important in the context.

What I wanted to achieve was to reduce the size of both screenshots, because when they are inserted simply with \includegraphics{myFigure} without any option both figures are way too large to fit the paper size.

Up to now, in order to get the result I want, I tricked the width of these screenshots manually to make them appear the same real size. For example, for the first figure \includegraphics[width=0.75\textwidth]{myFigure1} and for the second \includegraphics[width=0.9\textwidth]{myFigure2}. I also tried the scale option to resize my images. But this isn't clearly the right solution as this requires checking manually to see if the compiled result looks more or less the same (approximation).

After some researches on the web and several discussions with guys from the IRC #latex channel on freenode, I got some more solutions.

First, I learned from this link that when I don't specify an option, images are assumed to use 72 as a default dpi. This is indeed correct as I recalculated manually the formula presented in that link.

I also learned that I could change the dpi of my figures with an image editor to make them fit the paper size. Contrary to what is written in the link above, there is no need to install a specific software to change the dpi of an image if you have GIMP installed. The latter has indeed such a feature. Go to Image -> Scale Image... and adjust the X and Y resolution after making sure pixels/in is chosen in the drop down menu. A screenshot on my XPS 13 (13") is shot at 97.003 dpi. I changed the dpi to 150 and both images are fitting the paper on the compiled LaTeX document (pdf). What's great is that this does not require to reduce the image I have (PNG) and lose quality. This problem does not occur with vector graphics obviously. While this solution sounds great, this still requires to open an image editor and change the dpi manually. This is rather annoying, even if batch scripting with ImageMagick can come in handy.

The aforementioned link also advices us to use the resolution option of the graphicx package, while there seems to be no option like that contrary to what is written in the documentation. To make sure, I even tried to compile my document with that option, and it failed to compile with it. Even the wikibook is advertising this option!

So my questions are:

• Why does this solution and the LaTeX WikiBook both advertize the resolution option from the graphicsx package while it clearly does not exist? Some specified that this option is only available on certain engines like pdflatex. I tried to verify that assumption, but it does not work.
• Is there any pure LaTeX solution to specify the dpi of figures? I don't want a global command, as I want to be free to define the dpi for each bitmap image I include.
• Ideally, I would like the second figure to have a width equivalent to the size of \textwidth. When a figure is resized, does LaTeX change its dpi to resize it? If true, I want in that case that the first figure takes the dpi of the second figure which has been resized with the value of \textwith. (I don't know if I'm clear for this part.)
• I don't really understand the question. If the original screenshots were on the same machine they presumably have the same dpi, and if you did not change that while cropping, the images you are including will have the same dpi. That will still be true if you don't scale at all (which is usually best for bitmap images) or if you use scale= with the same factor. If you use width= then the images will use different scaling. Nov 28 '14 at 10:07
• You can use \settowidth on an unscaled \includegraphics to determine the "natural" width of the image. I'm not sure what that will tell you about the dpi of a rasterized image. Nov 29 '14 at 14:33
• Thanks both for your answers. I rewrote my question, since after having slept on it, it appeared it was not really clear. David Carlisle, you were the person I wanted to write to as you are directly concerned as the author of graphicx. The other question linked above and the LaTeX WikiBook both seems to make false assumptions: the resolution option does not exist, right?
– wget
Nov 29 '14 at 17:14
• Do you adjust the width manually to eventually have the images have the same height? If so, why not adjust height then?
– Werner
Nov 29 '14 at 17:48
• @Werner. I don't need to specify the height, as specifying the width will scale the image by preserving the aspect ratio. So yes, specifying a width change the height of the image too. And since both figures have NOT the same size, I don't really understand your reflexion then.
– wget
Nov 30 '14 at 0:03

This shows two figures each scaled by the same amount, such that the larger of the two is \linewidth wide:

\documentclass{article}

\setlength\textwidth{.5\textwidth}
\setlength\textheight{1.2\textheight}

\usepackage{graphicx}

\begin{document}

\sbox0{\includegraphics{house.png}}
\makeatletter
\Gscale@div\myscale\linewidth{\wd0}
\makeatother

\noindent X\dotfill X

\begin{figure}[hp]\centering
\includegraphics[scale=\myscale]{house.png}
\caption{a house}
\end{figure}

\begin{figure}[hp]\centering
\includegraphics[scale=\myscale]{man.png}
\caption{a man}
\end{figure}

\noindent X\dotfill X

\end{document}

• Thanks for this answer this really helped me. I know I have not specified this in my initial question (I'll modify it afterwards if this is possible). Do you know a way to apply this code only for the largest image we have in the document? I meant get the size of the largest image I have. Typically, this would be something like: \sbox0{\includegraphics{the largest image I have in my document}}
– wget
Dec 9 '14 at 0:39
• Gonna accept this answer and open a new question for the loop/parsing to search for the largest image.
– wget
Sep 3 '17 at 22:01
• For those searching for this topic, I opened a new follow-up question here: tex.stackexchange.com/q/389654/66964
– wget
Sep 4 '17 at 13:53