In particular, when I include a graphic, I would like it to have width=\linewidth if it's width is greater than \linewidth, but I'd like it to have \scale=1 if it is smaller than the line width.

In other words, I'd like my images to only shrink if they wouldn't otherwise fit.

I know that generating a document generally involves iterating on the typesetting, but when creating internally-shared documents that aren't going to be published, I'm not too concerned about how exactly perfect the layout looks. I'd just like it to be decent the first time around and not have to worry about iterating on it.nally-share

2 Answers 2

    \resizebox{\linewidth}{!}{\box0 }%

Use \lwincludegraphics as you would \includegraphics.

(The similarity with Werner's answer is evident, but having a proper command that allows for setting in the optional argument is better.)

  • Thanks a ton, this works great. I made one change. I used "\usebox0" instead of "\box0" in the else statement. I'm not sure why, but the latter messes up centering, while the former does not. Oh, and I actually did this as well: "\let\oldincludegraphics\includegraphics" and "\renewcommand{\includegraphics}{...}" so that the rest of my document doesn't have to change. Thanks again Commented Sep 2, 2011 at 15:09

You could use the following condition within your figures:

\usepackage[showframe]{geometry}% http://ctan.org/pkg/geometry
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{xifthen}% http://ctan.org/pkg/xifthen
  \setbox0=\hbox{\includegraphics{tiger}}% Store image in box0
    {\includegraphics[width=\linewidth]{tiger}}% Scale image to fit within \linewidth
    {\usebox0}% Use original (unaltered in width) image
  \caption{This is a tiger, rawr.}

Tiger image

The geometry package was added merely to highlight the page dimension via the showframe package option. The actual image width of tiger is 550.68999pt > 430.004462pt = \linewidth.

You could also combine the above code in a macro. For example, consider the macro \maxwidth[<max hlen>]{<stuff>}:

\newcommand{\maxwidth}[2][\linewidth]{% \maxwidth[<max hlen>]{<stuff>}
  \setbox0=\hbox{#2}% Store image in box0
    {\resizebox{#1}{!}{#2}}% Scale object to fit within \linewidth
    {\usebox0}% Use original (unaltered in width) object

The first (optional) argument <max hlen> specifies the maximum horizontal length of the object <stuff> (the second, mandatory, argument). The default, if no <max hlen> is specified, is \linewidth. You would use it in the following way:


This would yield the same output as above

  • 1
    Thanks a ton for your very detailed answer and the image example. I wish I could mark two answers, but I like that the other one is a little simpler and I could renew the include graphics command. Commented Sep 2, 2011 at 15:13
  • 1
    @cheshirekow: That was the motivation behind my second option - writing the \maxwidth macro. There you could use \maxwidth{\includegraphics[...]{tiger}} where you specify '...'. Either way, it works for you.
    – Werner
    Commented Sep 2, 2011 at 15:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .