Giter Club home page Giter Club logo

pseudocode.js's Introduction

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

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.16.7/katex.min.js"
        integrity="sha512-EKW5YvKU3hpyyOcN6jQnAxO/L8gts+YdYV6Yymtl8pk9YlYFtqJgihORuRoBXK8/cOIlappdU6Ms8KdK6yBCgA=="
        crossorigin="anonymous" referrerpolicy="no-referrer">
</script>

Step 1B · For MathJax 2.x users

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

<script src="https://cdn.jsdelivr.net/npm/[email protected]/MathJax.js?config=TeX-AMS_CHTML-full"
        integrity="sha256-DViIOMYdwlM/axqoGDPeUyf0urLoHMN4QACBKyB58Uw="
        crossorigin="anonymous" referrerpolicy="no-referrer">
</script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [['$','$'], ['\\(','\\)']],
            displayMath: [['$$','$$'], ['\\[','\\]']],
            processEscapes: true,
            processEnvironments: true,
        }
    });
</script>

Note The -full configuration is larger and loads more extensions, but I recommend using it it just to avoid any hiccups later. You may want to use the standard configuration instead if you do not require additional packages.

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-full.js"
        integrity="sha256-kbAFUDxdHwlYv01zraGjvjNZayxKtdoiJ38bDTFJtaQ="
        crossorigin="anonymous">
</script>

Note The -full configuration is larger and loads more extensions, but I recommend using it it just to avoid any hiccups later. You may want to use the standard configuration instead if you do not require additional packages, such as color.

Step 2 · Grab pseudocode.js

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

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/build/pseudocode.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/pseudocode.min.js">
</script>

You may also use the latest tag for pseudocode instead, but jsDelivr might be delayed in updating the pointer for this tag.

<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">
    % 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 all elements of 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>, <block>, or <condition> may 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, \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{<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 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

Global Options

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

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

The following options are currently supported:

  • captionCount: Reset the caption counter to this number.
  • commentDelimiter: The delimiters used to start and end a comment region. Note that only line comments are supported.
  • indentSize: The indent size of inside a control block, e.g. if, for etc. The unit must be in 'em'.
  • 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.
  • titlePrefix: The title prefix (defaults to "Algorithm") for captions.

The default values of these options are:

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 the <pre> DOM element.

The following example demonstrates how to enable line numbers and change title prefix:

<pre id="quicksort" class="pseudocode"
     data-line-number=true data-title-prefix="Algo">
   ...
</pre>

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.

pseudocode.js's People

Contributors

atomicvar avatar dependabot[bot] avatar heydariplusplus avatar j08ny avatar josedusol avatar leovan avatar marcosroriz avatar mavoort avatar ricopicone avatar saswatpadhi avatar tatetian avatar validark avatar zacharyespiritu avatar zcysxy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pseudocode.js's Issues

Rendering problem in markdown and Jekyll

Hi Tate,

Thanks for the project. I am using it in my own blog built with Jekyll and Minimal theme. When I put the following raw html code in the middle of Markdown file

<pre id="ts" style="display:none">
\begin{algorithmic}
\PRINT \texttt{'hello world'}
\end{algorithmic}
</pre>

<script type="text/javascript">
    var testExamples = document.getElementById("ts").textContent;
    pseudocode.render(testExamples, document.body, {
        lineNumber: true,
        noEnd: false
    });
</script>

the output html will put the algorithm in the end of the post like the following:

image

I will appreciate it a lot if you can provide some helpful suggestions.

Thanks,

Chao

Algorithms numbering

Thanks for writing pseudocode.js. It looks quite amazing.

By default, pseudocode.js does an automatic numbering starting at 1. I am personally trying to use it in a book, so I need a numbering with the chapters, like Algorithm 2.1 or Algorithm A.1.

I wonder if it would be possible to somehow specify the desired chapter prefix (in the example above it would be prefix "2" or "A").

In the worst case, I wonder if it would be possible to fully specify the number by hand rather than automatic. In the example above, I would specify "2.1" or "A.1".

Thanks!

Render Elements By Class Name

I would like to use this in Markdown, like

```pseudo
some algorithms
```

And this gives the <pre> element a "pseudo" class. So it would be great to support getElementsByClassName.

Support for MathJax

First of all, thanks a lot for this project. It looks great, and is quite useful.

I was wondering if it would be possible to add support for MathJax, together with KaTex. Although being a bit slower, MathJax would allow to render any equation.

Best,
Séb

Completely hide caption prefix

Great package I love it thank you ! is there a way force the Algorithm 1 part of the caption to my own words ?
Algorithm 1 New Quicksort

Regards
Haydar

Add some javascript code to automatically find and replace blocks.

I had to write some generic javascript in order to use what you wrote to find the blocks of pseudocode, render them, and remove the old ones. Seems to me this can be included as a small helper javascript method that does all that. I have some code that will find any and all blocks in a webside, delete it and replace it with the rendered code in the same place as the original block. You can see the javascript code and incorporate it into this code if you'd like. Here is a link and an explanation to my javascript to solve this.

http://jeffreyfreeman.me/latex-inspired-rendering-of-algorithms-in-html/

Uppercase variables

Pseudocode currently seems to lowercase all variables in tex code, as can be seen on the project website (note that the first argument, a, in the quicksort algorithm is uppercase in the code, but lowercase in the output).

Update rendering dynamically (on button click)

I would like to re-rendering the <pre> in the JS click event. The core logic is to update the content of <pre>, and call pseudocode.renderElement again. But re-rendering fails and it still shows the content before updating.

    const textInput = document.getElementById("input");
    const output = document.getElementById("output");

    const updateButton = document.getElementById("generate");
    updateButton.addEventListener("click", () => {
      const inputValue = textInput.value;
      output.innerHTML = inputValue;
      pseudocode.renderElement(output);
    });

Fail to import css in vue-cli

I'm trying to use pseudocode.js in a vue component.

But when I import pseudocode/build/pseudocode.min.css, vue-cli fails to build the app.

I think that maybe it losses some fonts? This is the error log.

ERROR  Failed to compile with 8 errors
These relative modules were not found:

These relative modules were not found:

* ./fonts/KaTeX_SansSerif-Bold.eot in ./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-3-1!./node_modules/postcss-loader/src??ref--6-oneOf-3-2!./node_modules/pseudocode/build/pseudocode.min.css
* ./fonts/KaTeX_SansSerif-Bold.ttf in ./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-3-1!./node_modules/postcss-loader/src??ref--6-oneOf-3-2!./node_modules/pseudocode/build/pseudocode.min.css
* ./fonts/KaTeX_SansSerif-Bold.woff in ./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-3-1!./node_modules/postcss-loader/src??ref--6-oneOf-3-2!./node_modules/pseudocode/build/pseudocode.min.css
* ./fonts/KaTeX_SansSerif-Bold.woff2 in ./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-3-1!./node_modules/postcss-loader/src??ref--6-oneOf-3-2!./node_modules/pseudocode/build/pseudocode.min.css
* ./fonts/KaTeX_Typewriter-Regular.eot in ./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-3-1!./node_modules/postcss-loader/src??ref--6-oneOf-3-2!./node_modules/pseudocode/build/pseudocode.min.css
* ./fonts/KaTeX_Typewriter-Regular.ttf in ./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-3-1!./node_modules/postcss-loader/src??ref--6-oneOf-3-2!./node_modules/pseudocode/build/pseudocode.min.css
* ./fonts/KaTeX_Typewriter-Regular.woff in ./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-3-1!./node_modules/postcss-loader/src??ref--6-oneOf-3-2!./node_modules/pseudocode/build/pseudocode.min.css
* ./fonts/KaTeX_Typewriter-Regular.woff2 in ./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-3-1!./node_modules/postcss-loader/src??ref--6-oneOf-3-2!./node_modules/pseudocode/build/pseudocode.min.css

And This is my component.

<template>
  <pre ref="pre" style="display:hidden;"><slot></slot></pre>
</template>

<script>
import pseudocode from 'pseudocode';
import 'pseudocode/build/pseudocode.min.css';

export default {
  name: 'Pseudocode',
  props: {
    lineNumber: Boolean,
    lineNumberPunc: String
  },
  mounted() {
    this.$nextTick(() => {
      pseudocode.renderElement(this.$refs.pre, {
        lineNumber: this.lineNumber,
        lineNumberPunc: this.lineNumberPunc
      });
    });
  }
}
</script>

Enable scopeLines (sidelines)

I think scope lines (not sure if it is the ‘formal name’) can be used to clarify algorithms. For instance, the screenshot below illustrates two algorithms with this functionality. Hence, I think it can be useful to enable the library to include this functionality. I implemented the changes and I’m doing a pull request for your consideration. The change basically adds an optional argument named scopeLines (false by default) that render the sidelines.

image

just a thanks

Sorry for the improper placement, but I found no other way to thank the contributors. I was making svgs of LaTeX-generated pseudocode for the web. This is so much better! Great work.

MathJax 4.0

I am trying to use pseudocode.js with MathJax 4.0. I have followed all the four steps listed in the ReadMe file, but cannot seem to get it to work with MathJax 4.0. Here is how I call MathJax 4.0:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-mml-chtml.js"
        integrity="9df6fbc821287c8717485c417ee18bde3f94a30d18d2dec88c29200806c7dabe"
        crossorigin="anonymous" referrerpolicy="no-referrer">
</script>

I18n for "Algorithm" string in caption

The "Algorithm" string appearing in caption title is hardcoded in the source.

Could this be made more flexible for other languages?, maybe adding an option to customize the caption prefix. For example in Spanish is "Algoritmo", and in french is "Algorithme".

Good project. Thanks

renderElement TypeError when backend doesn't exist

I'm getting the following error.
image

I'm using URLSearchParams like that visible in the error, ?TS=T1&DS=, and it seems this might be where the trouble lies. Interestingly, the algorithm still renders properly.

Any ideas?

Server side rendering

Hi, and thanks for sharing your code.

I would like to know if I can use this library from the server-side, something like the katex method: katex.renderToString(...).

JSON parsing error in `renderElement`

I'm trying to run the README example -- but I'm running into a JSON.parse error in renderElement:

image

I'm building a static site through quarto and hugo, and I've confirmed that my HTML output is what I expect -- specifically:

<pre id="hello-world-code" class="pseudocode">
  \begin{algorithmic}
  \PRINT \texttt{'hello world'}
  \end{algorithmic}
</pre>
<script>
    pseudocode.renderElement(document.getElementById("hello-world-code"));
</script>

gets inserted into my generated HTML (as I would expect). I also fetch pseudocode.js from CDN in header.

Any mistake I've made standing out?

Update to KaTex-0.8

Hi @tatetian ,
nice work, I was hoping to use your code to format my pseudo code it looks like there hasn't been any activity for long while. I was wondering if you could build a new version with newer KaTex? I would like to have \mathcal{}, \right & \left working to name a few.

T.I.A.

The limit of rendering elements is 10

This is how I setup my MathJax and pseudocode.js

<!-- Support for MatJax -->
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script>
    MathJax = {
        tex: {
            inlineMath: [['$','$'], ['\\(','\\)']],
            displayMath: [['$$','$$'], ['\\[','\\]']],
            processEscapes: true,
            processEnvironments: true,
        }
    }
</script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

<!-- Support for pseudocode.js -->
<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>

And I use this script to render all the elements with pseudocode class

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

However, when I write pseudocode in my markdown file, seems that I can render at most 10 algorithms. And the one not working is from 3 to 4. 3 is working but starting from 4, every pseudocode doesn't work.

3. Print the winner of the election, if there is one (`bool print_winner(void)`)
    <pre id="print_winner-02" class="pseudocode">
        \begin{algorithm}
        \caption{Print Winner}
        \begin{algorithmic}
        \PROCEDURE{PrintWinner}{}
            \STATE Define winning indicator as voter count divides 2
            \FOR{each candidate}
                \IF{candidate's votes > winning indicator}
                    \STATE Print
                    \RETURN true
                \ENDIF
            \ENDFOR
            \RETURN false
        \ENDPROCEDURE
        \end{algorithmic}
        \end{algorithm}
    </pre>

4. Return the minimum number of votes any remaining candidate has (`int find_min(void)`)
    <pre id="find_min" class="pseudocode">
        \begin{algorithm}
        \caption{Find Min}
        \begin{algorithmic}
        \PROCEDURE{FindMin}{}
            \STATE Initialize the variable min to MAX_VOTERS + 1
            \FOR{each candidate}
                \IF{candidate's votes is less than min}
                    \STATE Update min
                \ENDIF
            \ENDFOR
            \RETURN min
        \ENDPROCEDURE
        \end{algorithmic}
        \end{algorithm}
    </pre>

And here is the my website

Thanks for helping!

Problem with repeat...until loop with noEnd: true

When noEnd: true is set, the repeat...until loop does not render the until line. Although it seems to be working as intended, the until line contains an important part of the algorithm and should never be omitted. It is a different ending statement than end for, end if, and so on, so it should be treated differently.

I would expect to hide all end if, end for, and so on with noEnd: true, but keep until line regardless.

Color of horizontal Lines incompatible with dark Mode

On a web page with white text and a dark background ("dark mode"), the pseudocode block looks a bit strange.

In particular, the horizontal lines remain black, even though the text and the vertical lines are white.

pseudocode_bug-black_lines

To fix this, I suggest replacing

border: 2px solid black;

inside of pseudocode.css with:

border-width: 2px;
border-style: solid;

This way you could use pseudocode.js also on a dark website.

Render problem in existing mathmatical multiline expressions (R-Bookdown,R-Markdown)

Hello,
i use your pseudocode.js lib in my r-bookdown project to generate the nice latex algorithmic output. The include step was stright forward, as it is mentioned in the usage guide in the readme.

  1. include mathjax and pseudocode.js in header: I have a header_index.html that lists everything that goes into the header tag. Beause there was already an existing entry of mathjax i tried it first, without the new mathjax-include, but this did not work and i added the include for mathjax3.
  2. write code: after that setup and some modifications on the css-file i could add example code in my Rmd-file (R-Markdown) and had the expected output in the browser.

However, I got some errors, existing mathematical expressions were apparently influenced by the change in MathJax. In my book i had both, inline expressions ($somesthing$) and also multiline expressions ($$ centered mathmatical expression $$) and only the latter expressions are affected and are not correctly displayed.

For Example: from following expression:
$$L[k](A) = \prod_{i=1}^N[a_i,k]\cdot h[k]$$
is the folling output generated:

I noticed the following lines in the include of MathJax:

inlineMath: [['$','$'], ['\\(','\\)']],
displayMath: [['$$','$$'], ['\[','\]']],

The second line looks to me what influences multiline expressions and has just one "\" insteed of two "\\" as it is in the top line. I changed it to the following:
displayMath: [['$$','$$'], ['\\[','\\]']],

and voila, that solve my problem.

I`m not very familiar with javascript and related subjects, so I don't really understand what happens in the include statement and if the backslash is really missing or not. As a simple test I created a simple HTML page. There it seems to be no matter if there are 1 or 2 backslashes in the expression.

Algorithm numbers as hyperlinks

Just wondering if algorithms can be references as a hyperlinks as, for example, Equations. For example, the below latex equation could be referenced throughout a document, with the number being a hyperlink to the equation.

\begin{align}
x = 1 
\label{eq:equation}
\end{align}

If I try adding something similar to an algorithm environment (whether insider the algorithm or algorithmic environments), the algorithm does not compile, and as a result, does not show up on my webpage. That is, if I remove \label{alg:grad_descent} from the code below, the pseudocode block compiles and shows up.

<pre id="eq:grad_descent" class="pseudocode"  data-line-number=true >
    \begin{algorithm}
    \caption{Gradient Descent}
    \begin{algorithmic}
     \label{alg:grad_descent}
    \REQUIRE{Function for computing loss function, $\ell_{MSE}$ (see Equation $\ref{eq:mse-matrix}$)}
    \INPUT{Initial guess for weights $\mathbf{w}^{(0)}$} 
    \PROCEDURE{gradientDescent}{$K, \epsilon, \tau$}
      \STATE{$k = 0$}
     \WHILE{$k < K$ \AND $\lVert \mathbf{w}^{(k)} - \mathbf{w}^{(k+1)} \rVert^2_2 < \epsilon$} 
     \STATE{$\mathbf{w}^{(k+1)} = \mathbf{w}^{(k)} - \tau \nabla_{\mathbf{w}}\ell_{MSE}(\mathbf{w}^{(k)})$.} 
     \STATE{$k \mathrel{+}= 1$}
      \ENDWHILE
    \ENDPROCEDURE
    \end{algorithmic}
    \end{algorithm}
</pre>

Here is how I call pseudocode.js ( I call is internally, given that I am using MathJax 4.0.0-beta3).

  <script src="https://cdn.jsdelivr.net/npm/[email protected]/tex-chtml.js"
              integrity="sha256-ljPODBK7Jf/VfUrVqec63xzZbysEmwB9Ab20TWRMQRU="
              crossorigin="anonymous">
  </script>


  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/build/pseudocode.min.css">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/pseudocode.min.js"></script>
  <script src="{{ "js/pseudocode.js" | relURL }}"></script>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.