# Setting a global limit for image dimensions

I have a collection of images of different sizes. I would like to set a limit on their width, such that if the image is wider than this limit it will be scaled down, but if it is smaller then the image is inserted at its native size. Right now the only way I know how to manipulate the image width is with:

\includegraphics[width=SOMEWIDTH]{path/to/my/image}


But this sets the width regardless of the current image size.

Two questions:

• is it possible to limit the image width as described for a single image?
• is it possible to configure a global setting so that a limit is applied to all images, without need to use [width=SOMEWIDTH] on individuals?
-
Related: Shrink figure only when necessary? (the linked question doesn't provide a "global setting"). – Werner Nov 16 '13 at 3:45

You can redefine \includegraphics to always set a maximum width of \linewidth with the aid of adjustbox:

\documentclass{article}
\let\oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[2][]{%
\oldincludegraphics[#1,max width=\linewidth]{#2}}
\begin{document}

\noindent
X \dotfill X

\noindent
\includegraphics[width=2\textwidth]{example-image-a}
\end{document}


adjustbox's settings are exported to \includegraphics, which then allows you to use max width=<len> to set the maximum width of the included graphic. Anything wider than that will be shrunk; anything narrower than that will remain at that (natural) width.

-