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}
Supposing your document is in German , loading babel
with german
set via document class does the trick. To replace end
with endfor
, overwrite the definition of For
with \SetKwFor
:
\documentclass[german] {article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel} \usepackage[linesnumbered, ruled, vlined]{algorithm2e}
\SetKwFor{For}{for}{do}{endfor for}%
\begin{document}
\SetAlgoNoLine
\begin{algorithm}[H]
\DontPrintSemicolon
\KwIn{$X_{t-1}, u_t ยท z_t$}
\KwOut{$X_t$}
$\overline{X_t} = X_t = 0$\;
\For{$k = 1$ to $M$}{
$x^{[k]}_t = sample\_motion\_model(u_t, x^{[k]}_{t-1})$\;
$w^{[k]}_t = measurement\_model(z_t, x^{[k]}_t, m_{t-1})$\;
$m^{[k]}_t = updated\_occupancy\_grid(z_t, x^{[k]}_t, m^{[k]}_{t-1})$\;
$\overline{X_t} = \overline{X_t} + \langle x_x^{[m]}, w_t^{[m]} \rangle $\;
}
\For {$k = 1$ to $M$}{
draw $ i $ with probability $\approx w_t^{[i]}$\;
add $\langle x_x^{[m]}, w_t^{[m]} \rangle$ to $X_t$\;
}
\Return $X_t$
\caption{FastSLAM}
\end{algorithm}
\end{document}
Best Answer
You can use
\SetAlgorithmName{Listing}
inside thealgorithm
environment. Unfortunately, \autoref is going to translate always to algorithm #.