Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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

share|improve this answer
    
Thanks looks like i referred to a wrong doc which did not explain this properly. –  bhavs Apr 10 '12 at 6:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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