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?
Asked
Active
Viewed 2,093 times
4
-
3you 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 00: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
2 Answers
4
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{adjustbox}
\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}
John Kormylo
- 79,712
- 3
- 50
- 120
-
1That last bit of code is awesome, thank you! Note for
floatrowusers who want to use it: You need to put\RawCaptionaround the\caption, and you probably need to include- \abovecaptionskipin the\dimexpr. (Maybe you need to account for\abovecaptionskipregardless offloatrow.) Also, I put the\labelin theminipagewith the\captionand it seems to work. – dsedivec Aug 24 '17 at 23:22 -
I replaced
\textwidthby\columnwidthin the final piece of code for it to work in a two-column IEEE template. – bers May 06 '21 at 09:14 -
In addition, I had to subtract
\dp0in addition to\ht0, see examples in https://tex.stackexchange.com/q/596052/. By contrast, at least in IEEE document classes\abovecaptionskipis included in the\captioncommand, so it's measured by\ht0+\dp0. – bers May 07 '21 at 06:50
0
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}
See related answer https://tex.stackexchange.com/a/488985/34958
Mzq
- 259
