With \includegraphics I can give the width of the resulting image (which would be scaled).

How can I give a maximum width of the image? That is, if the original is smaller than X, I'd like to keep the smaller image, if the image is larger than X, then the image width should be X.

Using pdflatex/graphicx.

  • I think you need to program that explicitly. – Stephan Lehmke Dec 10 '12 at 10:07
  • 2
    You mean like maxwidth in ConTeXt :) – Aditya Dec 10 '12 at 10:08
  • 1
    See package adjustbox – Joseph Wright Dec 10 '12 at 10:09
  • @JosephWright I think I'll try adjustbox first (works fine), but when the distribution is too old, I'll just go for @ StephanLehmke's answer. So you could post your comment as an answer and I'll accept it. Thanks! – topskip Dec 10 '12 at 10:22
  • @Herbert's answer made it too simple for me to copy/paste ... – topskip Dec 10 '12 at 10:33

For more exotic requirements adjustbox or resizebox are useful but you don't really need any additional commands for this (other than a local command just to access @ commands more easily)


\def\maxwidth#1{\ifdim\Gin@nat@width>#1 #1\else\Gin@nat@width\fi}




| improve this answer | |
  • 15
    It would have been even greater if the person who wrote the graphics package had exposed \Gin@nat@width by a local definition of \width as for other latex2e box commands to save needing the \makeatletter dance. Now, who was that.... – David Carlisle Dec 10 '12 at 11:02
  • 1
    @DavidCarlisle: I had the same thought when I studied graphics/x while writing adjustbox. There I provide the original width as \width. – Martin Scharrer Feb 2 '13 at 8:14
\newlength\MaxWidth \newsavebox\IBox

  \ifdim\wd\IBox>\MaxWidth \resizebox{\MaxWidth}{!}{\usebox\IBox}%






enter image description here

| improve this answer | |

The adjustbox provides a max width key just for this purpose. If the package is loaded with the export option this key and most others can also be used for \includegraphics.

Here the content is only resized if its current size is larger than the given width. It works basically the same way as the code in David Carlisle's answer. If the \adjincludegraphics macro is used the original width can directly be accessed as \width. Note that this feature is not enabled for \includegraphics by the export option. However this can be done by making \includegraphics using \adjincludegraphics instead using \let\includegraphics\adjincludegraphics after loading adjustbox.


\usepackage{mwe}% just for the example images

\usepackage[export]{adjustbox}% 'export' allows adjustbox keys in \includegraphics


\hrule% to see \linewidth

\includegraphics[max width=\linewidth]{example-image-1x1}

\includegraphics[max width=\linewidth]{example-image-a4}

% With \adjincludegraphics (or \adjustimage) you can also use the original width as \width:
\adjincludegraphics[width=\ifdim\width>\linewidth \linewidth\else\width\fi]{example-image}

| improve this answer | |

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.