sphinxcontrib-pseudocode 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, we can also reference each algorithm:

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 will be messed up.

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

\[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \]
             \begin{algorithmic}
 \PRINT \texttt{'hello world'}
 \end{algorithmic}
        
\[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \]
\[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \]
\[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \]
\[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \]
\[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \]
\[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \]

Cross-References with :ref:

You can use RST :ref: roles inside a pcode block to link to any labeled algorithm (or any other labeled element) on the page. The role is resolved at build time and rendered as a clickable <a> link in the algorithm body.

For example, we can write

.. pcode::

   \begin{algorithm}
   \caption{A variant of Quicksort}
   \begin{algorithmic}
   \STATE Partition step is the same as in :ref:`Quicksort <quick-sort>`
   \PROCEDURE{Quicksort-Variant}{$A, p, r$}
       \IF{$p < r$}
           \STATE $q = $ \CALL{Partition}{$A, p, r$}
           \STATE \CALL{Quicksort-Variant}{$A, p, q - 1$}
           \STATE \CALL{Quicksort-Variant}{$A, q + 1, r$}
       \ENDIF
   \ENDPROCEDURE
   \end{algorithmic}
   \end{algorithm}

and the code will get rendered as

\[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \]

The :ref: role supports both the short form :ref:`label` and the long form :ref:`display text <label>`.

Custom Macros with \newcommand

You can define your own LaTeX macros using \newcommand in three ways:

  1. Inline Macros: Define the macro at the beginning of the pcode block. The macro will only be available for that specific block.

    Note

    Due to a quirk in the reStructuredText parser, a directive’s content block cannot begin with a \. To avoid issues, do not start a pcode block directly with a \newcommand. The simplest workaround is to add a comment line before the first macro definition.

    For example, we can write

    .. pcode::
    
       % A comment to avoid parser issues
       \newcommand{\floor}[1]{\lfloor #1 \rfloor}
    
       \begin{algorithm}
       \caption{Using an Inline Macro}
       \begin{algorithmic}
       \STATE The floor of 3.14 is $\floor{3.14}$.
       \end{algorithmic}
       \end{algorithm}
    

    and the code will get rendered as

    \[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \newcommand{\floor}[1]{\lfloor #1 \rfloor} \]
  2. Per-Page Macros: Define macros in a .. math:: block. These macros will be available to all pcode blocks on the same page. For example, we can write

    .. math::
    
       \newcommand{\ceil}[1]{\lceil #1 \rceil}
    
    .. pcode::
    
       \begin{algorithm}
       \caption{Using a Per-Page Macro}
       \begin{algorithmic}
       \STATE The ceiling of 3.14 is $\ceil{3.14}$.
       \end{algorithmic}
       \end{algorithm}
    

    and the code will get rendered as

    \[\newcommand{\ceil}[1]{\lceil #1 \rceil}\]
    \[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \]
  3. Global Macros: Define macros in your conf.py file using the mathjax3_config setting. These macros will be available across your entire Sphinx project.

    # In conf.py
    mathjax3_config = {
        'tex': {
            'macros': {
                'RR': r'\mathbb{R}',
            }
        }
    }
    

    Then we can write

    .. pcode::
    
       \begin{algorithm}
       \caption{Using a Global Macro}
       \begin{algorithmic}
       \STATE The set of real numbers is $\RR$.
       \end{algorithmic}
       \end{algorithm}
    

    and the code will get rendered as

    \[ \newcommand{\ceil}[1]{\lceil #1 \rceil} \]