138

I am trying to write pseudo code in my paper. Here is the snippet and image like what I want. Can some one please help me to format it.

\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}

it should look like this:

Image

Currently it looks messed up. Any help is appreciable ..

cyclic
  • 1,557
  • 2
  • 11
  • 8

1 Answers1

183

This is what can be done with algorithmicx:

enter image description here

Code:

\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} 
karlkoeller
  • 124,410
  • 1
    Does it do while as well? – Thomas Ahle Jun 04 '14 at 09:36
  • 3
    Yes it does. https://en.wikibooks.org/wiki/LaTeX/Algorithms – pratnala Nov 01 '15 at 21:24
  • 7
    It's having problems with seeing \def\BState{\State\hskip-\ALG@thistlm} - how can I fix this? - Undefinied control sequence \BState ->\State \hskip -\ALG ... – SwimBikeRun Nov 25 '15 at 09:08
  • 2
    I would rather recommend algorithm2e, since that is currently maintained: https://ctan.org/pkg/algorithm2e – lindhe Nov 25 '17 at 09:29
  • 2
    @lindhe notice first that this answer is more than 3 years old. Also, the fact that the latest version of algorithm2e is newer than algorithmicx doesn't mean that the latter is obsolete! – karlkoeller Nov 25 '17 at 18:36
  • 4
    Funny, you start with "what can be done with algorithmicx", but then in you code you use packages algorithm and algpseudocode... – JHBonarius Mar 07 '18 at 10:06
  • 3
    @JHBonarius algpseudocode is part of the algorithmicx bundle. – karlkoeller Mar 11 '18 at 08:19
  • 8
    Ah, but there's also a package called algorithmicx.. Why shoudn't I just use that one? – JHBonarius Mar 11 '18 at 14:44
  • How to exit the \BState and start the next line without the indent? e.g. at what if I have more code, outside of the loop? Must I define another "BState" type but modified? – pookie Feb 10 '20 at 23:56