As @egreg wrote in the comment it seems that listings
changed the internal definition of \par
without reseting. However based on great comments of @DavidCarliste and @egreg in the chat I fixed the problem.
Please try the new version on github.
With the new version I get 0 errors and the following result:
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}
Best Answer
You can require a new parameter to your environment which contains the caption, and use
\captionof
from thecaption
package:Code: