我有以下算法的文本,但无法对其进行格式化,使用如图所示的数字和间距:
$COVERABILITY-GRAPH((S,T,F,M_0))$\\
$(V,E,v_0):=(\{M_0\},\emptyset,M_0);$\\
Work:set:=$\{M_0\}$\\
\textbf{while} Work $\neq \emptyset$\\
\textbf{do} select $M$ from Work;\\
Work:=Work$\setminus\{M\};$\\
\textbf{for} $t\in$enabled(M)\\
\textbf{do} $M':=$fire($M,t);$\\
$M':=$AddOmegas($M,t,M',V,E$)\;\
\textbf{if} $M'\not \in V$\\
\textbf{then} $V:=V\cup \{M'\}$\\
Work:=Work$\cup\{M'\};$\\
$E:=E\cup\{(M,t,M')\}$\\
\textbf{return}$(V,E,v_0)$
并且
$ADDOMEGAS(M,t,M',V,E)$\\
\textbf{for}$M''\in V$\\
\textbf{do if} $M''<M'$ and $M''\xrightarrow{*}_{E} M$\\
\textbf{then} $M':=M'+((M'-M'').\omega);$\\
\textbf{return} $M';$
答案1
一种可能性是以下代码,但请注意,我将算法中使用的变量的确切格式留给您。您没有解释代码,因此请检查格式,例如,如果Work
是变量,则最好使用 来格式化它\mathit{Work}
。
我使用了
\usepackage{algorithm}% <=================http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% <========== http://ctan.org/pkg/algorithmicx
使用其他包会导致其他编码,因此请阅读文档(请按照评论中给出的链接)。
使用以下代码
\documentclass{article}
\usepackage{algorithm}% <=================http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% <========== http://ctan.org/pkg/algorithmicx
\begin{document}
\begin{algorithm}
\begin{algorithmic}[1] % numbering starts with 1
\Procedure {COVERABILITY-GRAPH}{$S$,$T$,$F$,$M_0$}% \Comment{my name is Xyz}
\State $(V,E,v_0):=(\{M_0\},\emptyset,M_0)$ % := --> \gets
\State $Work:$ set $:=\{M_0\}$
\While {$Work \neq \emptyset$}
\State select $M$ from $Work$
\State $Work:=Work\setminus\{M\}$
\For {$t\in$ enabled(M)}
\State $M':=$ fire($M,t);$
\State $M':=$ AddOmegas($M,t,M',V,E$)
\If {$M'\not \in V$}
\State $V:=V\cup \{M'\}$
\EndIf
\State $E:=E\cup\{(M,t,M')\}$
\EndFor
\EndWhile
\State \Return $(V,E,v_0)$
\EndProcedure
\Procedure {ADDOMEGAS}{$M,t,M',V,E$}
\For {$M''\in V$}
\If {$M''<M'$ \textbf{and} $M''\rightarrow{*}_{E} M$}
\State $M':=M'+((M'-M'')\omega)$
\EndIf
\EndFor
\State \Return $M'$
\EndProcedure
\caption{COVERABILITY}\label{alg:COVERABILITY}
\end{algorithmic}
\end{algorithm}
\end{document}
您将获得以下结果:
答案2
像这样吗?
\documentclass{article}
\usepackage{mathtools}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{setspace, etoolbox, caption}
\AtBeginEnvironment{algorithmic}{\setstretch{1.25}\let\textbf\textsf\vspace{0.4ex}}
\begin{document}
Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text.Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text.
\begin{algorithm}
\caption{ \textsc{Coverability-Graph} ($ (S,T,F,M₀) $)}
\begin{algorithmic}[1]
\State $(V,E,v₀) ∶= q(\{M₀\},\emptyset,M₀)$;
\State \textit{Work}: set $ ∶= q\{M₀\}$;
\While{$ \mathit{Work}\ne\emptyset $}
\State select $M$ from $\mathit{Work}$;
\State $\mathit{Work} ∶= q \mathit{Work}∖\{M\}$;
\For{$t ∈ \mathsf{enabled}(M)$}
\State $M' ∶= q \mathsf{fire}(M, t)$;
\State $M' ∶= q \mathsf{AddOmegas}(M, t, M',V, E)$;
\If{$M' ∉ V$}
\State $V ∶= q V ∪ \{M'\}$;
\State $\mathit{Work} ∶= q\mathit{Work} ∪ \{M'\}$;
\EndIf
\State $E ∶= q E ∪ \{(M, t, M')\}$;
\EndFor
\EndWhile
\State
\Return $(V, E, v₀)$;
\end{algorithmic}
\end{algorithm}
\end{document}