95

I would like to define a maximum height and a maximum width for an image at same time. I suppose there must be a package to do this, but actually I'm unable to find it...

xarmengol
  • 1,147
  • 1
  • 10
  • 8

4 Answers4

137

You can use the keepaspectratio key and define your graphics as:

\includegraphics[width=10cm,height=10cm,keepaspectratio]{image}

The MWE is as follows:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=2cm,height=3cm,keepaspectratio]{./graphics/amato}
\includegraphics[width=2cm,height=3cm,keepaspectratio]{./graphics/cardtrick}
\end{document}

enter image description here

Experiment with images with different aspect ratios to see the effect.

yannisl
  • 117,160
  • 9
    -1, he asked about max allowable width of image (so actual image width may be lower), but in your answer we set width. – Ivan Kush Dec 07 '16 at 10:56
  • @IvanKush a good question to ask is why is this the accepted answer... – Art Apr 25 '22 at 04:13
  • This answer is the solution because the image width is constrained to be up to width (and similarly for height) and the image will shrink as needed in order to satisfy the aspect ratio. – MD004 Apr 12 '23 at 22:34
51

If you load the package adjustbox with the export option, its keys are usable with \includegraphics: put the following in the preamble

\usepackage[export]{adjustbox} % also loads graphicx

and then use

\includegraphics[max height=<dimension>,max width=<dimension>]{file}

In this way the image will be scaled at the maximum size so that its width and its height will not exceed the two stated dimensions and no distortion will be made.

egreg
  • 1,121,712
  • 1
    if you want to set width AND max width, then you can do it like this with the adjustbox package: \includegraphics[width=300, max width=\textwidth]{file}. Don't put the max width first, it has to come after width. – bersling Dec 08 '16 at 08:18
  • @IvanKush Also, the number of views of this question, currently 254k, is extremely big. – muzimuzhi Z Jan 17 '19 at 01:47
7

You can use graphicx package as below.

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{imagefile.pdf}
\caption{Caption of the figure}
\end{figure}
\end{document}

imagefile.pdf is the filename of your figure file and it can be .png, .jpg......etc.

  • 5
    Do you really need keepaspectratio when you only specify either width or height but not both? – kiss my armpit Mar 08 '12 at 13:36
  • 1
    When only width or height is specified the aspect ratio is automatically maintained by TeX. I have corrected the MWE accordingly. Thank you for noting it. Some how I over looked the same. –  Mar 08 '12 at 13:39
  • 3
    @HarishKumar This can lead to problems. Suppose you have an image equal to textwidth height but 1.5textheight it will disappear at the bottom and vice versa. Best to use both and keepaspectratio. – yannisl Mar 08 '12 at 15:46
  • 5
    -1, he asked about max allowable width of image (so actual image width may be lower), but in your answer we set width. – Ivan Kush Dec 07 '16 at 10:54
-2

In text (for instance IEEE with 2 columns) you can use:

\begin{figure}[!t]
\centering{\includegraphics[width=9cm,height=5cm,keepaspectratio]{Figure}} 
\caption{type your caption}
\label{give a label if you want to refer to it in the text}
\end{figure} 
TeXnician
  • 33,589
Kazem
  • 1
  • 5
    Welcome to TeX.SX! This is what the accepted answer already says (because the important part is the includegraphics line). Btw: \centering does not take an argument. – TeXnician Feb 13 '18 at 10:04