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.

  • 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


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.

\lstset{literate={:=}{{$\gets$}}1 {<=}{{$\leq$}}1 {>=}{{$\geq$}}1 {<>}{{$\neq$}}1}
var i:integer;
if (i<=0) i := 1;
if (i>=0) i := 0;
if (i<>0) i := 0;

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.


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}
\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...
  \hbox to\algindent{\bf\hfil #1.\kern.25em}%
% 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 .