# How to align text within an algorithm environment?

I wish to duplicate an effect I've seen elsewhere (image below) in which text not necessarily at the beginning of a line is aligned with text elsewhere.

I don't know which environment is suitable, but I assume one of the various algorithm environments fits the bill (w.r.t. structure). I have tried with the apparently popular algorithm2e package, but could not see a way to do it.

\documentclass[11pt]{article}
\usepackage{algorithm2e}

\begin{document}
\begin{algorithm}
\caption{Algorithm}
Let  \quad $X$ be the set of $n$ training patterns $x_{1}, x_{2}, \dots x_{n}$\;
\qquad $W$ be a $p \times q$ grid of units $w_{ij}$ where $i$ and $j$ are their coordinates on that grid\;
\qquad $\alpha$ be the learning rate, assuming values in $[0,1]$, initialized to a given initial learning rate\;
\qquad $r$ be the radius of the neighborhood function $h(w_{ij},w_{mn},r$), initialized to a given initial radius\;
\Repeat{$\alpha$ reaches 0}{
\For{$k=1$ to $n$}{
For all $w_{ij} \in W$, calculate $d_{ij} = || x_{k} - w_{ij} ||$\;
Select the unit that minimizes $d_{ij}$ as the winner $w_{winner}$\;
Update each unit $w_{ij} \in W: w_{ij} = w_{ij} + \alpha h(w_{winner}, w_{ij}, r) || x_{k} - w_{ij} ||$\;
Decrease the value of $\alpha$ and $r$
}
}
\end{algorithm}
\end{document}


which produces...

This shows two problems: 1) the alignment doesn't exist, and 2) when a line wraps, it doesn't have an indent. How can I do this?

-

Here tabularx package is proposed to achieve the 2 objectives. Three columns are assigned with different property {l>{$}c<{$}X}. Further, .xx\textwidth can be changed via decimal number .xx.

Codes

\documentclass[11pt]{article}
\usepackage{algorithm2e}
\usepackage{tabularx}

\begin{document}
\begin{algorithm}
\caption{Algorithm}
\begin{tabularx}{\textwidth}{l>{$}c<{$}X}
Let &     X  & be the set of $n$ training patterns $x_{1}, x_{2}, \dots x_{n}$\\
& W  &be a $p \times q$ grid of units $w_{ij}$ where $i$ and $j$ are their coordinates on that grid\\
&  \alpha  & be the learning rate, assuming values in $[0,1]$, initialized to a given initial learning rate\\
&  r  & be the radius of the neighborhood function $h(w_{ij},w_{mn},r )$, initialized to a given initial radius
\end{tabularx}
\Repeat{$\alpha$ reaches 0}{
\For{$k=1$ to $n$}{
For all $w_{ij} \in W$, calculate $d_{ij} = || x_{k} - w_{ij} ||$\;
Select the unit that minimizes $d_{ij}$ as the winner $w_{winner}$\;
Update each unit $w_{ij} \in W: w_{ij} = w_{ij} + \alpha h(w_{winner}, w_{ij}, r) || x_{k} - w_{ij} ||$\;
Decrease the value of $\alpha$ and $r$
}
}
\end{algorithm}
\end{document}

-
Oh the shame, I assumed other environments couldn't be used inside algorithm. I had tried the align environment and it didn't like it. – a different ben Nov 8 '13 at 5:14
Ya, I had that experiences too. But it is the X column specifier leads me to use tabularx; X is like a p column, except that the width is calculated automatically. – Jesse Nov 8 '13 at 5:45
Thanks! I now have the problem that the table is included in the numbering of lines (when that option is set in the preamble). Any idea how to manage that? I'm trying some things but they are not working. – a different ben Nov 8 '13 at 5:47
I took a quick review of the manual of algorithm2e (from googling). There are two examples on page 5 and 10 may provide some help. – Jesse Nov 8 '13 at 6:15
Gah! I tried, and gave up. The docs are just too difficult to interpret. – a different ben Nov 10 '13 at 23:24

Thanks to @Jesse I have a means to obtain a pretty nice representation, with line numbering of part of the algorithm. It looks like this:

The tabular is wrapped inside the input keyword (redefined as "Let" using \SetKwInput---see page 34 of the v5.0 manual, section 11). The Input, Output, and Data sections in algorithm2e are not numbered. It does set the word "Let" to be centred vertically beside the tabular, but I can't be spending my entire day trying to fix a tiny little thing like that now can I?

Code:

\LinesNumbered
\SetKwInput{KwLet}{Let}
\begin{algorithm}[htbp]
\KwLet{ \begin{tabularx}{0.8\textwidth}{>{$}c<{$} X}
X  & be the set of $n$ training vectors $x_{1}, x_{2}, \dots x_{n}$\\
W  &be a $p \times q$ grid of units $w_{ij}$ where $i$ and $j$ are their coordinates on that grid\\
\alpha  & be the learning rate, assuming values in $[0,1]$, initialised to a given initial learning rate\\
r  & be the radius of the neighbourhood function $h(w_{ij},w_{mn},r )$, initialised to a given initial radius
\end{tabularx}}
\BlankLine
\Repeat{$\alpha$ reaches 0}{
\For{ k=1  to n }{
For all $w_{ij} \in W$, calculate $d_{ij} = || x_{k} - w_{ij} ||$\;
Select the unit that minimizes $d_{ij}$ as the winner $w_{winner}$\;
Update each unit $w_{ij} \in W: \allowbreak w_{ij} = w_{ij} + \alpha h(w_{winner}, w_{ij}, r) || x_{k} - w_{ij} ||$\;
Decrease the value of $\alpha$ and $r$
}
}
\caption{Algorithm}
\end{algorithm}

-
Have just realised that if I change the width of the tabular using something like 0.6\textwidth then the "Let" keyword is set beside the tabular. However it's centred vertically, so not sure whether I like that or the version posted above. – a different ben Nov 11 '13 at 4:26
Updated the code and figure. – a different ben Nov 11 '13 at 4:45
add a '\newline' command at the begining of wapping. i.e., '\KwLet{\newline ... ' can move the Let up on top, if that is what you wish to have. – Jesse Nov 11 '13 at 5:23