[Tex/LaTex] Height of figure + caption textheight

floats

I have a figure with a 3 line caption. I would like to insert the figure as tall as possible. If I use

\includegraphics[height=\textheight]{file}

The figure is as high as the textblock and the caption is at the footnote position.
Is it possible to set the height the figure, so the caption is aligned with the bottom of the textblock?

Best Answer

That is possible but it depends on many factors so that there is no single formula available that you can apply. It depends on

  • the formatting used for the caption
    • default caption format adds \abovecaptionskip above the caption and \belowcaptionskip below it
    • if your class uses other conventions or you use some float formatting packages different spaces might get added
    • the font used within the caption will have a certain \baselineskip
  • there will be a paragraph end after your graphics which will add \parskip
  • ... plus anything I have forgotten

But assuming the above is the situation then some calculations should get you to the right value:

\usepackage{calc}
\newlength\graphht
\newcommand\calculategraphicstargetheight[1]{%
     \setlength\graphht{\textheight 
                       -\parskip
                       -\abovecaptionskip -\belowcaptionskip
                       -(12pt * #1) % assuming baselineskip of 12pt in caption
                       }}

and then use this value when including the graphics. For flexibility I placed it into a command so that you can vary the number of caption lines.

Note: If you want to have the base of the caption text align with the textblock you should either set \belowcaptionskip to zero (in which case we do not really need to substract it above) or explicitly back up after the caption by that amount in case you want to use it in other circumstances.

In addition you have to add \vspace{-\prevdepth}below the caption because LaTeX turns the float into a box with the bottom line never being the baseline of the last line (i.e., the caption line in this case) so without this correction any character protruding below the baseline will shift the whole material upwards. This can't be included into the calculations because it depends on the material in the last caption line. It could be only added to the definition of the caption, e.g., \@makecaption in standard LaTeX.

Here is an example, based on the definition from book.cls:

\makeatletter
\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
  \sbox\@tempboxa{#1: #2}%
  \ifdim \wd\@tempboxa >\hsize
    #1: #2\par
  \else
    \global \@minipagefalse
    \hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
  \fi
  \vskip-\prevdepth           % add  this line
 % \vskip\belowcaptionskip    % drop this line
 }
 \makeatother