Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?
share|improve this question
    
Related: Shrink figure only when necessary? (the linked question doesn't provide a "global setting"). –  Werner Nov 16 '13 at 3:45

1 Answer 1

up vote 12 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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