[Tex/LaTex] Adjust spacing of algorithm float caption

algorithmscaptionsspacing

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:

enter image description here

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

Related

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!

Best Answer

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.