Take the 2-minute tour ×
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.

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.

share|improve this question

migrated from stackoverflow.com Feb 9 '12 at 22:04

This question came from our site for professional and enthusiast programmers.

7  
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. –  Konrad Rudolph Feb 9 '12 at 14:00
1  
@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 Feb 10 '12 at 11:51

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
    
That's what I was looking for. Thanks! –  Ralph Feb 16 '12 at 12:25

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
share|improve this answer

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.