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?

  • 3
    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. 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. 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.




\savebox{\tempbox}{\includegraphics{example-image}}% get width of image


resizebox with caption

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
  \ifdim\ht0>#1\relax\errmessage{\string\maxfigure: caption too big}
  \else\centering\maxsizebox{\textwidth}{\dimexpr #1 - \ht0}{#2}\fi
  • 1
    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
  • I replaced \textwidth by \columnwidth in the final piece of code for it to work in a two-column IEEE template.
    – bers
    May 6 at 9:14
  • In addition, I had to subtract \dp0 in addition to \ht0, see examples in tex.stackexchange.com/q/596052. By contrast, at least in IEEE document classes \abovecaptionskip is included in the \caption command, so it's measured by \ht0+\dp0.
    – bers
    May 7 at 6:50

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


See related answer https://tex.stackexchange.com/a/488985/34958

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.