Welcome to sphinxcontrib-pseudocode2 demo

Features

Below shows various rendered algorithms, which are copied from corresponding pseudocode.js examples. The source code of those algorithms can be found here.

We can also reference any particular algorithm. For example:

Note

We assume each pcode directive contains exactly one \(\LaTeX\) algorithmic block:

\begin{algorithm}
\caption{Test atoms}
\begin{algorithmic}
\STATE my algorithm 1
\END{ALGORITHMIC}
\END{ALGORITHM}

You still can have multiple algorithmic blocks but the numbering might be messed up.

By default, each pcode is mapped to ‘Algorithm %s’ when referenced via ref. You can change this behavior by overriding the corresponding string of 'pseudocode' key in numfig_format.

Configuration Options

Pseudocode rendering is extended with practical options (all compatible with pseudocode.js native capabilities):

  • linenos: Enable line numbering

  • no-linenos: Disable line numbering

  • indent: Set indentation (working only for em, no other units) for code blocks, default: 1.2em

  • comment-delimiter: Customize comment delimiters, default: //

  • line-number-punc: Set line number punctuation, default: :

  • no-end: Omit the END keyword for control blocks

  • title-prefix: Customize the algorithm title prefix (e.g., PseudoCode instead of default Algorithm)

  • caption-count: Reset the caption counter to this number

  • scopelines: Highlight scope lines (those with control block starters like IF, FOR, WHILE, etc.)

  • no-scopelines: Disable scope line highlighting

Global Configuration via pseudocode2_options

Pseudocode rendering styles can be unified across the entire project using a single global configuration (supports all pseudocode.js native parameters, see the Options section of [pseudocode.js](https://github.com/SaswatPadhi/pseudocode.js)). The following example shows how to set global options in conf.py:

pseudocode2_options = {
    "lineNumber": True,           # Global default: enable line numbering
    "lineNumberPunc": " | ",      # Punctuation after line numbers (e.g., "1 | ")
    "commentDelimiter": "#",      # Global default comment delimiter
    "noEnd": False,               # Global default: show "END" for control blocks
    "titlePrefix": "PseudoCode",  # Global default title prefix (replace "Algorithm")
    "scopeLines": True,           # Global default: enable scope line highlighting
}

Priority Rule: Configuration priority (higher priority overrides lower): Directive option (e.g., :linenos: in .rst) > pseudocode2_options (global in conf.py) > pseudocode.js default

Tips

  • Custom (Manual) indentation Control: pseudocode.js and algorithmic do not have a built-in way (a single command or a pair of commands) to set custom indentation levels. However, you can manually adjust indentation by the following workaround: use LaTeX’s horizontal space command inside a math environment. Specifically, use

    $\hspace{<length>}$ where <length> is a LaTeX length (e.g., 2em, 1cm, etc.). For example:

    \STATE $\hspace{2em}$ This line is indented by 2em
    

    See also this example

Examples

\begin{algorithmic}
\PRINT \texttt{'hello world'}
\end{algorithmic}
% 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}
\begin{algorithm}
\caption{Test text-style}
\begin{algorithmic}
\REQUIRE some preconditions
\ENSURE some postconditions
\INPUT some inputs
\OUTPUT some outputs
\PROCEDURE{Test-Declarations}{}
    \STATE font families: {\sffamily sffamily, \ttfamily ttfamily, \normalfont normalfont, \rmfamily rmfamily.}
    \STATE font weights: {normal weight, \bfseries bold, \mdseries
    medium, \lfseries lighter. }
    \STATE font shapes: {\itshape itshape \scshape Small-Caps \slshape slshape \upshape upshape.}
    \STATE font sizes: \tiny tiny \scriptsize scriptsize \footnotesize
    footnotesize \small small \normalsize normal \large large \Large Large
    \LARGE LARGE \huge huge \Huge Huge \normalsize
\ENDPROCEDURE
\PROCEDURE{Test-Commands}{}
    \STATE \textnormal{textnormal,} \textrm{textrm,} \textsf{textsf,} \texttt{texttt.}
    \STATE \textbf{textbf,} \textmd{textmd,} \textlf{textlf.}
    \STATE \textup{textup,} \textit{textit,} \textsc{textsc,} \textsl{textsl.}
    \STATE \uppercase{uppercase,} \lowercase{LOWERCASE.}
\ENDPROCEDURE
\PROCEDURE{Test-Colors}{}
% feature not implemented
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\caption{Test atoms}
\begin{algorithmic}
\STATE \textbf{Specials:} \{ \} \$ \& \# \% \_
\STATE \textbf{Bools:} \AND \OR \NOT \TRUE \FALSE
\STATE \textbf{Carriage return:} first line \\ second line
\STATE $\hspace{2em}$ This line is indented by 2em
\STATE $\hspace{2em}$ This line is indented by 2em
\STATE \textbf{Text-symbols:} \textbackslash
\STATE \textbf{Quote-symbols:} `single quotes', ``double quotes''
\STATE \textbf{Math:} $(\mathcal{C}_m)$, $i \gets i + 1$, $E=mc^2$, \( x^n + y^n = z^n \), $\$$, \(\$\)
\END{ALGORITHMIC}
\END{ALGORITHM}
\begin{algorithm}
\caption{Test control blocks}
\begin{algorithmic}
\PROCEDURE{Test-If}{}
    \IF{ <cond>}
        \STATE <block>;
    \ELIF{<cond>}
        \STATE <block>;
    \ELSE
        \STATE <block>;
    \ENDIF
\ENDPROCEDURE
\PROCEDURE{Test-For}{$n$}
    \STATE $i \gets 0$
    \FOR{$i < n$}
        \PRINT $i$
        \STATE $i \gets i + 1$
    \ENDFOR
\ENDPROCEDURE
\PROCEDURE{Test-For-To}{$n$}
    \STATE $i \gets 0$
    \FOR{$i$ \TO $n$}
        \PRINT $i$
    \ENDFOR
\ENDPROCEDURE
\PROCEDURE{Test-For-DownTo}{$n$}
    \FOR{$i \gets n$ \DOWNTO $0$}
        \PRINT $i$
    \ENDFOR
\ENDPROCEDURE
\PROCEDURE{Test-For-All}{$n$}
    \FORALL{$i \in \{0, 1, \cdots, n\}$}
        \PRINT $i$
    \ENDFOR
\ENDPROCEDURE
\PROCEDURE{Test-While}{$n$}
    \STATE $i \gets 0$
    \WHILE{$i < n$}
        \PRINT $i$
        \STATE $i \gets i + 1$
    \ENDWHILE
\ENDPROCEDURE
\PROCEDURE{Test-Repeat}{$n$}
    \STATE $i \gets 0$
    \REPEAT
        \PRINT $i$
        \STATE $i \gets i + 1$
    \UNTIL{$i>n$}
\ENDPROCEDURE
\PROCEDURE{Test-Break-Continue}{$n$}
    \FOR{$i = 0$ \TO $2n$}
        \IF{$i < n/2$}
            \CONTINUE
        \ELIF{$i > n$}
            \BREAK
        \ENDIF
        \PRINT $i$
    \ENDFOR
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\caption{Test statements and comments}
\begin{algorithmic}
\PROCEDURE{Test-Statements}{}
    \STATE This line is a normal statement
    \PRINT \texttt{`this is print statement'}
    \RETURN $retval$
\ENDPROCEDURE

\PROCEDURE{Test-Comments}{} \COMMENT{comment for procedure}
    \STATE a statement \COMMENT{inline comment}
    \STATE \COMMENT{line comment}
    \IF{some condition}\COMMENT{comment for if}
        \RETURN \TRUE \COMMENT{another inline comment}
    \ELSE \COMMENT{comment for else}
        \RETURN \FALSE \COMMENT{yet another inline comment}
    \ENDIF
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
% 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}