# How to limit the size of figure+caption?

I do have my figures (and tables) sometimes with several lines long caption. I am trying to fill out my pages optimally, so I put the payload content in a \maxsizebox. In most cases it works fine, until a comment is too long, in which case part of the caption goes off-page. As I understand, I cannot place a floating object in a \maxsizebox. Can I limit the height of image+caption somehow?

• you would have a better chance of an answer if you showed a complete document that allowed peopel to see the problem and test possible answers. – David Carlisle Dec 12 '15 at 20:50
• You can put the image and caption inside a minipage, and the minipage inside \maxizebox, and a \maxsizebox inside a float. – John Kormylo Dec 13 '15 at 0:49
• @ John Kormylo I think my problem is that a caption must not be used outside a figure or table, which can float, and so cannot go into a \maxizebox. – katang Dec 13 '15 at 13:42

One could use \newlength and \settowidth instead of \savebox, but this is faster. One could also set the minipage width to \textwidth instead of the image width. The main difference is that this way a long caption will match the image.

\documentclass{article}
\usepackage{mwe}

\newsavebox{\tempbox}

\begin{document}

\begin{figure}
\savebox{\tempbox}{\includegraphics{example-image}}% get width of image
\centering\maxsizebox{\textwidth}{.2\textheight}{%
\begin{minipage}{\wd\tempbox}%
\usebox{\tempbox}
\caption{\blindtext}
\end{minipage}}
\end{figure}

\lipsum[1-6]
\end{document}


Note, it is also possible to resize just the image so that the image plus caption fit into a given height.

\newcommand{\maxfigure}[3]% #1 = max height, #2 = image, #3 = caption
{\bgroup
\sbox0{\begin{minipage}{\textwidth}\caption{#3}\end{minipage}}%
\ifdim\ht0>#1\relax\errmessage{\string\maxfigure: caption too big}
\else\centering\maxsizebox{\textwidth}{\dimexpr #1 - \ht0}{#2}\fi
\par\box0
\egroup}

• That last bit of code is awesome, thank you! Note for floatrow users who want to use it: You need to put \RawCaption around the \caption, and you probably need to include - \abovecaptionskip in the \dimexpr. (Maybe you need to account for \abovecaptionskip regardless of floatrow.) Also, I put the \label in the minipage with the \caption and it seems to work. – dsedivec Aug 24 '17 at 23:22

Alternatively, you can use the \usepackage[figuresleft]{rotating} package for this, it automatically scales.

Or a less elegant but shorter manual solution is to use the scale option

\includegraphics[scale=0.6]{figure.png}