# Resize large images that exceed page margin whilst respecting existing scale

The solution posted here Scale (resize) large images (graphics) that exceed page margins works great. Just to summarise, Dave Jarvis suggested

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


but unfortunately this breaks any scaling you apply in Lyx to an image. e.g. I have an image resized as so

\includegraphics[scale=0.5]{nicecats}


My scaling of 0.5 gets removed by the macro suggested in the solution above. I'm wondering if there is a way to have the macro apply any user specified scaling first and then check the width? Or perhaps check for any pre-specified scaling and if it exists do not resize. Many thanks.

-
Welcome to tex.sx! Note that it is unusual around here to sign your questions (as there is already a box with your username below it) or to have any greeting. – lockstep Mar 2 '11 at 12:28
you do not need the \makeatletter ... \makeatother LyX does it already for you if it is in the user part of the preamble. – Herbert Mar 2 '11 at 14:14

use it this way, without the \ScaleIfNeeded

\newsavebox\IBox
\let\IncludeGraphics\includegraphics
\renewcommand\includegraphics[2][]{%
\sbox\IBox{\IncludeGraphics[#1]{#2}}%
\ifdim\wd\IBox>\linewidth\resizebox{\linewidth}{!}{\usebox\IBox}\else\usebox\IBox\fi}


your parameters are first applied, then it is tested if greater than \linewidth

-

There is no need to redefine \includegraphics. The default width can be set globally as described in the manual section 4.6 Global setting of keys:

\documentclass{article}

\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

\setkeys{Gin}{width=\ScaleIfNeeded}

\begin{document}

\begin{minipage}{2cm}
\includegraphics{smiley}
\end{minipage}

\begin{minipage}{10cm}
\includegraphics{smiley}
\end{minipage}

% Overwrite global width setting:
\includegraphics[width=.5\textwidth]{smiley}

% Overwrite global width setting: '!' = normal width
\includegraphics[scale=.2,width=!]{smiley}

\end{document}


This allows you to overwrite the default width setting if required. Note that scale and width don't play nice together, so you have to decide on which one to use.

If you really want to mix width and scale you have to wrap the \includegraphics manually into a \resizebox (also provided from graphics/x package). Note the different \ScaleIfNeeded macro here which could be in-lined anyway because it is only used once.

\documentclass{article}

\usepackage{graphicx}

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

\let\origincludegraphics\includegraphics
\renewcommand*\includegraphics[2][]{%
\resizebox{\ScaleIfNeeded}{!}{\origincludegraphics[#1]{#2}}%
}

\begin{document}

\begin{minipage}{2cm}
\includegraphics[scale=10]{smiley}
\end{minipage}

\begin{minipage}{10cm}
\includegraphics{smiley}
\end{minipage}

\includegraphics[width=.5\textwidth]{smiley}

\includegraphics[scale=.2]{smiley}

\end{document}

-