我正在尝试在论文中编写伪代码。以下是我想要的片段和图像。有人能帮我格式化一下吗?
\begin{algorithm}
\caption{Euclid’s algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $stringlen\gets length of string$
\State $i\gets patlen$
top:
\eIf{i > stringlen}{
return false\;
}
{
\State $j\gets patlen$
}
loop:
\eIf{ j == 0}{
return j+1\;
}
{
\eIf{string(i) > pat(j)}{
\State $j\gets $j -1$
\State $i\gets $i -1$
\State goto loop
\State Close.
}
{
\State $j\gets patlen$
}
}
\EndWhile\label{euclidendwhile}
\EndProcedure
\end{algorithmic}
\end{algorithm}
它应该看起来像这样:
目前看起来乱糟糟的。任何帮助都是值得赞赏的。
答案1
可以这样做algorithmicx
:
代码:
\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\begin{document}
\begin{algorithm}
\caption{My algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{MyProcedure}{}
\State $\textit{stringlen} \gets \text{length of }\textit{string}$
\State $i \gets \textit{patlen}$
\BState \emph{top}:
\If {$i > \textit{stringlen}$} \Return false
\EndIf
\State $j \gets \textit{patlen}$
\BState \emph{loop}:
\If {$\textit{string}(i) = \textit{path}(j)$}
\State $j \gets j-1$.
\State $i \gets i-1$.
\State \textbf{goto} \emph{loop}.
\State \textbf{close};
\EndIf
\State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.
\State \textbf{goto} \emph{top}.
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}