8

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?
14

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

enter image description here

\documentclass{article} 
\usepackage[export]{adjustbox}% http://ctan.org/pkg/adjustbox
\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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.