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.

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.

share|improve this question
    
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

3 Answers 3

up vote 19 down vote accepted

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)

\documentclass{article}

\usepackage{graphicx}
\makeatletter
\def\maxwidth#1{\ifdim\Gin@nat@width>#1 #1\else\Gin@nat@width\fi}
\makeatother

\begin{document}

\includegraphics[width=\maxwidth{1in}]{image}

\includegraphics[width=\maxwidth{1cm}]{image}

\end{document}
share|improve this answer
1  
Wow, great solution! –  Stephan Lehmke Dec 10 '12 at 10:57
11  
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
    
@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
\documentclass{article}
\usepackage[demo]{graphicx}
\newlength\MaxWidth \newsavebox\IBox
\MaxWidth=100pt

\newcommand*\IncludeGraphics[2][]{%
  \sbox\IBox{\includegraphics[#1]{#2}}%
  \ifdim\wd\IBox>\MaxWidth \resizebox{\MaxWidth}{!}{\usebox\IBox}%
  \else\usebox\IBox\fi}

\begin{document}

\includegraphics{image}

\IncludeGraphics{image}

\IncludeGraphics[width=50pt]{image}

\end{document}

enter image description here

share|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.

\documentclass{article}

\usepackage{mwe}% just for the example images
\setlength{\parindent}{0pt}

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

\begin{document}

\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}

\end{document}
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.