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:
:ref:`test control blocks <test-control-blocks>`produces test control blocks
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 numberingno-linenos: Disable line numberingindent: Set indentation (working only forem, no other units) for code blocks, default:1.2emcomment-delimiter: Customize comment delimiters, default://line-number-punc: Set line number punctuation, default::no-end: Omit theENDkeyword for control blockstitle-prefix: Customize the algorithm title prefix (e.g.,PseudoCodeinstead of defaultAlgorithm)caption-count: Reset the caption counter to this numberscopelines: 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.jsandalgorithmicdo 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}.. pcode::
\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} 1.. pcode::
2 :linenos:
3 :title-prefix: PseudoCode
4
5 % This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition)
6 \begin{algorithm}
7 \caption{Quicksort}
8 \begin{algorithmic}
9 \PROCEDURE{Quicksort}{$A, p, r$}
10 \IF{$p < r$}
11 \STATE $q = $ \CALL{Partition}{$A, p, r$}
12 \STATE \CALL{Quicksort}{$A, p, q - 1$}
13 \STATE \CALL{Quicksort}{$A, q + 1, r$}
14 \ENDIF
15 \ENDPROCEDURE
16 \PROCEDURE{Partition}{$A, p, r$}
17 \STATE $x = A[r]$
18 \STATE $i = p - 1$
19 \FOR{$j = p$ \TO $r - 1$}
20 \IF{$A[j] < x$}
21 \STATE $i = i + 1$
22 \STATE exchange
23 $A[i]$ with $A[j]$
24 \ENDIF
25 \STATE exchange $A[i]$ with $A[r]$
26 \ENDFOR
27 \ENDPROCEDURE
28 \end{algorithmic}
29 \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} 1.. pcode::
2 :title-prefix: 算法
3 :indent: 2.3em
4 :caption-count: 12
5
6 \begin{algorithm}
7 \caption{Test text-style}
8 \begin{algorithmic}
9 \REQUIRE some preconditions
10 \ENSURE some postconditions
11 \INPUT some inputs
12 \OUTPUT some outputs
13 \PROCEDURE{Test-Declarations}{}
14 \STATE font families: {\sffamily sffamily, \ttfamily ttfamily, \normalfont normalfont, \rmfamily rmfamily.}
15 \STATE font weights: {normal weight, \bfseries bold, \mdseries
16 medium, \lfseries lighter. }
17 \STATE font shapes: {\itshape itshape \scshape Small-Caps \slshape slshape \upshape upshape.}
18 \STATE font sizes: \tiny tiny \scriptsize scriptsize \footnotesize
19 footnotesize \small small \normalsize normal \large large \Large Large
20 \LARGE LARGE \huge huge \Huge Huge \normalsize
21 \ENDPROCEDURE
22 \PROCEDURE{Test-Commands}{}
23 \STATE \textnormal{textnormal,} \textrm{textrm,} \textsf{textsf,} \texttt{texttt.}
24 \STATE \textbf{textbf,} \textmd{textmd,} \textlf{textlf.}
25 \STATE \textup{textup,} \textit{textit,} \textsc{textsc,} \textsl{textsl.}
26 \STATE \uppercase{uppercase,} \lowercase{LOWERCASE.}
27 \ENDPROCEDURE
28 \PROCEDURE{Test-Colors}{}
29 % feature not implemented
30 \ENDPROCEDURE
31 \end{algorithmic}
32 \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}.. pcode::
:linenos:
:title-prefix: 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 \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}.. pcode::
:linenos:
:no-scopelines:
:line-number-punc: :
\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}.. pcode::
:comment-delimiter: %
:no-end:
:linenos:
:line-number-punc: !
\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}.. pcode::
:indent: 5em
:no-scopelines:
:linenos:
% 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}