i have a bunch of images in a folder (used to lighten-up lecture slides). currently, i change the images included by hand when updating the lecture slides. this requires to change either the width or the height of the image to match, say, \textwidth or \textheight. i'm wondering if there's an automatic way that would use the aspect ratio of an image to automatically decide if width or height should be manipulated.

i am also considering using a pseudo-random selection of images from that folder, which means that it will not matter if the aspect-ratio-based width-or-height decision is made on the fly or when pre-computing the list of images (with potentially additional information per image).

(hope this two part problem doesn't go against forum rules :)

  • you can set both width and height and use the key keepaspectratio. Beside this you can naturally measure a graphic before using it and set keys accordingly. Jul 5, 2020 at 16:22

2 Answers 2


Use adjustbox package to achieve this.

\usepackage[export]{adjustbox} % graphicx is also included

Then set both max width and max height whenever using includegraphics.

\includegraphics[max height=\textheight,max width=\textwidth]{image}

This maintains the aspect ratio and adjusts either height or width to the set parameters (\textheight and \textwidth in this case).


As @ulrike-fischer said, you can use the options already available for the \includegraphics command. In particular, you need the keepaspectratio option.

For example, I typically use the command below when I want to fill most of a beamer frame with an image. This works for any image regardless of its aspect ratio or original size.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .