Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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
share|improve this answer
    
Thank! I used this solution and works fine because my class is the standard book. –  zunbeltz Mar 8 '12 at 9:58
    
Added a patch for book.cls then to automate the \prevdepth adjustment. If this solution works for you, could you accept the answer? –  Frank Mittelbach Mar 8 '12 at 10:55
add comment

You would have to use the following expression for your image height:

\includegraphics[height=\dimexpr
  \textheight-3\baselineskip-\parskip-.2em-
  \abovecaptionskip-\belowcaptionskip\relax]{image}

which removes from \textheight 3 lines of text = 3\baselineskip, a paragraph skip = \parskip, "roughly" the depth of the last line = .2em*, as well as the skip above and below the caption = \abovecaptionskip and \belowcaptionskip.

This may differ from one document class to the next. For example, this is how article (and the other standard document classes) define \@makecaption (executed by \caption to typeset the actual caption):

\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\belowcaptionskip}

Note the (vertical) skips like \abovecaptionskip, \belowcaptionskip and \par.

For completeness, here's a minimal example, with the image exchanged for a rectangular box (or \rule):

\documentclass{article}
\begin{document}
\begin{figure}
  \rule{\textwidth}{\dimexpr\textheight-3\baselineskip-\parskip-.2em-
    \abovecaptionskip-\belowcaptionskip}
  \caption{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
    elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
    mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.}
\end{figure}
\end{document}​

* The actual depth of the last paragraph depends on the caption contents. However, at 10pt font, the depth is just under 2pt, while 1em is about 10pt, making .2em roughly similar.

share|improve this answer
    
Werner \partopsep is a list environment variable that gets added to the top of a list if it starts out with a paragraph. So it doesn't belong into your calculations. –  Frank Mittelbach Mar 7 '12 at 22:47
    
@FrankMittelbach: Without it I'm out by 1.94444pt and \partopsep is 2.0pt plus 1.0pt minus 1.0pt. So what am I missing otherwise? –  Werner Mar 7 '12 at 23:00
3  
This is the depth in your last line, see update to my answer. You can't account for this in the calculation, it would need to be changed in the caption command. So you are just lucky with \partopsep being close to what you need in this particular case (because of the g or y in the line). –  Frank Mittelbach Mar 7 '12 at 23:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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