# Vertically fit a figure with caption to page

I have a very large(heightwise) figure and would like to fit it on a page with its caption, that I place below (document wide style which I would like to keep, and the figure fills the line anyhow, so placing the caption on the side is not a solution). Is there any way to scale the figure to fit the page with the caption? (I amaware of scalebox, but I would like to avoid scaling the captiontext).

\documentclass{report}
\usepackage{floatrow}
\usepackage[demo]{graphicx}
\begin{document}
\begin{figure}
\includegraphics[height=1.2\textheight]{dummy}
\caption{this should be in text}
\end{figure}
\end{document}

-
Can't you just change 1.2\textheight to e.g. 0.9\textwidth, or something like that? –  Torbjørn T. Sep 11 '13 at 14:24
@TorbjørnT. : this is a MWE, I use a tikz picture. I could try guessing some height(!) however I would like to scale my picture down as little as possible. And I hope that there is a tex style of way to say "fit this figure (with its caption and floatfoot) into the page bounds". (In short: my actual figure does not have height, and I am trying not to guess the apropriate scaling factor. –  ted Sep 11 '13 at 14:38
Ah, I see, a tikzpicture is a little different, wouldn't have hurt to mention that. (Oh, and not that it matters, but I did of course mean 0.9\textheight in my previous comment.) –  Torbjørn T. Sep 11 '13 at 14:43

You could work out how much space is taken up by your float style and adjust accordingly, or you could just let latex do it for you.

If you use

  \includegraphics[height=\dimexpr\textheight\relax]{dummy}


LaTeX reports

LaTeX Warning: Float too large for page by 22.2pt on input line 9.


so if you use

  \includegraphics[height=\dimexpr\textheight-22.2pt\relax]{dummy}


It is happy.

-

Use LaTeX to calculate the height automatically, and scale the image appropriately:

\documentclass{report}
\usepackage{floatrow}
\usepackage[demo]{graphicx}
\usepackage{calc}
\usepackage{fp}
\begin{document}
\begin{figure}
\newlength{\spacearoundfigures}        % It should be possible to calculate this value
\setlength{\spacearoundfigures}{124pt} % Include \abovecaptionskip and \belowcaptionskip\
\newsavebox{\figurebox}
\savebox{\figurebox}{\includegraphics[height=1.2\textheight]{dummy}}
\newsavebox{\captionbox}
\savebox{\captionbox}{this should be in text}% N.B. only works for one-line captions add code to allow line breaking and limit width to \textwidth less figure horizontal space
\newlength{\cbh}
\settototalheight{\cbh}{\usebox{\captionbox}}
\newlength{\fbh}
\setlength{\fbh}{\textheight}
\makeatletter\FPdiv\result{\strip@pt\fbh}{\strip@pt\textheight}\makeatother
\scalebox{\result}{\usebox{\figurebox}}
\caption{\usebox{\captionbox}}
\end{figure}
\end{document}

-
I get LaTeX Warning: Float too large for page by 123.86978pt on input line 21. –  David Carlisle Sep 11 '13 at 16:47
Missed that. Naturally, one would also have to include into one's calculations the space used thus far on the page, the space above and below figures, &c. (which is left as an exercise for the reader --- I added a hard-coded value to remove the warning message) –  WillAdams Sep 11 '13 at 16:50
A multiline caption will give a bunch of problems. Can you see why? –  egreg Sep 12 '13 at 17:04
Yes, one would need to add in code to limit the width of the caption box to \textwidth less any horizontal spacing around the figure. –  WillAdams Sep 13 '13 at 12:37