[comp.text.tex] Environment for algorithms

halldors@paul.rutgers.edu (Magnus M Halldorsson) (03/16/90)

In article <MPF.90Mar14220157@triplea.cs.umd.edu> mpf@triplea.cs.umd.edu (Martin Farach) writes:

> Does anyone have an environment for formating algorithms in LaTeX.

I have one, thanks to Martin Carroll. 

----- algorithm.sty -----

%The following code creates an environment that works just like the
%figure environment, except that it's for algorithms.  That is, its
%captions look like
%
%		Algorithm 2.1:  An algorithm.
%
%The algorithms are numbered independently of the figures.
%It also produces a .loa file, which can be used to create a list of
%algorithms via the command \listofalgorithms.
% Author: Martin Carroll

\def\listofalgorithms{\@restonecolfalse\if@twocolumn\@restonecoltrue\onecolumn
 \fi\chapter*{List of Algorithms\@mkboth
 {LIST OF ALGORITHMS}{LIST OF ALGORITHMS}}\@starttoc{loa}\if@restonecol\twocolumn
 \fi}
\def\l@algorithm{\@dottedtocline{1}{1.5em}{2.3em}}

\newcounter{algorithm}[chapter]
\def\thealgorithm{\thechapter.\@arabic\c@algorithm}
\def\fps@algorithm{tbp}
\def\ftype@algorithm{4}
\def\ext@algorithm{loa}
\def\fnum@algorithm{Algorithm \thealgorithm}
\def\algorithm{\@float{algorithm}}
\let\endalgorithm\end@float
\@namedef{algorithm*}{\@dblfloat{algorithm}}
\@namedef{endalgorithm*}{\end@dblfloat}

-----------------------------------------

The only problem with it is that
it assumes chapters in the document (i.e. that the document is a
book). As a quick and dirty solution I use another file when doing,
say, articles, changing only these two lines:

\newcounter{algorithm}[chapter]
\def\thealgorithm{\thechapter.\@arabic\c@algorithm}
    to
\newcounter{algorithm}[section]
\def\thealgorithm{\thesection.\@arabic\c@algorithm}


Magnus

emcmanus@cs.tcd.ie (Eamonn McManus) (03/16/90)

mpf@triplea.cs.umd.edu (Martin Farach) writes:
>Does anyone have an environment for formating algorithms in LaTeX?

You could try using my recently-posted macros for formatting C programs.
They are not very dependent on C syntax and format algorithms in a
reasonable way.  Each line in the input corresponds to a line in the
output, and tabs can be used to control indentation.

Your example could be rendered like this:

\begin{figure}\caption{Name of Algorithm}
\begin{cprog}

i = 1

for j = 0 to 100
	i = i + j

\end{cprog}
\end{figure}

All the text is rendered in the normal text font, except that comments in
/*...*/ will be in a slanted font, and strings in "..." or '...' in
typewriter.  Character pairs like <= and != get converted into single
symbols like $\le$ and $\ne$.

The {cprog} environment is a verbatim one, so you can't use TeX commands to
change fonts or whatever within it.  It would be easy to hack cprog.sty so
that \, {, and } still have their usual meanings.  Just remove commands
that say \makeactive\\, \makeactive{, and \makeactive}, and those that say
\def{[...], \def}[...], \expandafter\def\activebackslash[...].  Also remove
\begingroup and \cprogarg from the definitions of \cprog and \cprog*, and
\endgroup from the definition of \endcprog.  I think this would be
sufficient.

If you do this, rename the style file and the environment so there won't
be mutant cprog.sty files around.
--
Eamonn McManus <emcmanus@cs.tcd.ie>	<emcmanus%cs.tcd.ie@cunyvm.cuny.edu>
	  One of the 0% of Americans who are not Americans.