[pdftex] Autoscaling included images
Jens-Uwe Morawski
morawski at gmx.net
Thu Dec 13 12:46:57 CET 2001
On Tue, 11 Dec 2001 10:46:50 -0500 (EST)
Jona Aron Andersen <janders at deas.harvard.edu> wrote:
> When including an image in PDFTeX it occasionally happens that the image
> I'm trying to include is larger than the page, and thus overruns the
> margins.
>
> What I would like (PDF)TeX to do in case the image is too large, is scale
> the image so that it will fit on the page.
Maybe following can help you:
\documentclass[pdftex]{article}
\usepackage{graphicx,ifthen,calc}
\newsavebox{\TempGraphic}
\newlength{\maxWidth}
\newlength{\maxHeight}
\newlength{\hasWidth}
\newlength{\hasHeight}
% set this lengths to required maximum values
\setlength{\maxWidth}{\textwidth}
\setlength{\maxHeight}{\textheight}
% use \FitGraphics{<filename>} instead of
% \includegraphics{<filename>}
\newcommand{\FitGraphics}[1]{%
\sbox{\TempGraphic}{\includegraphics{#1}}%
\settowidth{\hasWidth}{\usebox{\TempGraphic}}%
\settoheight{\hasHeight}{\usebox{\TempGraphic}}%
\ifthenelse{\lengthtest{\maxWidth < \hasWidth}\or%
\lengthtest{\maxHeight < \hasHeight}}%
{%
\setlength{\hasWidth}{1pt*\ratio{\maxWidth}{\hasWidth}}%
\setlength{\hasHeight}{1pt*\ratio{\maxHeight}{\hasHeight}}%
\ifthenelse{\lengthtest{\hasWidth < \hasHeight}}%
{\resizebox{\maxWidth}{!}{\usebox{\TempGraphic}}}%
{\resizebox{!}{\maxHeight}{\usebox{\TempGraphic}}}}%
{\usebox{\TempGraphic}}}
\begin{document}
% an example how to use the macro \FitGraphics;
% replace "test" with a filename of an existing graphic
% and play with the lengths \maxHeight and \maxWidth
\setlength{\unitlength}{1mm}
\begin{picture}(150,150)
\multiput(0,0)(10,0){16}{\line(0,1){150}}
\multiput(0,0)(0,10){16}{\line(1,0){150}}
\put(0,0){\FitGraphics{test}}
\end{picture}
\end{document}
More information about the pdftex
mailing list