3

I am trying to imitate an example I found in the LaTeX wikibook to write my own algorithm using the algorithmic package. I can't understand it clearly, though, and I am getting errors all the time.

 \documentclass{article}
 \usepackage{times}
 \usepackage{amsthm}
 \usepackage{algorithm}
 \usepackage{algorithmic}
 \usepackage{alltt}
 \usepackage{mathtools}
 \usepackage{parskip}
 \begin{document}
 \begin{algorithm}
 \caption{Assigning j to a center}
 \begin{algorithmic}  
 INITIALIZE $C = \emptyset$
 \FOR {i=1,...,r}  <------Error
 \IF {$N_i \cap N_{i_0} \neq\emptyset$ for some $i_0 \leq i$
 \STATE assign to $p_{i_0}$ all demand nodes j with $i \in   \widetilde{p_j}$
 \ELSE
 \STATE $C = C \cup \{ i \}$ and assign to $p_i$ all the demand nodes $j$ with  $i \in   \widetilde{p_j}$
 \ENDIF
 \ENDFOR
 \end{algorithmic}
 \end{algorithm}
 \end{document}

I am getting the error: Something is wrong. Perphaps a missing \item on the line with the \FOR command. Can anybody help?

| improve this question | | | | |
  • 1
    You haven't closed the group/argument to \IF. – Werner Mar 20 '14 at 0:10
  • You are right though the error still persists cause it is in the line above I guess. I added the } and I am still getting it – Paramar Mar 20 '14 at 0:12
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – jub0bs Mar 20 '14 at 0:18
4

Two problems: you had a missing closing brace for the \IF and you were using INITIALIZE in a forbidden place (the beginning of the list internally used). Perhaps you could use

\STATE INITIALIZE $C = \emptyset$

or any other of the keywords provided by the package.

\documentclass{report}
\usepackage{algorithmic,algorithm}

\begin{document}

\begin{algorithm}
 \caption{Assigning j to a center}
 \begin{algorithmic}  
 \STATE INITIALIZE $C = \emptyset$
 \FOR {i=1,...,r}
 \IF {$N_i \cap N_{i_0} \neq\emptyset$ for some $i_0 \leq i$}
 \STATE assign to $p_{i_0}$ all demand nodes j with $i \in   \widetilde{p_j}$
 \ELSE
 \STATE $C = C \cup \{ i \}$ and assign to $p_i$ all the demand nodes $j$ with  $i \in   \widetilde{p_j}$
 \ENDIF
 \ENDFOR
 \end{algorithmic}
 \end{algorithm}

\end{document}

enter image description here

| improve this answer | | | | |
  • I did it, simultaneously with your comments. Thanks again – Paramar Mar 20 '14 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.