Adjust spacing of algorithm float caption

Background

Using the algorithm package to display source code listings.

Problem

The LaTeX (LyX) code (test.lyx) is at: http://pastebin.com/QRWDhA7E

An example graphics (query.png) file is at: http://i.stack.imgur.com/8RRL5.png

The result:

Notice the gap between the very first black line and the start of the blue background.

Questions

1. How do you make the caption flush with the top line?
2. What is the proper way to make the caption flush with its bottom line? (I think I cheated by using \vspace{-1ex}.)

Thank you!

-
Could you please give us a complete example code which we can compile & improve? – Axel Sommerfeldt Feb 17 '11 at 6:38

First of all many of us (if not even most of us) don't have LyX so better offer a LaTeX code instead. Second, better give a minimal example code, because usually reducing the code to it's minimum is the very first step when analyzing a LaTeX problem.

An reasonable example code showing the problem would be something like this:

\documentclass{scrbook}
\usepackage{float}
\usepackage{caption}
\usepackage[dvipsnames,svgnames,x11names,table]{xcolor}

\usepackage{algorithm}

% Use a hyphen for captions, and make links give a bit of space.
\DeclareCaptionFormat{algorithm}{\vspace{-1ex}\colorbox[HTML]{A6BFF2}{%
\parbox[c][1.75em][c]{\textwidth}{\hspace{0.25em}#1#2#3}}}
\captionsetup[algorithm]{format=algorithm}

\begin{document}
\begin{algorithm}
\caption{Blah\ldots}
A
\end{algorithm}
\end{document}


But now let's take a look at your problem: The extra spaces are typeset by the "ruled" float style offered by the float package. One can change this by defining an own float style and making the algorithm environment using this one instead of the original one. I did that by copying the "ruled" style and modifying it:

% Define own float style called "algorithm"
\makeatletter
\newcommand\fs@algorithm{%
\let\@fs@capt\floatc@algorithm
\def\@fs@pre{\hrule height.8pt depth0pt\relax}% \kern2pt removed
\def\@fs@mid{\hrule\kern2pt}%  \kern2pt removed
\def\@fs@post{\kern2pt\hrule\relax}%
\let\@fs@iftopcapt\iftrue}
\makeatother

% Make the algorithm environment use the algorithm float style
\floatstyle{algorithm}
\restylefloat{algorithm}


So in total we have: (Please note that I have removed the extra \vspace of yours.)

\documentclass{scrbook}
\usepackage{float}
\usepackage{caption}
\usepackage[dvipsnames,svgnames,x11names,table]{xcolor}

\usepackage{algorithm}

% Define own float style called "algorithm"
\makeatletter
\newcommand\fs@algorithm{%
\let\@fs@capt\floatc@algorithm
\def\@fs@pre{\hrule height.8pt depth0pt\relax}% \kern2pt removed
\def\@fs@mid{\hrule\kern2pt}%  \kern2pt removed
\def\@fs@post{\kern2pt\hrule\relax}%
\let\@fs@iftopcapt\iftrue}
\makeatother

% Make the algorithm environment use the algorithm float style
\floatstyle{algorithm}
\restylefloat{algorithm}

% Use a hyphen for captions, and make links give a bit of space.
\DeclareCaptionFormat{algorithm}{\colorbox[HTML]{A6BFF2}{%
\parbox[c][1.75em][c]{\textwidth}{\hspace{0.25em}#1#2#3}}}
\captionsetup[algorithm]{format=algorithm}

\begin{document}
\begin{algorithm}
\caption{Blah\ldots}
A
\end{algorithm}
\end{document}


Please note that this code still contains 2x \kern2pt, one before and one after the algorithm body. If you don't like this extra vertical space, too, just remove them.

-
@lockstep: Thanks for editing. It seems that using four spaces is more bullet proof than using the <pre> and <code> tags!? – Axel Sommerfeldt Feb 17 '11 at 19:38
Yes, it seems. I only succeeded by chance. :-) – lockstep Feb 17 '11 at 19:39
Thank you. I was digging into the source for caption.sty, I did not think to look at float.sty. – Dave Jarvis Feb 17 '11 at 20:06
The problem was that your <pre><code> was on the same line as "would be something like this:". – Hendrik Vogt Feb 18 '11 at 7:11