# Help with algorithm2e formatting

recentely I've got into a bit more advanced LaTeX formatting, i.e algorithms. My package of choice is the algorithm2e package, but I'm having some difficulities operating it.

So I've wanted to create an algorithm using the algorithm2e package, but I can't quite wrap my head around how it operates.

Here is my code.

As for packages, all I've used is

\usepackage{amsmath}
\usepackage[linesnumbered]{algorithm2e}


amsmath for mathematical library and re-set the algorithm2e to enumerate the lines of my algorithm

\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}


Which produces this:

My desired result would be:

So I've got 5 questions.

1) How can I redefine the algorithm2e to print "Algoritmus" instead of Algorithm in place of my caption? *Answered in comments
2) Probably will have similar solution as 1), but is it possible for my algorithm to print end for instead of only end?
3) How to place the caption title on the top instead of the bottom of the algorhitm?
4) How to add the <hr>look-a-like line break in the specific parts of algorithm?
5) Is it good practice inside the \For loop to define it as \For{$k = 1$ to $M$}{ code.. }? Seems a bit artificial to me to add the word to there. Isn't there an inbuilt function for this?

EDIT: Found a solution to question number 3 and 4. Studied the algorithm2e.sty file and found solution.

For those wondering, change the package definition to \usepackage[linesnumbered, ruled, vlined]

Produces the following:

The remaining questions however still stand.

• @Naphaneal if i'm not mistaken, that doesn't work with algorithm2e package, but only different ones. At least I did at the very least get an unknown command error. Although I found, that if I change the language options in usepackage, it will change the name of the Algorhitm to Algoritmus automatically. (in this particular case, it was changing it to Slovak). – Rawrplus Apr 2 '16 at 20:25
• my bad. I overlooked the missing 2e part. I was actually looking for this answer: tex.stackexchange.com/questions/146050/… – naphaneal Apr 2 '16 at 20:29
• yup as i said, defining the language in the algorithm2e usepackage does the trick :) Although by any chance I presume you don't know how to apply namechange to also the ending of for loop, or? – Rawrplus Apr 2 '16 at 20:40

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}


Well sadly, nobody really answered and by time it took everybody to do so, I managed to find answers to majority of my questions already.

So to go over them 1 by 1.

To set the algorithm name (caption) to be displayed on top, to add lines inbetween blocks and to change name of Algorithm to Algoritmus, you have to edit the settings in your \usepackage declaration.

So we edited the declaration to following: \usepackage[linesnumbered, ruled, vlined, slovak]{algorithm2e}

Obviously, you will set the language option to your own personal preference. On top of that, you can edit the original algorithm2e.sty file to change the localisation definitions/declarations to your personal liking, if you don't like the default options.

As for changing the definition of end to print end for instead, this was a tricky one and I've spent hours searching for this, but inside the documentation I found the \SetKw command.

In this particular case, we changed it to \SetKwFor{For}{for}{do}{end~for}

I can't answer my last question, whether it's good practice to manually add to to the for condition, somebody will have to answer that for me.

Here is the final code

    \SetAlgoNoLine
\begin{algorithm}[H]
\DontPrintSemicolon
\SetKwFor{For}{for}{do}{end~for}
\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}
\label{alg:alg1}
\end{algorithm}


Package definition is as mentioned \usepackage[linesnumbered, ruled, vlined, slovak]{algorithm2e} Will produce the following