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.

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

share|improve this question

2 Answers 2

up vote 12 down vote accepted
\newcommand{\lwincludegraphics}[2][]{%
  \sbox{0}{\includegraphics[#1]{#2}}%
  \ifdim\wd0>\linewidth
    \resizebox{\linewidth}{!}{\box0 }%
  \else
    \leavevmode\box0
  \fi}

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

share|improve this answer
    
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 –  cheshirekow Sep 2 '11 at 15:09

You could use the following condition within your figures:

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

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
  \ifthenelse{\dimtest{\wd0}>{#1}}%
    {\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:

\maxwidth{\includegraphics{tiger}}

This would yield the same output as above

share|improve this answer
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. –  cheshirekow Sep 2 '11 at 15:13
    
@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 Sep 2 '11 at 15:17

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.