latex 算法伪代码

时间:2021-08-08
本文章向大家介绍latex 算法伪代码,主要包括latex 算法伪代码使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

转载自:Latex-算法伪代码

宏包

\usepackage{algorithm}
\usepackage{algorithmic}

示例1:基本用法

官方用例如下:

\begin{algorithm} 
	\caption{Calculate $y = x^n$} 
	\label{alg3} 
	\begin{algorithmic}
		\REQUIRE $n \geq 0 \vee x \neq 0$ 
		\ENSURE $y = x^n$ 
		\STATE $y \gets 1$ 
		\IF{$n < 0$} 
		\STATE $X \gets 1 / x$ 
		\STATE $N \gets -n$ 
		\ELSE 
		\STATE $X \gets x$ 
		\STATE $N \gets n$ 
		\ENDIF 
		\WHILE{$N \neq 0$} 
		\IF{$N$ is even} 
		\STATE $X \gets X \times X$ 
		\STATE $N \gets N / 2$ 
		\ELSE[$N$ is odd] \STATE $y \gets y \times X$ 
		\STATE $N \gets N - 1$ 
		\ENDIF 
		\ENDWHILE 
	\end{algorithmic} 
\end{algorithm}

示例2:

包含了加comment + procedure的用法

\begin{algorithm}
\caption{Put your caption here}
\begin{algorithmic}[1]

\Procedure{Roy}{$a,b$}       \Comment{This is a test}
    \State System Initialization
    \State Read the value 
    \If{$condition = True$}
        \State Do this
        \If{$Condition \geq 1$}
        \State Do that
        \ElsIf{$Condition \neq 5$}
        \State Do another
        \State Do that as well
        \Else
        \State Do otherwise
        \EndIf
    \EndIf

    \While{$something \not= 0$}  \Comment{put some comments here}
        \State $var1 \leftarrow var2$  \Comment{another comment}
        \State $var3 \leftarrow var4$
    \EndWhile  \label{roy's loop}
\EndProcedure

\end{algorithmic}
\end{algorithm}

一些额外的指令

  • \Statex %算法中的空行
  • \Comment{xxx} %注释
  • \Function{xxx}{xxx} ....... \EndFunction 函数
  • \If{xxx} ....... \EndIf
  • \Repeat .... \Until{xxx}

原文地址:https://www.cnblogs.com/mercurysun/p/15115395.html