# Algorithm environment problem

I am doing my master thesis and need to include an algorithm into my LaTeX code. I have been trying for hours now, but I still have several errors that always show up. It is my first time using the algorithm package, so help would be appreciated. Here is the code:

\documentclass{article}
\usepackage{algorithm,algorithmic,algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Resource Allocation Algorithm }
\label{alg:algorithm_sum}
\begin{algorithmic}[1]
\State{Initialization of maximum number of iteration $I_{\text{max}}$, given $\mu^m$ and $\beta^m$ and obtain the intermediate solution for (opt. variables)}
\If{ref. is satisfied}
\State{Convergence =  true}
\Return{opt. variables}
\Else
\State{update $\mu$ and $\beta$ according to ref. and $m=m+1$}
\State{Convergence =  false}
\EndIf
\Until{Convergence = true or $m=I_{\text{max}}$}
\end{algorithmic}
\end{algorithm}
\end{document}

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Jul 21, 2015 at 8:59

First of all you are loading both algorithmic and algpseudocode (variant of algorithmicx) which are not compatible.

The syntax you are using is the one of algpseudocode so don't load algorithmic. Moreover, you are using \text and forgot to load amsmath.

Finally, the \Until command has to be used to close a \Repeat statement which is missing in your code.

This is your modified MWE with \Repeat (move it to the right place in the algorithm):

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Resource Allocation Algorithm }
\label{alg:algorithm_sum}
\begin{algorithmic}[1]
\State{Initialization of maximum number of iteration $I_{\text{max}}$, given $\mu^m$ and $\beta^m$ and obtain the intermediate solution for (opt. variables)}
\Repeat
\If{ref. is satisfied}
\State{Convergence =  true}
\Return{opt. variables}
\Else
\State{update $\mu$ and $\beta$ according to ref. and $m=m+1$}
\State{Convergence =  false}
\EndIf
\Until{Convergence = true or $m=I_{\text{max}}$}
\end{algorithmic}
\end{algorithm}
\end{document}


Output:

• Thanks a lot for the feedback, I will be more careful, it is a strange new environment for me :) And sometimes the errors are weird. This solved my problem! Jul 21, 2015 at 9:37
• @redpixel2511 You're welcome. Remember that you can accept one of the answers (the one which helped you most) to mark your question as solved. See How do you accept an answer? Jul 21, 2015 at 9:40

Another possibility is to leave algorithmic and remove algpseudocode to eliminate incompatibility. But change the syntax a bit as this:

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

\begin{algorithm}
\caption{Resource Allocation Algorithm }
\label{alg:algorithm_sum}
\begin{algorithmic}[1]
\STATE{Initialization of maximum number of iteration $I_{\text{max}}$, given $\mu^m$ and $\beta^m$ and obtain the intermediate solution for (opt. variables)}
\REPEAT
\IF{ref. is satisfied}
\STATE{Convergence =  true}
\RETURN{opt. variables}
\ELSE
\STATE{update $\mu$ and $\beta$ according to ref. and $m=m+1$}
\STATE{Convergence =  false}
\ENDIF
\UNTIL{Convergence = true or $m=I_{\text{max}}$}
\end{algorithmic}
\end{algorithm}

\end{document}


Note also that you may revise your algorithm, because until usually needs a repeat to close. You may change it as you wish.

• until without a repeat doesn't look like pseodo-code... Jul 21, 2015 at 9:19
• Absolutely right! Jul 21, 2015 at 9:19
• Yes, thank you, I think I just forgot that line.. Jul 21, 2015 at 9:36