Question

I have a command which includes an includegraphics command - I can pass an image to my command, and it will do some standard formatting for me before actually including the image. Some of the images that I'm including via this command are smaller than \textwidth, while some are larger. I'd like to scale the larger images down to \textwidth, while not scaling the smaller images up - this means I can't just do

\includegraphics[width=\textwidth]{img}

Is there a way to specify a maxwidth? Or, can I get the width of the image somehow so I can do something like

\ifthenelse{\imagewidth > \textwidth}{%
    \includegraphics[width=\textwidth]{img}}{%
    \includegraphics{img}}
Was it helpful?

Solution

To get the width of the image you can use this code:

\newlength{\imgwidth}
\settowidth{\imgwidth}{\includegraphics{img}}

You could use this in the document preamble to create a new command to automatically set the width:

\usepackage{graphicx}
\usepackage{calc}

\newlength{\imgwidth}

\newcommand\scalegraphics[1]{%   
    \settowidth{\imgwidth}{\includegraphics{#1}}%
    \setlength{\imgwidth}{\minof{\imgwidth}{\textwidth}}%
    \includegraphics[width=\imgwidth]{#1}%
}

and then, in your document:

\scalegraphics{img}

I hope this helps!

OTHER TIPS

I like an additional parameter for optionally scaling the image down or up a bit, so my version of \scalegraphics looks like this:

\newcommand\scalegraphics[2][]{%
    \settowidth{\imgwidth}{\includegraphics{#2}}%
    \setlength{\imgwidth}{\minof{#1\imgwidth}{\textwidth}}%
    \includegraphics[width=\imgwidth]{#2}%
}

The adjustbox package is usefull for this. Below you will find a short example. It allows the following (besides triming, clipping, adding margins and relative scaling:

\documentclass[a4paper]{article}


\usepackage[demo]{graphicx}
\usepackage[export]{adjustbox}

\begin{document}

\adjustbox{max width=\linewidth}{\includegraphics[width=.5\linewidth,height=3cm]{}}

\adjustbox{max width=\linewidth}{\includegraphics[width=2\linewidth,height=3cm]{}}

\includegraphics[width=2\linewidth,height=3cm,max width=\linewidth]{}
\end{document}

If you use the export package option most of its keys can be used directly with \includegraphics. FOr instance the key relevant to you, max width.

If what you want to constrain is not an image but a standalone .tex file, you can slightly modify ChrisN's \scalegraphics to

\newlength{\inputwidth}
\newcommand\maxwidthinput[2][\linewidth]{%   
    \settowidth{\inputwidth}{#2}%
    \setlength{\inputwidth}{\minof{\inputwidth}{#1}}%
    \resizebox{\inputwidth}{!}{#2}
}

and then use it like so

\maxwidthinput{\input{standalone}}
\maxwidthinput[0.5\textwidth]{\input{standalone}}

And of course, adjustbox as suggested by ted will work as well:

\usepackage{adjustbox}
...
\adjustbox{max width=\linewidth}{\input{standalone}}

After a few minutes of searching through CTAN manuals and Google results, I think I can safely say that what you want to do is either impossible or very hard. My only recommendation is that you have two commands, one for small images and one for large, or one command with an option.

There may be a way, but I leave it to other S.O. LaTeX wizards to provide a better answer.

Edit: I am wrong, see above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top