TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ..

share|improve this question
1  
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Mar 5 '14 at 9:07
    
Don't overlook (like I did!) the excellent and brand spanking new (2015) package algorithm2e! – drozzy Jan 15 at 1:45
up vote 54 down vote accepted

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} 
share|improve this answer
    
Does it do while as well? – Thomas Ahle Jun 4 '14 at 9:36
    
Yes it does. en.wikibooks.org/wiki/LaTeX/Algorithms – pratnala Nov 1 '15 at 21:24
    
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 9:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.