# LaTeX - how to detect scale of \includegraphics

I have a general question, so without any example code.

I'm implementing part of document containing gallery of images.

Generally, I have two types of images:

1) document, typically scanned A4, which shall be displayed to full page, required scaling is to keep maximum width and detect the height: `\includegraphics[width=\textwidth]{image.jpg}`

2) photo, which shall be scaled down to maximize amount of images at one page - height is fixed and width is detected: `\includegraphics[height=4cm]{image.jpg}`

For most images it works OK, but for very rare exceptions I have problem with non-standard dimensions:

1) very high document which doesn't fit to page height if I specify width=\textwidth

2) very wide photo which doesn't fit to text width if I specify height=XXcm

Question is: how to implement something like below description?

1) `width = \textwidth` if height fits page height, otherwise scale image (= keep aspect ratio) to `image height = {page height - some spare buffer for caption}`

2) `height = {some height}` if width fits \textwidth, otherwise scale image to image `width = \textwidth`

Note: dimensions of images can vary from image to image and all above cases could be randomly in one chapter.

• `\includegraphics[width=\textwidth, height=.75\textheight,keepaspectratio]{..}` – David Carlisle Jan 1 at 21:47

If you specify both height and width keys, and specify `keepaspectratio` then the values are taken as upper bounds and the image is scaled so that both height and width fit within the specified areas, so you need something like
``````\includegraphics[width=\textwidth, height=.75\textheight,keepaspectratio]{..}
• Is `.75` a random number or does it have any special meaning? – manooooh Jan 2 at 2:29