Beautiful pseudocode for the Web
npm install pseudocode
pseudocode.js is a JavaScript library that typesets pseudocode beautifully to
HTML.
* _Intuitive grammar:_ Pseudocode.js takes a LaTeX-style input that supports
the algorithmic constructs from LaTeX's algorithm packages. With or without
LaTeX experience, a user should find the grammar fairly intuitive.
* _Print quality:_ The HTML output produced by pseudocode.js is (almost)
identical with the pretty algorithms printed on publications that are
typeset by LaTeX.
* _Math formula support:_ Inserting math formulas in pseudocode.js is as easy
as LaTeX. Just enclose math expression in $...$ or \(...\).
It supports all modern browsers, including Chrome, Safari, Firefox, Edge, and Edge.
Visit the project website for a demo.
pseudocode.js can render math formulas using either
KaTeX, or MathJax.
#### Step 1A · For KaTeX users
Include the following in the of your page:
``html`
integrity="sha512-EKW5YvKU3hpyyOcN6jQnAxO/L8gts+YdYV6Yymtl8pk9YlYFtqJgihORuRoBXK8/cOIlappdU6Ms8KdK6yBCgA=="
crossorigin="anonymous" referrerpolicy="no-referrer">
#### Step 1B · For MathJax 2.x users
Include the following in the
of your page:`html
integrity="sha256-DViIOMYdwlM/axqoGDPeUyf0urLoHMN4QACBKyB58Uw="
crossorigin="anonymous" referrerpolicy="no-referrer">
`#### Step 1C · For MathJax 3.x users
Include the following in the
of your page:`html
integrity="sha256-Cm3tWrvOEzMWWN0jnzQ4Kr0GSSx0txth6MqoES7FX6U="
crossorigin="anonymous" referrerpolicy="no-referrer">
`#### Step 2 · Grab pseudocode.js
Include the following in the
of your page:`html
`#### Step 3 · Write your pseudocode inside a
We assume the pseudocode to be rendered is in aDOM element.`
Here is an example that illustrates a quicksort algorithm:html`
% This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition)
\begin{algorithm}
\caption{Quicksort}
\begin{algorithmic}
\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$}
\ENDIF
\ENDPROCEDURE
\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]$
\ENDIF
\STATE exchange $A[i]$ with $A[r]$
\ENDFOR
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}`#### Step 4A · Render the element using pseudocode.js
Insert the following Javascript snippet at the end of your document:html``#### Step 4B · Render all elements of the class using pseudocode.js
Insert the following Javascript snippet at the end of your document:html`algorithmic$3
There are several packages for typesetting algorithms in LaTeX, among whichalgorithmic
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 withpackage withalgorithmic
a few improvement to make it even more easier to use.Commands for typesetting algorithms must be enclosed in an
environment:`tex`
\begin{algorithmic}A precondition is optional
\REQUIREA postcondition is optional
\ENSUREAn input is optional
\INPUTAn output is optional
\OUTPUTThe body of your code is a
\STATE ...
\end{algorithmic}can include zero or more,,
and:`tex`A
can be:
\STATE
\RETURNA
can be: A conditional
\IF{}
\ELIF{}
\ELSE
\ENDIFOr a loop: \WHILE, \FOR or \FORALL
\WHILE{}
\ENDWHILEOr a repeat: \REPEAT
\UNTIL{ }
\REPEAT
\UNTIL{} A
can by defined by either \FUNCTION or \PROCEDURE Both are exactly the same
\FUNCTION{}{ }
\ENDFUNCTIONA
is:
\COMMENT{} A
,, ormay include the following:`tex`
% Normal characters
Hello world
% Escaped characters
\\, \{, \}, \$, \&, \#, \% and \_
% Math formula
$i \gets i + 1$
% Function call
\CALL{}{ }
% Keywords
\AND, \OR, \XOR, \NOT, \TO, \DOWNTO, \TRUE, \FALSE, \BREAK, \CONTINUE
% 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{}, \textrm{ }, \textsf{ }, \texttt{ }
\textup{}, \textit{ }, \textsl{ }, \textsc{ }
\uppercase{}, \lowercase{ }
\textbf, \textmd, \textlf
% And it's possible to group text with braces
normal text {\small the size gets smaller} back to normal againalgorithm> Note
> 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, useenvironment as a 'float' wrapper :`tex`
\begin{algorithm}
\caption{The caption of your algorithm}
\begin{algorithmic}
\STATE ...
\end{algorithmic}
\end{algorithm}pseudocode.renderElement$3
#### Global Options
can accept an option object as the last argument, such as`js`
pseudocode.renderElement(document.getElementById("quicksort"),
{ lineNumber: true });indentSizeThe following options are currently supported:
*
: The indent size of inside a control block, e.g. if, for,commentDelimiter
etc. The unit must be in 'em'.
*: The delimiters used to start and end a comment region.lineNumber
Note that only line comments are supported.
*: Whether line numbering is enabled.lineNumberPunc
*: The punctuation that follows line number.noEnd
*: Whether block ending, likeend if, end procedure, etc., are
showned.
*captionCount: Reset the caption counter to this number.The default values of these options are:
``
js`
var DEFAULT_OPTIONS = {
indentSize: '1.2em',
commentDelimiter: '//',
lineNumber: false,
lineNumberPunc: ':',
noEnd: false,
captionCount: undefined
};#### Per-Element Options
The above-mentioned global options may be overridden on a per-element basis
using HTML data-*attributes
on theDOM element.`The following example demonstrates how to enable line numbers and change title prefix:
html
data-line-number=true data-title-prefix="Algo">
...
``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:
bash`
cd pseudocode.js/
npm install
makebuild/katex-samples.htmlThen, open one of the sample documents:
-, orbuild/mathjax-v2-samples.html
-, orbuild/mathjax-v3-samples.html`
-
in your favorite browser to check if the algorithms are typeset correctly.
Author
pseudocode.js was originally written by Tate Tian (@tatetian).
Together with @ZJUGuoShuai,
I (@SaswatPadhi) added the MathJax support,
and I am the current maintainer of this project.
Suggestions, bug reports and pull requests are most welcome.Acknowledgement
pseudocode.js is partially inspired by KaTeX.
Thanks Emily Eisenberg(@xymostech)
and other contributors for building such a wonderful project.