Pseudocode.js - Beautiful pseudocode for the Web

Pseudocode.js is a JavaScript library that typesets pseudocode beautifully to HTML.

It supports all modern browsers, including Chrome, Safari, Firefox, Edge, and IE 9 - IE 11.


Visit the project website for demo.


Quick Start

Download pseudocode.js, and host the files on your server. And then include the js and css files in your HTML files:

<link rel="stylesheet" href="//path/to/pseudocode/pseudocode.min.css">
<script src="//path/to/pseudocode/pseudocode.min.js"></script>

Pseudocode.js depends on KaTeX to render math formulas and uses KaTeX’s fonts to render texts. So make sure that KaTeX is setup properly.

Assume the pseudocode to be rendered is in a <pre> DOM element:

<pre id="hello-world-code" style="display:hidden;">
\PRINT \texttt{'hello world'}

To render the above code as a HTML element and append to a parent DOM element, call pseudocode.render:

var code = document.getElementById("hello-world-code").textContent;
var parentEl = document.body;
var options = {
    lineNumber: true
pseudocode.render(code, parentEl, options);

To generate a string of rendered HTML, call pseudocode.renderToString:

var code = document.getElementById("hello-world-code").textContent;
var options = {
    lineNumber: true
var htmlStr = pseudocode.renderToString(code, options);


To give you a sense of the grammar for pseudocode, here is an example that illustrates a quicksort algorithm:

% This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition)
\PROCEDURE{Quicksort}{$A, p, r$}
    \IF{$p < r$} 
        \STATE $q = $ \CALL{Partition}{$A, p, r$}
        \STATE \CALL{Quicksort}{$A, p, q - 1$}
        \STATE \CALL{Quicksort}{$A, q + 1, r$}
\PROCEDURE{Partition}{$A, p, r$}
    \STATE $x = A[r]$
    \STATE $i = p - 1$
    \FOR{$j = p$ \TO $r - 1$}
        \IF{$A[j] < x$}
            \STATE $i = i + 1$
            \STATE exchange
            $A[i]$ with $A[j]$
        \STATE exchange $A[i]$ with $A[r]$


There are several packages for typesetting algorithms in LaTeX, among which algorithmic package is the most simple and intuitive, and is chosen by IEEE in its LaTeX template file. The grammar of pseudocode.js is mostly compatible with algorithmic package with a few improvement to make it even more easier to use.

Commands for typesetting algorithms must be enclosed in an algorithmic environment:

# A precondition is optional
\REQUIRE <text>
# A postcondition is optional
\ENSURE <text>
# The body of your code is a <block>
\STATE ...

<block> can include zero or more <statement>, <control>, <comment> and <function>:

# A <statement> can be:
\STATE <text>
\RETURN <text>
\PRINT <text>

# A <control> can be:
# A conditional
# Or a loop: \WHILE, \FOR or \FORALL

# A <function> can by defined by either \FUNCTION or \PROCEDURE
# Both are exactly the same

# A <comment> is:

A <text> (or <condition>) can include the following:

# Normal characters
Hello world
# Escaped characters
\\, \{, \}, \$, \&, \#, \% and \_
# Math formula
$i \gets i + 1$
# Function call
# Keywords
# LaTeX's sizing commands
\tiny, \scriptsize, \footnotesize, \small \normalsize, \large, \Large, \LARGE, 
\huge, \HUGE
# LaTeX's font declarations
\rmfamily, \sffamily, \ttfamily
\upshape, \itshape, \slshape, \scshape
\bfseries, \mdseries, \lfseries
# LaTeX's font commands
\textnormal{<text>}, \textrm{<text>}, \textsf{<text>}, \texttt{<text>}
\textup{<text>}, \textit{<text>}, \textsl{<text>}, \textsc{<text>}
\uppercase{<text>}, \lowercase{<text>}
\textbf, \textmd, \textlf
# And it's possible to group text with braces
normal text {\small the size gets smaller} back to normal again

Note that although pseudocode.js recognizes some LaTeX commands, it is by no means a full-featured LaTeX implementation in JavaScript. It only support a subset of LaTeX commands that are most relevant to typesetting algorithms.

To display the caption of an algorithm, use algorithm environment as a ‘float’ wrapper :

\caption{The caption of your algorithm}
\STATE ...


Function pseudocode.renderToString and pseudocode.renderToString can accept an option as the last argument.

The values of the options, if not reset specifically, are:

    indentSize: '1.2em',
    commentDelimiter: '//'
    lineNumber: false,
    lineNumberPunc: ':',
    noEnd: false,
    captionCount: undefined

Build and Test

Pseudocode.js is written in JavaScript and built with Node.js. So, make sure you have Node.js installed before building pseudocode.js.

To compile the project on Ubuntu Linux, run the following commands in terminal:

cd pseudocode.js/
make setup

Then, open static/test-suite.html in your favourite browser to see whether algorithms are typeset correctly.


Tate Tian (@tatetian) creates pseudocode.js. Any suggestions and bug reports are welcome.


Pseudocode.js is partially inspired by KaTeX and relies on it to render math formulas. Thanks Emily Eisenberg(@xymostech) and other contributers for building such a wonderful project.