TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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
@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
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.

\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.

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

Your Answer


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.