8

I am using the

\usepackage{algpseudocode}
\usepackage{algorithm}

packages and creating a sample algorithm using the \COMMENT, \IF, \ENDIF and \FOR constructs. When I compile this and view the PDF file, I don't see any of the key words, the indenting is not correct and I have to explicitly mention the key words. Is there anything that I am missing here?

I am using TeXShop on OS X.

EDIT :

This is what I am using

       \begin{algorithm}
       \caption{Algorithm for finding server indices using OFG}

        \begin{algorithmic}
        \newline
        \COMMENT { \%comment: servers[] contains the index of servers whose         data rate are sorted in descending order\%}\newline
        \STATE servers[]= index(of all servers) \newline
        \STATE serverIndex[]=servers[0..K]\newline
        \STATE linearlyIndependentServerIndex[]=0\newline
        \STATE $[Z] \leftarrow 0$\newline
        \FOR  {$i=0$ to $serverIndex.length$} \newline
        \COMMENT{ \%comment: find the equation corresponding the serverIndex        from the mapping at the File Server\%} \newline
        \STATE        $eqn= equation(serverIndex[i])$ \newline
        \COMMENT{ \%comment: try insert equation into Z using OFG\%} \newline

         \ENDFOR end for\newline \newline
         \WHILE{ ( linearlyIndependentServerIndex.length!=K ) } \newline
          \COMMENT{\%comment: remove all the server index which were not inserted in Z\%} \newline 
         \STATE temp[]=serverIndex[]-linearlyIndependentServerIndex \newline
           \IF{  (linearlyIndependentServerIndex.length=K) }\newline
           \STATE break\newline
           \ENDIF  \newline
         \ENDWHILE  \newline
        \end{algorithmic}
        \end{algorithm}

I had to include newline so that the code is readable.

The resulting output after compiling:

9

Please look at the documentation of the algorithmicx package. You used the wrong names for the keywords.

The \newline commands can be removed once the correct keywords are used.

As your comments seem to be meant to be on separate lines, I'd prefix them with \Statex keywords.

Here the corrected MWE:

\documentclass{article}

\usepackage{algpseudocode}
\usepackage{algorithm}

\begin{document}
\begin{algorithm}
  \caption{Algorithm for finding server indices using OFG}

  \begin{algorithmic}
    \Statex \Comment { \%comment: servers[] contains the index of servers whose         data rate are sorted in descending order\%}
    \State servers[]= index(of all servers) 
    \State serverIndex[]=servers[0..K]
    \State linearlyIndependentServerIndex[]=0
    \State $[Z] \leftarrow 0$
    \For  {$i=0$ to $serverIndex.length$} 
    \Statex\Comment{ \%comment: find the equation corresponding the serverIndex        from the mapping at the File Server\%} 
    \State        $eqn= equation(serverIndex[i])$ 
    \Statex\Comment{ \%comment: try insert equation into Z using OFG\%} 

    \EndFor end for 
    \While{ ( linearlyIndependentServerIndex.length!=K ) } 
    \Statex\Comment{\%comment: remove all the server index which were not inserted in Z\%}  
    \State temp[]=serverIndex[]-linearlyIndependentServerIndex 
    \If{  (linearlyIndependentServerIndex.length=K) }
    \State break
    \EndIf  
    \EndWhile  
  \end{algorithmic}
\end{algorithm}


\end{document}

example

  • Thanks looks like i referred to a wrong doc which did not explain this properly. – bhavs Apr 10 '12 at 6:41

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.