[Tex/LaTex] Scale image based on the size of another image

graphics

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.)

Best Answer

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

enter image description here

\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}