[Tex/LaTex] Writing algorithms in pseudocode in latex

algorithms

Why if I write :

\State \textbf{upon event $< Init >$  do}
        \For{\textbf{all $n$} $\in N$}
            \State $value[r]=0$;
        \EndFor

I have this output:

enter image description here
but I want:

enter image description here
How can I do it?Thankyou very much

Best Answer

You can define a new block structure \Event...\EndEvent to suit your needs:

enter image description here

\documentclass{article}
\usepackage{amsmath,algpseudocode}% http://ctan.org/pkg/{amsmath,algorithmicx}
\algdef{SE}[EVENT]{Event}{EndEvent}[1]{\textbf{upon event}\ #1\ \algorithmicdo}{\algorithmicend\ \textbf{event}}%
\algtext*{EndEvent}
\begin{document}
\begin{algorithmic}[0]
  \Event{$< Init >$}
    \For{\textbf{all} $n \in N$}
      \State $\text{value}[r] = 0$;
    \EndFor
  \EndEvent
  \State Do something else;
\end{algorithmic}
\end{document}