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.

\caption{Euclid’s algorithm}\label{euclid}
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}

\State $stringlen\gets length of string$
\State $i\gets patlen$

 \eIf{i > stringlen}{
   return false\;
   \State $j\gets patlen$

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


it should look like this:


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

  • 7
    Don't overlook (like I did!) the excellent and brand spanking new (2015) package algorithm2e! Commented Jan 15, 2016 at 1:45
  • 6
    en.wikibooks.org/wiki/LaTeX/Algorithms This wiki page provides a nice overview of several packages to achieve such things. Commented Dec 5, 2018 at 9:56

1 Answer 1


This is what can be done with algorithmicx:

enter image description here




\caption{My algorithm}\label{euclid}
\State $\textit{stringlen} \gets \text{length of }\textit{string}$
\State $i \gets \textit{patlen}$
\BState \emph{top}:
\If {$i > \textit{stringlen}$} \Return false
\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};
\State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.
\State \textbf{goto} \emph{top}.
  • 3
    Yes it does. en.wikibooks.org/wiki/LaTeX/Algorithms
    – pratnala
    Commented Nov 1, 2015 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 ... Commented Nov 25, 2015 at 9:08
  • 4
    Funny, you start with "what can be done with algorithmicx", but then in you code you use packages algorithm and algpseudocode...
    – JHBonarius
    Commented Mar 7, 2018 at 10:06
  • 3
    @JHBonarius algpseudocode is part of the algorithmicx bundle. Commented Mar 11, 2018 at 8:19
  • 8
    Ah, but there's also a package called algorithmicx.. Why shoudn't I just use that one?
    – JHBonarius
    Commented Mar 11, 2018 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .