# How can I inject the proper amount of vertical space between captions and figures?

In my source document, I have an image and a code listing, separated by some text. When I generate PDF using pdflatex, the image is just above the listing, and it looks like the caption for the image is the caption for the listing, i.e., the caption for the image is closer to the listing than to the image. Here is the shortest example that I could recreate the problem with.

\documentclass{book}
\usepackage[pdftex]{graphicx}
\usepackage{listings} % code listings
\begin{document}

before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before

before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before

before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before
before before before before before before before before before before

\begin{figure}[!htbp]
\centering
\includegraphics[keepaspectratio,width=4cm,clip,trim = 0mm 6cm 15cm 0mm]
{Images/XMLElement}
\caption{Constructor removal in XMLElement}
\label{XMLElement}
\end{figure}

between between between between between between between between
between between between between between between between between

\begin{figure}[!htb]
\lstset{language=Java, showspaces=false,
showstringspaces=false, tabsize=2, breaklines=true}
\begin{lstlisting}[frame=single]
public IXMLElement createElement(String fullName) { }
\end{lstlisting}
\caption{Example2}
\end{figure}

after after after after after after after after after after after
after after after after after after after after after after after
after after after after after after after after after after after
after after after after after after after after after after after

\end{document}


The image below shows the output for the source above. In my full document, there is even less distance between the first caption and the code listing. How can I insert more space (as appropriate) between the first caption and the second figure? Depending on intervening edits, the two figures may or may not end up on the same page.

-

You can modify the length \floatsep, as appropriate, which designates the vertical distance/length between floats at the top/bottom of a page. By "as appropriate" you would typeset your document and only worry about this (manual) fine-tuning at the end.

The following minimal example shows two pages that are duplicate, except for a modification in \floatsep to show the effect of modifying the float separation.

\documentclass{article}
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\begin{document}
\lipsum[1]
\begin{figure}[t]
\centering \includegraphics{figure1}
\caption{This is a figure}
\end{figure}
\begin{figure}[t]
\centering \includegraphics{figure2}
\caption{This is another figure}
\end{figure}
\lipsum[2]

\clearpage

\setlength{\floatsep}{1pt}
\lipsum[1]
\begin{figure}[t]
\centering \includegraphics{figure1}
\caption{This is a figure}
\end{figure}
\begin{figure}[t]
\centering \includegraphics{figure2}
\caption{This is another figure}
\end{figure}
\lipsum[2]

\end{document}


From the layouts package documentation it is possible to see the lengths in terms of page/document float elements (p 21 onward):

The default is 12.0pt plus 2.0pt minus 2.0pt which allows for a 2pt stretchability from 12pt.

It is also possible to modify the distance between the figure and the caption using the skip key-value from the caption package. Default is 10pt.

-
Might it be worth mentioning skip from the caption package too? – cmhughes Nov 28 '11 at 3:34
@Werner There is a bug in LaTeX, in that the way LaTeX captions work, LaTeX does not know if the caption is at the top or at the bottom. There is a write up about it the caption package, maybe you want to add a bit about this in your answer and how you should compensate for it. – Yiannis Lazarides Nov 28 '11 at 5:00