14

Is there any LaTeX package for typesetting algorithms in the style Donald Knuth uses in The Art of Computer Programming?

I know about the algorithm2e package, but I am looking for something that produces output like this:

1. [Check for lower bound.] If X < lower_bound, go to 3.

2. [Check for upper bound.] If X > upper_bound, go to 4.

...

I suppose I can use \newcommand and define my own macro, but I was hoping for something with more intelligence.

2
  • 9
    Just as a matter of personal opinion, Knuth’s style of typesetting algorithms is horrible. He intentionally shirks control structure and only uses jumps, which completely obscures the structure of the algorithm – this is the last thing you want for pseudocode. He also groups several operations in a single statement, thus obscuring the code further. Please don’t use this style. Commented Feb 9, 2012 at 14:00
  • 2
    @Konrad: I am actually just trying to describe a series of steps, not a computer algorithm. For that, I think Knuth's style is appropriate.
    – Ralph
    Commented Feb 10, 2012 at 11:51

2 Answers 2

8

You can use the listings package, which has support for typesetting literate code. You need to set the replacement parameters first using \lstset{literate=}. This will replace for example := with $\gets$ etc.

\documentclass{article}
\usepackage{graphicx}
\usepackage{listings}
\begin{document}
\lstset{literate={:=}{{$\gets$}}1 {<=}{{$\leq$}}1 {>=}{{$\geq$}}1 {<>}{{$\neq$}}1}
\begin{lstlisting}
var i:integer;
if (i<=0) i := 1;
if (i>=0) i := 0;
if (i<>0) i := 0;
\end{lstlisting}
\end{document}

The setting is a bit tedious and the details can be found on page 48 of the listings package (just texdoc listings) from a command prompt interface.

0
13

Actually, Donald Knuth's macros for writing The Art of Computer Programming are available online

These can be modified for LaTeX in a straightforward way, e.g.:

% These macros are borrowed from TAOCPMAC.tex
\newcommand{\slug}{\hbox{\kern1.5pt\vrule width2.5pt height6pt depth1.5pt\kern1.5pt}}
\def\xskip{\hskip 7pt plus 3pt minus 4pt}
\newdimen\algindent
\newif\ifitempar \itempartrue % normally true unless briefly set false
\def\algindentset#1{\setbox0\hbox{{\bf #1.\kern.25em}}\algindent=\wd0\relax}
\def\algbegin #1 #2{\algindentset{#21}\alg #1 #2} % when steps all have 1 digit
\def\aalgbegin #1 #2{\algindentset{#211}\alg #1 #2} % when 10 or more steps
\def\alg#1(#2). {\medbreak % Usage: \algbegin Algorithm A (algname). This...
  \noindent{\bf#1}({\it#2\/}).\xskip\ignorespaces}
\def\algstep#1.{\ifitempar\smallskip\noindent\else\itempartrue
  \hskip-\parindent\fi
  \hbox to\algindent{\bf\hfil #1.\kern.25em}%
  \hangindent=\algindent\hangafter=1\ignorespaces}
% end of borrowed macros

% For Example, with less than 10 steps:
% \algbegin X (Multiplication). blah blah blah blah...
% \algstep X1. [{\it Do stuff\/}] blah blah blah
% \algstep X2. Terminate the algorithm.\quad\slug

You must log in to answer this question.

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