Dynamic scaling of figure width

I recently found the following Latex code snippet in the web and thought it is worth to share it with you. If you have large figures that stretch over the linewidth or textwidth, whatever you prefer and want to reduce them automatically, the following piece of code will do it for you:

%Preamble
\usepackage{graphicx}
\makeatletter
\def\ScaleIfNeeded{%
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother

%Document content
\begin{document}
...
\includegraphics[width=\ScaleIfNeeded]{Bild}
...
\end{document}

This piece of code was published here under the GNU Free Documentation License, Version 1.2.

One Comment

  1. Will Robertson

    Nice. This would be handy to add as a keyval option such as

    \includegraphics[maxwidth=\linewidth]{foobar}

    with a convenient method to specify this as a default option. Perhaps someone should write a package for CTAN 🙂

    I can’t really comment whether this sort of thing would ever have a chance of being incorporated into graphicx directly, however.

Leave a Reply