[Tex/LaTex] Writing an algorithm in LaTeX

algorithmicxalgorithmsindentation

Enter image description here

I am trying to write an algorithm in LaTeX. I used a for loop inside the algorithm (see attached image), but the problem is that the statements that are below the for loop are not tapped. Why?

I used the packages \usepackage{algorithm} and \usepackage{algpseudocode}.

This the code I wrote:

\begin{algorithm}
\caption{CH election algorithm}
\label{CHalgorithm}
\begin{algorithmic}[1]
\Procedure{CH\textendash Election} {}
\For{each node $i$ \Pisymbol{psy}{206} $N$ }
\\Broadcast HELLO message to its neighbor
\\let $k$ \Pisymbol{psy}{206} $N1$ ($i$) U {$i$} be s.t
\\QOS($k$) = max {QOS($j$) \textbar $j$ \Pisymbol{psy}{206} $N1$($i$)  U $i$}
\\ MPRSet($i$) = $k$
\EndFor
\EndProcedure
\end{algorithmic}
\end{algorithm}

Best Answer

You have to use \State instead of \\ to indent your lines inside the \For statement.

MWE

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{pifont}

\begin{document}
\begin{algorithm}
\caption{CH election algorithm}
\label{CHalgorithm}
\begin{algorithmic}[1]
\Procedure{CH\textendash Election}{}
\For{each node $i$ \Pisymbol{psy}{206} $N$ }
\State Broadcast HELLO message to its neighbor
\State let $k$ \Pisymbol{psy}{206} $N1$ ($i$) U {$i$} be s.t
\State QOS($k$) = max {QOS($j$) \textbar $j$ \Pisymbol{psy}{206} $N1$($i$)  U $i$}
\State MPRSet($i$) = $k$
\EndFor
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document} 

Result:

enter image description here

Related Question