Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a script which automatically creates a beamer presentation with several .ps images, each one inserted in a distinct frame. Those images consist on graphs built using GraphViz. The script is basically iterating on this code snippet:

\begin{frame}{Image 1}



I have some images with large widths, like this one (reduced the size and quality for best viewing):

An image with large width

So the above code works like a charm. But I do have small images as well, like this one:

A small image

If I leave the \includegraphics command with no adjustments, my images will become way bigger than the frame itself. If I put width=1\textwidth, it will work fine with large width images, but not with small ones.

Is there a way to find the best figure size adjustment when using \includegraphics, so my images will be displayed correctly? I usually do that manually, but in this case it's a script which simply adds the image to the .tex body and compile it. I'm pretty sure I'm missing something obvious here, but couldn't figure out what it is.

share|improve this question

1 Answer 1

up vote 18 down vote accepted

graphicx provides the boolean value keepaspectratio. It's explained in the documentation as follow:

If set to true then specifying both width and height (or totalheight) does not distort the figure but scales such that neither of the specified dimensions is exceeded.

share|improve this answer
Thanks a lot! I made a few adjustments based on your answer and voilà, it worked perfectly! –  Paulo Cereda May 4 '11 at 16:54
keepaspectratio is a new one for me. Exactly what I need, too. –  Matthew Leingang May 4 '11 at 17:12
Just in case someone needs the same for text: The adjustbox package allows to use \adjustbox[width=\linewidth,height=\textheight,keepaspectratio]{<text>}. You might want to use totalheight instead of height. Alternatievly you can use \resizebox{<width>}{<height>}{<text>} (or \resizebox* to use totalheight instead) but this requires \Gin@isotrue locally to enable the keepaspectratio feature. You could write is as \csname Gin@isotrue\endcsname to avoid issues with the @. –  Martin Scharrer May 25 '11 at 11:21
@Martin: Thanks for the update! I'd probably ask the very same thing for text, you read my mind. Living and learning. =) –  Paulo Cereda May 25 '11 at 11:43
@Marco @Martin Is there something similar for floats? If I use \begin{figure}[p] \includegraphics[width=\linewidth,height=\textheight,keepaspectratio]{picture} \caption{somecaption} \end{figure} then the graphic will be scaled to fit the page, but the float might still produce an overfull hbox because of the caption. –  Janek Jun 20 '13 at 11:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.