A large number of screen captures have been scaled 300% at 288dpi. They appear correctly when embedded in OpenOffice, but not LyX.


Images that do not extend beyond the margins (small images) appear perfectly. Images that are too big extend beyond the margins.

Attempt #1


However, that code scales the smaller images to also fit the width, which is undesirable. (The smaller images become distorted.)

Attempt #2

Didn't attempt this, but it looks useful.

% Redefine includgraphics for avoiding images larger than the screen size
% If the size is not specified.

    \ifdim \image@width>\linewidth%

Source: http://svn.python.org/projects/external/Sphinx-1.0.5/sphinx/texinputs/sphinx.sty

Attempt #3

% Resize figures that are too wide for the page


Using graphicx.

Other package suggestions are welcome.


Without adding LaTeX code to every image that exceeds the margins, what can be done to resize just those images that are too big?


The following image is an example of many, many images that must be scaled back to fit the page width:



4 Answers 4


Using the adjustbox package with the export option which extends graphicx with several new keys you can now simply use the max width key which scales the image down to that width if it is larger but doesn't scale it at all if it is smaller the equal to it. There are also the related keys max height, max size (for both width and height) as well as min width, min height and min size.

In your case simply use the following code:

\includegraphics[max width=\linewidth]{<image file name>}

Note that you can't set max width etc. in a global way using \setkeys{Gin}{max width=...}. It is implemented in a different way as width. However, I'm planning to add a possibility for this as well.

  1. Define a macro that calculates whether the image is wider than \linewidth.
  2. If the image is too wide then assign a value of \linewidth to the macro.
  3. Otherwise, assign the value of the graphic input's natural width (\Gin@nat@width).
  4. Redefine \includegraphics to leverage the new macro definition.

% Determine if the image is too wide for the page.

% Resize figures that are too wide for the page.


  • Ah! Guten.Ich fühle mich gut. That is basically the solution that I proposed in my comment below. I just didn't think of using the renewcommand structure as above. Danke.
    – bev
    Nov 28, 2010 at 9:10
  • This does have the effect of ignoring any optional arguments that are passed to \includegraphics.
    – TH.
    Nov 28, 2010 at 11:38
  • Good answer, but you don't need to redefine \includegraphics but you can set the default width using \setkeys{Gin}{width=\ScaleIfNeeded} in the preamble. And if you redefine it please pass the optional argument along! Mar 2, 2011 at 14:28
  • Thanks for the idea. I added a max width option to \includegraphics over the adjustbox package now (CTAN release pending). Here it can be used as \includegraphics[max width=\linewidth]{image}. Jul 4, 2011 at 11:24
  • i assume the (broken) link to l2picfaq would give us the same thing as at ctan:info/l2picfaq/german -- anyone know? Oct 27, 2011 at 19:24

I don't use Lyx, but many people in my lab do, and come to me with all sorts of problems created by Lyx. All of the Lyx-created problems I've seen can only be fixed by editing the latex code manually. YMMV.

This should fix it (you'll need


\ifthenelse{\lengthtest{Gin > \textwidth}}
  • @Dave - (sigh) you're right, Dave. Teach me to wing it without checking. For some reason, at the time it seemed simple. I've just spend an hour looking into it deeper, and all I can say is that I'm sorry that I wasted your time. On the plus side, I know the graphicx and graphics package much better than 60 mins ago. The problem, as I see it now, it that the internal variable \Gin@nat@width is set by subtracting the the \Gin@llx from \Gin@urx. So it seems a simple matter to compare that the \textwidth using \lengthtest, but, this has to be done after \includegraphics is called -->
    – bev
    Nov 28, 2010 at 4:39
  • -->**cont** . So I tried \ifthenelse{1=1}{\def\blah{[width=3in]}{\def\blah{[width=5in]}} then \expandafter\includegraphics\blah{mypic} just to see whether I could put a cs into the space where the optional keyval pairs go. So that worked, but now I'm stuck :-) . What I really want to do is \def\blah{\makeatletter\ifthenelse{\lengthtest{\Gin@nat@width > \textwidth}\makeatother}{[width=\textwidth]}{[]}} but this fails. Anyway, I feel like I owe you a little more time, so unless somebody sets me straight, hang in there.
    – bev
    Nov 28, 2010 at 4:47

Here is another solution, based on the original attempt #2 (from sphinx.sty). What I needed was to only resize images that were too big, in all the others that could fit I wanted to keep the original arguments to \includegraphics.

  \ifdim \image@width>\linewidth%

I can now do:


And in case the myimage scaled down to 70% is still wider than current line it will be scaled down even more to fit the \linewidth otherwise it will stay scaled down to only 70%.

Please note, that this will create a new command \IncludeGraphics rather than modify the \includegraohics. If you want to use \includegraphics instead then you need to add


And change the \newcommand\IncludeGraphics to \renewcommand\includegraphics and inside the body call the \oldincludegraphics.

You must log in to answer this question.

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