# Formatting - Algorithm

I have following pseudocode: I want to write it as an algorithm but not able to format it properly. Following is my attempt:

\documentclass{article}
\usepackage{algorithm,amsmath,algpseudocode}
\begin{document}

\begin{algorithm}
\caption{Adaptive Thresholding}\label{euclid}

\begin{algorithmic}

\State Convert $R$ to a binary region using the threshold $\theta_{0}$. \label{step:convert}

\State Assume $N$ is the sum of the number of non-zero pixels within $F_{l}$ and $F_{r}$.

\State If $N$ is larger than a predefined threshold $\beta$, then
\State $\theta_{0} = \theta_{0} + \Delta\theta$
\State Repeat the procedure from step \ref{step:convert}
\State Else
\State $\theta = \theta_{0}$
\State End
\State where $\Delta\theta$, $\beta$ are two constants.

\end{algorithmic}
\end{algorithm}

\end{document}


and this is what I get: Any ideas how to format lines 4-8? I have to show the pseudocode in algorithm format.

• If you read the user guide of the algorithm package, you'll see that the If Then Else is properly defined as: \IF{condition 1} \STATE procedure 1 \ELSIF{condition 2} \STATE procedure 2 \ELSIF{condition 3} \STATE procedure 3 \ELSE \STATE procedure 4 \ENDIF – pluton Nov 8 '14 at 19:57
• @pluton could you please provide a working solution? I tried various ways already... – learner Nov 8 '14 at 20:01
• @pluton thanks. just an edit: All letters are not in caps. Instead of \IF \ELSE \ENDIF I tried \If \Else \EndIf and it worked. Wasn't working with caps. – learner Nov 8 '14 at 20:11

## 1 Answer

The main problem in your code was that you did not call if-else as a command. If you fix this, then it solves your problem.

\begin{algorithm}
\caption{Adaptive Thresholding}\label{euclid}

\begin{algorithmic}

\State Convert $R$ to a binary region using the threshold $\theta_{0}$. \label{step:convert}

\State Assume $N$ is the sum of the number of non-zero pixels within $F_{l}$ and $F_{r}$.

\If{$N$ is larger than a predefined threshold $\beta$}
\State $\theta_{0} = \theta_{0} + \Delta\theta$
\State Repeat the procedure from step \ref{step:convert}
\Else
\State $\theta = \theta_{0}$
\EndIf
\end{algorithmic}
\end{algorithm}

Where $\Delta\theta$, $\beta$ are two constants.