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}
I now am using the following workaround (i call it that way because it causes circumstances).
- ever algorithm has its own tex file but without any preamble and without includes etc.
- this way I cannot compile these files directly but need another tex using \input and all neccessary includes to compile this file
- this is cumbersome because I always need to change the tex for compiling when just changed a little on the pseudo-code
this way my code-tex is looking like this:
\begin{algorithm}
...
\end{algorithm
these "wrapper-tex" is looking like this:
\documentclass[12pt, letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{import}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage[linesnumbered,ruled]{algorithm2e}
\begin{document}
\import{Algo}{MyAlgo.tex}
\end{document}
For not getting the line pitch of the surrounding document I use \renewcommand before unsing input on the code and after it again to setting it back.
When the code does not fit into the line latex display text, which actually occurs after the input, before the code itself (on the previous page if theres enough space left for text but not for code). I got it by adding a \newpage after the input of the code.
Using include does not show my pseudo code at all.
My include that way looks like:
\begin{flushleft}
some text
...
again some text
\renewcommand{\baselinestretch}{1.0}
\input{Algo/MyAlog.tex}
\renewcommand{\baselinestretch}{1.5}
\newpage
againagainsometext
...
u know
\end{flushleft}
...
I had also problems because in the tex with my texts I defined 12pt fontsize while I didn at first on my code-wrapper.
The pseudocode that way was enlarged after inputting it. Resizing it using recommendations of these
Reduce pseudocode font size (not global)
or othere did not help very much, I surrendered after over an hour trying so I reduced my pseudocode by dividing it into multiple/ separate algorithms and by deleting code commentary.
Best Answer
You could use the command
\lForEach
. See https://www.tug.org/texlive/Contents/live/texmf-dist/doc/latex/algorithm2e/algorithm2e.pdf page 33.