Way One
Need to include the
\usepackage[noend]{algpseudocode}\usepackage{algorithmicx,algorithm}
Source
%算法的名字%算法的输入, \hspace*{0.02in}用来控制位置,同时利用 \\ 进行换行%算法的结果输出% \State 后写一般语句% For 语句,需要和EndFor对应% If 语句,需要和EndIf对应% While语句,需要和EndWhile对应 \State ...\EndWhile\State \Return result\end{algorithmic}\end{algorithm}
Note :
- The capitalization of the keyword is problematic, otherwise Undefined control sequence will appear.
- The control flow should correspond to the front and back. If there is while but there is no endwhile, the Some blocks is not closed appears.
Way Two
Need to include the
\usepackage[ruled]{algorithm2e}
Source
\begin{algorithm}[H] \caption{algorithm caption}%算法名字 %要求显示行号 \KwIn{input parameters A, B, C}%输入参数 \KwOut{output result}%输出 %\;用于换行 \For{condition}{ only if\; \If{condition}{ 1\; } } \While{not at end of this document}{ if and else\; \eIf{condition}{ 1\; }{ 2\; } } \ForEach{condition}{ \If{condition}{ 1\; } }\end{algorithm}
Mode three
Need to include the
\usepackage[ruled,vlined]{algorithm2e}
Source
\begin{algorithm}[H] \caption{algorithm caption}%算法名字 %要求显示行号 \KwIn{input parameters A, B, C}%输入参数 \KwOut{output result}%输出 %\;用于换行 \For{condition}{ only if\; \If{condition}{ 1\; } } \While{not at end of this document}{ if and else\; \eIf{condition}{ 1\; }{ 2\; } } \ForEach{condition}{ \If{condition}{ 1\; } }\end{algorithm}
For more information on the form, refer to:
- Drawing of various forms of "LaTeX Tips"
Latex algorithm Typesetting notes