LaTeX Alive
Auto-resize images that overfill a page

LaTeX’s support for graphics through the graphicx package is reliable and works well, but occasionally I find its options somewhat limiting. One thing that often causes me to stop and immediately recompile a document I’m working on happens when an image I’ve inserted is poorly sized and takes up far more space than it should:

In these sorts of situations I’ll immediately chuck in a quick [width=\textwidth] and merrily continue writing my document (or sometimes [height=0.7\textheight] in a beamer document, where vertical space is often compromised instead).

I’ve often thought of writing some code that wraps around \includegraphics that would check whether the width or height of an included graphic was too large and shrink down the image if and only if necessary. Note that while it is possible to specify

\usepackage{graphicx}
\setkeys{Gin}{width=\textwidth}

this has the side-effect of making all the graphics the width of the text — often just as disastrous when inserting smaller images. Furthermore, after setting this option globally, trying to write height=<whatever> will have unexpected consequences!

Luckily for me, the TeX community is wide and wonderful, and Martin Scharrer has already implemented what I’m after (and more) in the adjustbox package. I’ll leave you to digest the rest of the manual on your own, but here’s a simple declaration in the preamble:

\usepackage[Export]{adjustbox}
\adjustboxset{max size={\textwidth}{0.7\textheight}}

This enables the smart resizing that I’ve always wanted without any change to the rest of the document — large images are resized down but small images are left as they are. There are also max width and max height options in case you do not wish to set both as in the above example.

This may sound like a small thing but I iterate quickly when writing lectures with beamer, where I often use images that need shrinking. Having such an unobtrusive method to automatically avoid doing this manually each and every time will save me perhaps not appreciably in time but certainly in brain space. Thanks Martin!