# Fit float to page

This is a follow-up question to Best figure size adjustment when dealing with different image sizes. I want to resize a figure in a way, that the figure including the caption fits the page. Resizing just the figure has been explained in the previously mentioned question. The solution looked as follws:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\centering
\includegraphics[width=\textwidth,height=\textheight]{AnyGraphic}
\caption{Some random caption. This sholud be long enough to create at least one linebreak, as the overall size of the float naturally depends on the size of the caption}
\end{figure}
\end{document}


Without the caption, everything is fine. But including a caption will result in a Float too large for page warning.
Thus the question: How can I fit a float including the caption to a page?
Note: I want to do this for many floats with captions of different lenghts. I would thus prefer a solution that works for captions of arbitrary lengths

If you use both width and height then you also almost always want to use keepaspectratio otherwise the image will be distorted to match the specified dimensions. For the rest you can do something like:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\centering

\includegraphics[width=\textwidth,height=\dimexpr\textheight-4\baselineskip-\abovecaptionskip-\belowcaptionskip\relax,
keepaspectratio]{ug}
\caption{Some random caption. This should be long enough to create at least one linebreak, as the overall size of the float naturally depends on the size of the caption}
\end{figure}
\end{document}


Or you could measure the caption. I would get a warning that the float is too large by 0.04385pt which looks suspiciously like the depth, but rather than worry about that I just subtracted an extra 2pt for luck. keepaspectratio is commented out so that my test image stretches in this direction.

\documentclass[a4paper,10pt]{scrartcl}
\usepackage{graphicx}
\newsavebox\cpsave
\begin{document}
\begin{figure}
\centering
\savebox\cpsave{\begin{minipage}{\textwidth}\vspace*{\abovecaptionskip}%
\caption{Some random caption. This sholud be long enough to create at least one linebreak, as the overall size of the float naturally depends on the size of the caption}\end{minipage}}

\includegraphics[width=\textwidth,height=\dimexpr\textheight-\ht\cpsave-\dp\cpsave-\lineskip-2pt\relax,
%keepaspectratio
]{ug}

\noindent\usebox\cpsave
\end{figure}
\end{document}

• I usually use keepaspectratio, but to reproduce the warning with ANY figure, I omitted it here, as a wide (width > height) figure would not result in this warning with keepaspectratio. As foor your solution: It works with this caption, but a longer caption will again produce the warning. – Janek Jun 27 '13 at 12:17
• @Janek yes the alternative is to set the caption first in a box and measure it, but do you really need all the images to be that big, if so use \newsavebox\foo in the preamble \savebox\foo{\begin{minipage}{\textwidth}\caption{.... before the image use image then subtract -\ht\foo-\dp\foo in your calculation and use blank line \noindent\usebox\foo after the figure. – David Carlisle Jun 27 '13 at 12:35
• As the used graphics are larger than the a4 page but contain some important details, I would prefer them to be as large as possible. Could you edit the new suggestions into your original answer? When I tried it here, I still got a warning. – Janek Jun 27 '13 at 13:06
• @Janek updated answer – David Carlisle Jun 27 '13 at 13:17