Monday, May 05, 2008

Algorithms in Latex

What do you use if you want to write algorithms in Latex ? I hope you know Latex (wiki), by the way. Before, I used to use algorithm2e because it just happened to be the first algorithm package that I found, and it just worked. However, since I began using a certain document class of a certain conference, using algorithm2e will produce several error messages.

I searched again, and this time, I found several alternatives, like algorithmic package and listing package. Personally, I think they aren't as nice as algorithm2e, so I decided to go back and see if there is anything I can do about the error messages. And, yes, fortunately, I found that there is a quick fix to solve this problem. If you want to read the details, you can look at the page in about-here.org and groups.google.com that I found.

So, basically, before declaring usepackage{algoritm2e}, you have to write several commands below. Note that you don't need them if there are no error messages at all at the first place.


\makeatletter
\newif\if@restonecol
\makeatother
\let\algorithm\relax
\let\endalgorithm\relax
\usepackage{algorithm2e}


And then, writing, for example:


\begin{algorithm}[H]
\SetLine
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}


will generate the following pdf:



For algorithmic package that I used for comparison, I found the following source code from Zasha Weinberg's page (not that I personally know him). Anyway, this is the source code.

\documentclass{article}
\usepackage{algorithmic}
\begin{document}
\title{Example algorithm document}
\author{Zasha Weinberg (zasha@cs)}
\maketitle
Consider the following algorithm for sorting.
The call $SlowSort(A,i,j)$ sorts the items
$A[i]\ldots A[j]$.

{\bf procedure} $SlowSort(A,i,j)$
\begin{algorithmic}[1]
\IF{$i\geq j$}
\STATE Return
\ENDIF
\STATE $m\gets \lfloor (i+j)/2 \rfloor$
\STATE $SlowSort(A,i,m)$
\STATE $SlowSort(A,m+1,j)$
\IF{$A[m]>A[j]$}
\STATE exchange $A[j],A[m]$
\ENDIF
\STATE $SlowSort(A,i,j-1)$
\end{algorithmic}

(This algorithm is due to Broder and Stolfi
[SIGACT News, Fall, 1984]).
\end{document}

And this is the result. Note that compared to algorithm2e, it has line numbers, but no title. By title, I mean something like "Algorithm 1. Explaining something", something which can be referred to in the paper.



Another alternative that I found: listing package, is not exactly for algorithm or pseudocode, but rather for the source code itself (listing). This is the source code that I found from a wiki page of algorithms and pseudocode in Latex

\usepackage{listings} % Include the listings-package
\begin{document}
\lstset{language=Pascal} % Set your language (you can change the language for each code-block optionally)

\begin{lstlisting} % Start your code-block
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}

\end{document}

And this is the result.



I also noticed that there is another alternative called algorithmicx package, but for some reasons, it just didn't work for me because there were some errors, and I haven't got time to look into them yet. And, I guess I also haven't played around a lot with all the above packages, so there may be several options to display something (like the title/caption for example) although it isn't the default behavior for that particular package. Anyway, looking at the default examples, and since I was using algorithm2e before, I vote for it :).

News of the Post:
Yahoo CEO Yang on hot seat after Microsoft's $47.5 billion offer rebuffed

4 comments:

k said...

Nice find. I won't have to go searching the web next time if I need to write an algorithm. I'll just come here. haha

Sindharta Tanuwijaya said...

I am glad it's helpful for you :). Let me know if you've found other better packages for writing algorithm or new tricks ;)

Anonymous said...

Grande Loco! no sabia como ponerle el nombre al procedimiento
Aguante Aldosivi

Sindharta Tanuwijaya said...

Errm, can someone translate that into English ?

Post a Comment