All Projects → SaswatPadhi → pseudocode.js

SaswatPadhi / pseudocode.js

Licence: MIT License
Beautiful pseudocode for the Web

Programming Languages

javascript
184084 projects - #8 most used programming language
Makefile
30231 projects
CSS
56736 projects

Projects that are alternatives of or similar to pseudocode.js

Spmathkit
Render math exercises for your view. Contains the rendering of mathematical formulas and mathematical graphics, as well as the general text
Stars: ✭ 81 (-38.64%)
Mutual labels:  latex, mathjax, katex
Flutter tex
A Flutter Package to render Mathematics, Physics and Chemistry Equations based on LaTeX
Stars: ✭ 161 (+21.97%)
Mutual labels:  latex, mathjax, katex
Jekyll Spaceship
🚀 A Jekyll plugin to provide powerful supports for table, mathjax, plantuml, mermaid, emoji, video, audio, youtube, vimeo, dailymotion, soundcloud, spotify, etc.
Stars: ✭ 196 (+48.48%)
Mutual labels:  latex, mathjax, katex
purple-pi
💜 LaTeX math wherever you want
Stars: ✭ 31 (-76.52%)
Mutual labels:  latex, mathjax, katex
Jaxbin
MathJax and TeX pastebin
Stars: ✭ 15 (-88.64%)
Mutual labels:  latex, mathjax
Cocalc
CoCalc: Collaborative Calculation in the Cloud
Stars: ✭ 888 (+572.73%)
Mutual labels:  latex, mathjax
Jekyll Katex
Jekyll plugin for easy server-side math rendering via KaTeX
Stars: ✭ 73 (-44.7%)
Mutual labels:  latex, katex
React Katex
Display math in TeX with KaTeX and ReactJS
Stars: ✭ 345 (+161.36%)
Mutual labels:  latex, katex
Remark Math
remark and rehype plugins to support math
Stars: ✭ 129 (-2.27%)
Mutual labels:  latex, katex
Texme
Self-rendering Markdown + LaTeX documents
Stars: ✭ 1,970 (+1392.42%)
Mutual labels:  latex, mathjax
notes
Simple text editor for your Markdown and LaTeX notes.
Stars: ✭ 24 (-81.82%)
Mutual labels:  mathjax, katex
Papers
Publications from the MathJax project
Stars: ✭ 6 (-95.45%)
Mutual labels:  latex, mathjax
Mdmath
LaTeX Math for Markdown inside of Visual Studio Code.
Stars: ✭ 675 (+411.36%)
Mutual labels:  latex, katex
Mathjax
Beautiful and accessible math in all browsers
Stars: ✭ 8,551 (+6378.03%)
Mutual labels:  latex, mathjax
Franklin.jl
(yet another) static site generator. Simple, customisable, fast, maths with KaTeX, code evaluation, optional pre-rendering, in Julia.
Stars: ✭ 413 (+212.88%)
Mutual labels:  latex, katex
Katex
Fast math typesetting for the web.
Stars: ✭ 14,623 (+10978.03%)
Mutual labels:  latex, katex
ckeditor5-math
Math feature for CKEditor 5.
Stars: ✭ 51 (-61.36%)
Mutual labels:  mathjax, katex
Liandi
📕 一款桌面端的 Markdown 块级引用和双向链接笔记应用,支持 Windows、Mac 和 Linux。A desktop Markdown Block-Reference and Bidirectional-Link note-taking application, supports Windows, Mac and Linux.
Stars: ✭ 354 (+168.18%)
Mutual labels:  mathjax, katex
Qilin App
Fully hackable text editor developed for exact sciences with built-in KaTeX and AsciiMath support. Extensible via plugins and themes. Exportable as HTML, PDF and GFM.
Stars: ✭ 336 (+154.55%)
Mutual labels:  latex, katex
vscode-markdown-editor
A vscode extension to make your vscode become a full-featured WYSIWYG markdown editor
Stars: ✭ 249 (+88.64%)
Mutual labels:  mathjax, katex

pseudocode.js

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 IE 9 - IE 11.

Visit the project website for a demo.

Usage

Quick Start

pseudocode.js can render math formulas using either KaTeX, or MathJax.

Step 1A · For KaTeX users

Include the following in the <head> of your page:

<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.js"
        integrity="sha256-F/Xda58SPdcUCr+xhSGz9MA2zQBPb0ASEYKohl8UCHc=" crossorigin="anonymous">
</script>

Step 1B · For MathJax 2.x users

Include the following in the <head> of your page:

<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_CHTML'>
</script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [['$','$'], ['\\(','\\)']],
            displayMath: [['$$','$$'], ['\\[','\\]']],
            processEscapes: true,
            processEnvironments: true,
        }
    });
</script>

Step 1C · For MathJax 3.x users

Include the following in the <head> of your page:

<script>
    MathJax = {
        tex: {
            inlineMath: [['$','$'], ['\\(','\\)']],
            displayMath: [['$$','$$'], ['\\[','\\]']],
            processEscapes: true,
            processEnvironments: true,
        }
    }
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-chtml.js"
        integrity="sha256-3Fdoa5wQb+JYfEmTpQHx9sc/GuwpfC/0R9EpBki+mf8=" crossorigin>
</script>

Step 2 · Grab pseudocode.js

Include the following in the <head> of your page:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pseudocode@latest/build/pseudocode.min.css">
<script src="https://cdn.jsdelivr.net/npm/pseudocode@latest/build/pseudocode.min.js">
</script>

Step 3 · Write your pseudocode inside a <pre>

We assume the pseudocode to be rendered is in a <pre> DOM element. Here is an example that illustrates a quicksort algorithm:

<pre id="quicksort" class="pseudocode" style="display:hidden;">
    % 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}
</pre>

Step 4A · Render the element using pseudocode.js

Insert the following Javascript snippet at the end of your document:

<script>
    pseudocode.renderElement(document.getElementById("quicksort"));
</script>

Step 4B · Render the class using pseudocode.js

Insert the following Javascript snippet at the end of your document:

<script>
    pseudocode.renderClass("pseudocode");
</script>

Grammar

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:

\begin{algorithmic}
# A precondition is optional
\REQUIRE <text>
# A postcondition is optional
\ENSURE <text>
# An input is optional
\INPUT <text>
# An output is optional
\OUTPUT <text>
# The body of your code is a <block>
\STATE ...
\end{algorithmic}

<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
\IF{<condition>}
    <block>
\ELIF{<condition>}
    <block>
\ELSE
    <block>
\ENDIF
# Or a loop: \WHILE, \FOR or \FORALL
\WHILE{<condition>}
    <block>
\ENDWHILE
# Or a repeat: \REPEAT <block> \UNTIL{<cond>}
\REPEAT
    <block>
\UNTIL{<cond>}

# A <function> can by defined by either \FUNCTION or \PROCEDURE
# Both are exactly the same
\FUNCTION{<name>}{<params>}
    <block> 
\ENDFUNCTION

# A <comment> is:
\COMMENT{<text>}

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

# Normal characters
Hello world
# Escaped characters
\\, \{, \}, \$, \&, \#, \% and \_
# Math formula
$i \gets i + 1$
# Function call
\CALL{<func>}{<args>}
# Keywords
\AND, \OR, \XOR, \NOT, \TO, \DOWNTO, \TRUE, \FALSE
# 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 :

\begin{algorithm}
\caption{The caption of your algorithm}
\begin{algorithmic}
\STATE ...
\end{algorithmic}
\end{algorithm}

Options

pseudocode.renderElement can accept an option as the last argument, such as

pseudocode.renderElement(document.getElementById("quicksort"),
                         { lineNumber: true });

The following options are currently supported:

  • indentSize: The indent size of inside a control block, e.g. if, for, etc. The unit must be in 'em'.
  • commentDelimiter: The delimiters used to start and end a comment region. Note that only line comments are supported.
  • lineNumber: Whether line numbering is enabled.
  • lineNumberPunc: The punctuation that follows line number.
  • noEnd: Whether block ending, like end if, end procedure`, etc., are showned.
  • captionCount: Reset the caption counter to this number.

The default values of these options are:

var DEFAULT_OPTIONS = {
    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/
npm install
make

Then, open one of the sample documents:

  • build/katex-samples.html, or
  • build/mathjax-v2-samples.html, or
  • build/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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].