[Tex/LaTex] Styling a lstlisting caption using caption package

captionslistings

I'd like to style a caption in a specific way, but I can't seem to figure out the proper settings to do so.

First off, I'd like to change the label from "Listing" to "Algorithm" when I use \captionof{lstlisting}{foo}

Secondly, I'd like to make the "Algorithm 1" part be bold with the rest of the label non boldface

Thirdly, I'd like to have a line above the caption.

Here's an image of something akin to what I'd like to achieve, and following is my code.

enter image description here

And this is what I have:

enter image description here

\documentclass{article}

\usepackage{listings}
\usepackage{caption}
\usepackage{geometry}
\usepackage{DejaVuSans}
\usepackage{DejaVuSansMono}
\renewcommand*\familydefault{\ttdefault}

\geometry{left=1.0in,right=1.0in,top=1.0in,bottom=1.0in }

\lstset{
  frame=top,frame=bottom,
  basicstyle=\small\normalfont\sffamily,    % the size of the fonts that are used for the code
  stepnumber=1,                           % the step between two line-numbers. If it is 1 each line will be numbered
  numbersep=10pt,                         % how far the line-numbers are from the code
  tabsize=2,                              % tab size in blank spaces
  extendedchars=true,                     %
  breaklines=true,                        % sets automatic line breaking
  captionpos=t,                           % sets the caption-position to top
  mathescape=true,
  stringstyle=\color{white}\ttfamily, % Farbe der String
  showspaces=false,           % Leerzeichen anzeigen ?
  showtabs=false,             % Tabs anzeigen ?
  xleftmargin=17pt,
  framexleftmargin=17pt,
  framexrightmargin=17pt,
  framexbottommargin=5pt,
  framextopmargin=5pt,
  showstringspaces=false      % Leerzeichen in Strings anzeigen ?
 }

\DeclareCaptionFormat{listing}{#1#2#3}
\captionsetup[lstlisting]{format=listing,singlelinecheck=false, margin=0pt, font={sf}}

\DeclareMathVersion{sans}
\SetSymbolFont{operators}{sans}{OT1}{cmbr}{m}{n}
\SetSymbolFont{letters}  {sans}{OML}{cmbrm}{m}{it}
\SetSymbolFont{symbols}  {sans}{OMS}{cmbrs}{m}{n}

\lstnewenvironment{sflisting}[1][]
  {\lstset{#1}\mathversion{sans}}{}


\begin{document}

\begin{sflisting}
Initially $\langle M\rangle$ is in transit from $p_{r}$ to all its children in the spanning tree.

Code for $p_{r}$:
1:  upon receiving no message:                                          // first computation event by $p_{r}$
2:      terminate

Code for $p_{i},\ 0\leq i\leq n-1,\ i\neq r$:
3:  upon receiving $\langle M\rangle$ from parent:
4:      send $\langle M\rangle$ to all children
5:      terminate
\end{sflisting}
\end{document}

Best Answer

You can produce the top rule with

\DeclareCaptionFormat{listing}{\rule{\dimexpr\textwidth+17pt\relax}{0.4pt}\vskip1pt#1#2#3}

The additional 17pt come from the settings you used for xleftmargin.

For the other parts of the formatting, you can add

labelsep=space,labelfont=bf

to your already declared options for \captionsetup.

The change of the name from "Listing" to "Algorithm" can be obtained with a redefinition of \lstlistingname:

\renewcommand\lstlistingname{Algorithm}

A complete example:

\documentclass{article}
\usepackage{listings}
\usepackage{caption}
\usepackage{geometry}
\usepackage{DejaVuSans}
\usepackage{DejaVuSansMono}
\renewcommand*\familydefault{\ttdefault}

\geometry{left=1.0in,right=1.0in,top=1.0in,bottom=1.0in }

\lstset{
  frame=top,frame=bottom,
  basicstyle=\small\normalfont\sffamily,    % the size of the fonts that are used for the code
  stepnumber=1,                           % the step between two line-numbers. If it is 1 each line will be numbered
  numbersep=10pt,                         % how far the line-numbers are from the code
  tabsize=2,                              % tab size in blank spaces
  extendedchars=true,                     %
  breaklines=true,                        % sets automatic line breaking
  captionpos=t,                           % sets the caption-position to top
  mathescape=true,
  stringstyle=\color{white}\ttfamily, % Farbe der String
  showspaces=false,           % Leerzeichen anzeigen ?
  showtabs=false,             % Tabs anzeigen ?
  xleftmargin=17pt,
  framexleftmargin=17pt,
  framexrightmargin=17pt,
  framexbottommargin=5pt,
  framextopmargin=5pt,
  showstringspaces=false      % Leerzeichen in Strings anzeigen ?
 }

\DeclareCaptionFormat{listing}{\rule{\dimexpr\textwidth+17pt\relax}{0.4pt}\par\vskip1pt#1#2#3}
\captionsetup[lstlisting]{format=listing,singlelinecheck=false, margin=0pt, font={sf},labelsep=space,labelfont=bf}

\renewcommand\lstlistingname{Algorithm}

\DeclareMathVersion{sans}
\SetSymbolFont{operators}{sans}{OT1}{cmbr}{m}{n}
\SetSymbolFont{letters}  {sans}{OML}{cmbrm}{m}{it}
\SetSymbolFont{symbols}  {sans}{OMS}{cmbrs}{m}{n}

\lstnewenvironment{sflisting}[1][]
  {\lstset{#1}\mathversion{sans}}{}


\begin{document}

\begin{sflisting}[caption=Spanning tree broadcast algorithm.]
Initially $\langle M\rangle$ is in transit from $p_{r}$ to all its children in the spanning tree.

Code for $p_{r}$:
1:  upon receiving no message:                                          // first computation event by $p_{r}$
2:      terminate

Code for $p_{i},\ 0\leq i\leq n-1,\ i\neq r$:
3:  upon receiving $\langle M\rangle$ from parent:
4:      send $\langle M\rangle$ to all children
5:      terminate
\end{sflisting}

\end{document}

enter image description here