Scale (resize) large images (graphics) that exceed page margins

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.
\usepackage{graphicx}

% Determine if the image is too wide for the page.
\makeatletter
\def\ScaleIfNeeded{%
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

% Resize figures that are too wide for the page.
\let\oldincludegraphics\includegraphics
\renewcommand\includegraphics[2][]{%
  \oldincludegraphics[width=\ScaleIfNeeded]{#2}
}

Source


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

\usepackage{ifthen}

\ifthenelse{\lengthtest{Gin > \textwidth}}
{\setkeys{Gin}{width=1.0\textwidth}}{\relax}