# How to show algorithm steps in algorithm2e?

I am trying to figure it out how to show algorithm steps in algorithm2e.

My code looks like this:

\title{AlgorithmTemplate}
\documentclass[17pt]{article}
\usepackage{fullpage}
\usepackage{times}
\usepackage{fancyhdr,graphicx,amsmath,amssymb}
\usepackage[ruled,vlined]{algorithm2e}
\include{pythonlisting}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwIn{Multi-label dataset : $\left(x^{(n)}, \mathbf{y}^{(n)}\right), n=1,2, \dots, N$ ; \newline A zero matrix $A \in \mathbb{R}{^{n\times n}}$ ; \newline A numpy matrix $H^l \in \mathbb{R}{^{n\times d}}$}

\KwOut{Predicted label set $\mathbf{\widehat y}$}
\emph{Feature vector from bi-directional LSTM}\;

\For{each epoch}{
\emph{Feature vector from bi-directional LSTM}\;
\For{each batch}{\label{forins}
$1)$ Compute \textit{x} using equation (1)\newline
$\mathbf{\textit{x} = f_{rnn}(f_{word2vec}(I;\theta_{word2vec});\theta_{rnn})\in\mathbb{R}^{D}}$\newline

Compute forward pass for lstm \newline
$\overrightarrow{\boldsymbol{h}}_{i}=\overrightarrow{\mathrm{LSTM}}\left(\overrightarrow{\boldsymbol{h}}_{i-1}, \boldsymbol{x}_{i}\right)$\newline
Compute forward pass for lstm \newline
$\overleftarrow{\boldsymbol{h}}_{i}=\overleftarrow{\mathrm{LSTM}}\left(\overleftarrow{\boldsymbol{h}}_{i+1}, \boldsymbol{x}_{i}\right)$ \newline
concatenating the hidden states from both directions \newline
$\boldsymbol{h}_{i}=\left[\overrightarrow{\boldsymbol{h}_{i}} ; \overleftarrow{\boldsymbol{h}}_{i}\right]$\newline
$2)$ Compute \textit{W} using equation (10)\newline
$\mathbf{W = f_{network} (I;J;\theta_{network})}$ \newline
compute mulit-head attention using equation (9)
\mathbf{H_{i}^{(l+1)}=\sigma\left(\frac{1}{K} \sum_{k=1}^{K} \sum_{j \in N(i)} \alpha_{i j, k}^{(l)} H_{j}^{(l)} W^{(l)}\right)}\newline

Matrix multiplication

$\mathbf{\widehat y}$ = \textit{x} \mathbf{\odot} \textit{W} \newline

Compute cross entropy \newline

$\mathbf{\mathcal{L}=\sum_{c=1}^{C} y^{c} \log \left(\sigma\left(\hat{y}^{c}\right)\right)+\left(1-y^{c}\right) \log \left(1-\sigma\left(\hat{y}^{c}\right)\right)}$

loss = reduce ( cross entropy )

update the parameters basis on loss using back propagation \newline

$\mathbf{\theta_{t+1}=\theta_{t}-\frac{\eta}{\sqrt{\hat{v}_{t}}+\epsilon} \hat{m}_{t}}$

}
}

\caption{Algorithm}

\end{algorithm}
\end{document}


How to get numbering like this:

edited algo:

\title{AlgorithmTemplate}
\documentclass[17pt]{article}
\usepackage{fullpage}
\usepackage{times}
\usepackage{fancyhdr,graphicx,amsmath,amssymb}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}
\include{pythonlisting}
\begin{document}
\SetAlgoLined

\begin{algorithm}[H]
\KwIn{Multi-label dataset : $\left(x^{(n)}, \mathbf{y}^{(n)}\right), n=1,2, \dots, N$ ; \newline A zero matrix $A \in \mathbb{R}{^{n\times n}}$ ; \newline A numpy matrix $H^l \in \mathbb{R}{^{n\times d}}$}

\KwOut{Predicted label set $\mathbf{\widehat y}$}
\emph{Feature vector from bi-directional LSTM}\;

\For{each epoch}{
\emph{Feature vector from bi-directional LSTM}\;
\For{each batch}{\label{forins}
$1)$ Compute \textit{x} using equation (1)

$\mathbf{\textit{x} = f_{rnn}(f_{word2vec}(I;\theta_{word2vec});\theta_{rnn})\in\mathbb{R}^{D}}$

Compute forward pass for lstm

$\overrightarrow{\boldsymbol{h}}_{i}=\overrightarrow{\mathrm{LSTM}}\left(\overrightarrow{\boldsymbol{h}}_{i-1}, \boldsymbol{x}_{i}\right)$

Compute forward pass for lstm

$\overleftarrow{\boldsymbol{h}}_{i}=\overleftarrow{\mathrm{LSTM}}\left(\overleftarrow{\boldsymbol{h}}_{i+1}, \boldsymbol{x}_{i}\right)$

concatenating the hidden states from both directions

$\boldsymbol{h}_{i}=\left[\overrightarrow{\boldsymbol{h}_{i}} ; \overleftarrow{\boldsymbol{h}}_{i}\right]$

$2)$ Compute \textit{W} using equation (10)

$\mathbf{W = f_{network} (I;J;\theta_{network})}$

compute mulit-head attention using equation (9)

\mathbf{H_{i}^{(l+1)}=\sigma\left(\frac{1}{K} \sum_{k=1}^{K} \sum_{j \in N(i)} \alpha_{i j, k}^{(l)} H_{j}^{(l)} W^{(l)}\right)}

Matrix multiplication

$\mathbf{\widehat y}$ = \textit{x} \mathbf{\odot} \textit{W}

Compute cross entropy

\begin{aligned}

\mathbf{\mathcal{L}=\sum_{c=1}^{C} y^{c} \log \left(\sigma\left(\hat{y}^{c}\right)\right)+ \\
\left(1-y^{c}\right) \log \left(1-\sigma\left(\hat{y}^{c}\right)\right)}
\end{aligned}

loss = reduce ( cross entropy )

update the parameters basis on loss using back propagation

$\mathbf{\theta_{t+1}=\theta_{t}-\frac{\eta}{\sqrt{\hat{v}_{t}}+\epsilon} \hat{m}_{t}}$

}}

\caption{Algorithm}
\end{algorithm}
\end{document}

• Every latex package has a manual, have you checked texdoc.net/texmf-dist/doc/latex/algorithm2e/algorithm2e.pdf, you most likely also have it on your computer. Commented Jul 11, 2019 at 15:13
• That is the same buggy code you used in your first question. Please correct the math errors first! And please register your account! Commented Jul 12, 2019 at 9:16

You need to add the option linesnumbered when loading the algorithm2e package or call \LinesNumbered after loading it:

\documentclass{article}

\usepackage[ruled,vlined,linesnumbered]{algorithm2e}

\begin{document}

\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}

\end{document}


If you wish to change the style to include a colon : suffix, then you can also add

\SetNlSty{textbf}{}{:}% Add colon after line number
\IncMargin{.2em}% Push algorithm to the right (allowing for larger line numbering)


to your preamble (after loading algorithm2e).

• Thanks for your answer @Werner. I tried your answer and it's working well but numbering is not starting from input, it's ignoring input and output. Second the number and end are not aligned at the end of algorithm, I have updated the algorithm, please check. Commented Jul 11, 2019 at 15:33
• @aaditya_sha: I don't understand your comment about the "number and end not being aligned." Also, please make your document into something that compiles.
– Werner
Commented Jul 11, 2019 at 19:05