我正在使用
\usepackage{algpseudocode}
\usepackage{algorithm}
\COMMENT
包并使用、\IF
和构造创建示例算法。当我编译此\ENDIF
文件\FOR
并查看 PDF 文件时,我没有看到任何关键词,缩进不正确,我必须明确提及关键词。我这里遗漏了什么吗?
我在 OS X 上使用 TeXShop。
编辑 :
这就是我正在使用的
\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}
我必须包含换行符以便代码可读。
编译后的结果输出:
答案1
请查看算法包。您使用了错误的关键字名称。
\newline
一旦使用正确的关键字,就可以删除命令。
由于您的评论似乎应该放在不同的行上,因此我会在其前面加上\Statex
关键字。
以下是修正后的 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}