# What is a good maximum height for an image?

I would like to have figures within my document which are as big as possible without breaking the aspect ratio and without breaking the layout. This means the content (including the caption) should still be above the footer. I could, of course, just manually change the height until it fits, but I wonder if there is a better option.

If this doesn't work for the general case, is there a solution that works assuming there are not more than two lines for the caption?

Currently, I use

keepaspectratio,width=\linewidth,height=\paperheight


as default options for images, but I guess there is something better than \paperheight?

While writing this question I found \textheight which is much better, but there is still a problem due to the caption.

## Minimal example

### Code

\documentclass[a4paper]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\usepackage[a4paper, left=1cm, right=1cm, top=2cm, bottom=2cm]{geometry}

\usepackage{lastpage}
\usepackage{graphicx}
\usepackage[pagestyles]{titlesec}
\usepackage{float}

\newpagestyle{mystyle}{%
\setfoot{This is left}{middle}{Page \thepage{} of \pageref*{LastPage}}}
\pagestyle{mystyle}

\begin{document}
% https://de.wikipedia.org/wiki/Datei:Burj_Khalifa.jpg
\begin{figure}[H]
\centering
\includegraphics[keepaspectratio,width=\linewidth,height=\paperheight]{Burj_Khalifa.jpg}
\caption{A caption}
\end{figure}
\end{document}


### Rendered

Effectively, you should use \textheight, not \paperheight. If there are a caption for the figure use a fraction of that length as .8\textheight or \dimexpr\textheight-5\baselineskip (text height minus five lines) for a caption of 4 lines or to be more precise, in theory, \dimexpr\textheight-\abovecaptionskip-4\baselineskip, but in practice use \abovecaptionskip could produce a float a bit too large. However, you hardly will notice this (<2pt with the default 10pt font size, 0.13pt at 12pt, nothing at 11pt...). Of course, always assuming normal lines without large characters or inline maths increasing the line height.

\documentclass[a4paper]{article}
\usepackage[showframe]{geometry} % to show document layout
\usepackage{graphicx,lipsum}
\begin{document}
\begin{figure}
\centering\includegraphics[
keepaspectratio,
width=\linewidth,
height=\dimexpr\textheight-7\baselineskip]%
{example-image-9x16}
\caption{\protect\lipsum*[2]}
\end{figure}
\end{document}


If one assumes exactly two lines for the caption, \textheight-34.02pt is about the largest hight that will fit on the page.

How the value is derived: use an image with \textheight and look into the .log file, there will be a warning how overfull the vbox is (about 34.02pt in case of a two line caption with your setup).

\documentclass[a4paper]{article}
\usepackage[showframe]{geometry} % to show document layout
\usepackage{graphicx}
\begin{document}
\begin{figure}
\centering\includegraphics[keepaspectratio,
width=\linewidth,height=\dimexpr\textheight-34.02pt\relax]{example-image-9x16}
\caption{text text text text text text text text text text text text text text text text text text text text text text text text }
\end{figure}
\end{document}