Pseudocode
The documentation embeds some algorithms written using pseudocode.js
whose syntax is mostly compatible with LaTeX package algorithmic
.
The complete documentation and grammar is available at pseudocode.js grammar reference.
In order to display pseudocode properly you have to use [.pseudocode] on a literal block.
|
1. Basic Structure
Every pseudocode block follows this basic structure:
[.pseudocode]
....
\begin{algorithm}
\caption{Simple Example}
\begin{algorithmic}
\STATE $x \leftarrow 0$
\STATE $y \leftarrow 1$
\RETURN{$x + y$}
\end{algorithmic}
\end{algorithm}
....
\begin{algorithm} \caption{Simple Example} \begin{algorithmic} \STATE $x \leftarrow 0$ \STATE $y \leftarrow 1$ \RETURN{$x + y$} \end{algorithmic} \end{algorithm}
2. Available Commands
2.1. Control Structures
Command | Syntax | Description |
---|---|---|
|
|
Conditional statement |
|
|
Else-if branch |
|
|
Else branch |
|
|
End of if block |
|
|
For loop |
|
|
For-all loop |
|
|
While loop |
|
|
Repeat-until loop |
|
|
Until condition |
2.2. Statements and Actions
Command | Syntax | Description |
---|---|---|
|
|
General statement |
|
|
Print statement |
|
|
Return statement |
|
|
Function call |
|
|
Add comment |
3. Complete Examples
3.1. Example 1: Quicksort Algorithm
[.pseudocode]
....
% Quicksort from 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
\ENDFOR
\STATE exchange $A[i + 1]$ with $A[r]$
\RETURN{$i + 1$}
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
....
% Quicksort from 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 \ENDFOR \STATE exchange $A[i + 1]$ with $A[r]$ \RETURN{$i + 1$} \ENDPROCEDURE \end{algorithmic} \end{algorithm}
3.2. Example 2: Binary Search
[.pseudocode]
....
\begin{algorithm}
\caption{Binary Search}
\begin{algorithmic}
\FUNCTION{BinarySearch}{$A, target, low, high$}
\WHILE{$low \leq high$}
\STATE $mid \leftarrow \lfloor (low + high) / 2 \rfloor$
\IF{$A[mid] = target$}
\RETURN{$mid$}
\ELSIF{$A[mid] < target$}
\STATE $low \leftarrow mid + 1$
\ELSE
\STATE $high \leftarrow mid - 1$
\ENDIF
\ENDWHILE
\RETURN{$-1$} \COMMENT{Not found}
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
....
\begin{algorithm} \caption{Binary Search} \begin{algorithmic} \FUNCTION{BinarySearch}{$A, target, low, high$} \WHILE{$low \leq high$} \STATE $mid \leftarrow \lfloor (low + high) / 2 \rfloor$ \IF{$A[mid] = target$} \RETURN{$mid$} \ELSIF{$A[mid] < target$} \STATE $low \leftarrow mid + 1$ \ELSE \STATE $high \leftarrow mid - 1$ \ENDIF \ENDWHILE \RETURN{$-1$} \COMMENT{Not found} \ENDFUNCTION \end{algorithmic} \end{algorithm}
3.3. Example 3: Graph Traversal
[.pseudocode]
....
\begin{algorithm}
\caption{Breadth-First Search}
\begin{algorithmic}
\PROCEDURE{BFS}{$G, s$}
\FORALL{$u \in V[G] \setminus \{s\}$}
\STATE $color[u] \leftarrow WHITE$
\STATE $d[u] \leftarrow \infty$
\STATE $\pi[u] \leftarrow NIL$
\ENDFOR
\STATE $color[s] \leftarrow GRAY$
\STATE $d[s] \leftarrow 0$
\STATE $\pi[s] \leftarrow NIL$
\STATE $Q \leftarrow \emptyset$
\STATE \CALL{Enqueue}{$Q, s$}
\WHILE{$Q \neq \emptyset$}
\STATE $u \leftarrow$ \CALL{Dequeue}{$Q$}
\FORALL{$v \in Adj[u]$}
\IF{$color[v] = WHITE$}
\STATE $color[v] \leftarrow GRAY$
\STATE $d[v] \leftarrow d[u] + 1$
\STATE $\pi[v] \leftarrow u$
\STATE \CALL{Enqueue}{$Q, v$}
\ENDIF
\ENDFOR
\STATE $color[u] \leftarrow BLACK$
\ENDWHILE
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
....
\begin{algorithm} \caption{Breadth-First Search} \begin{algorithmic} \PROCEDURE{BFS}{$G, s$} \FORALL{$u \in V[G] \setminus \{s\}$} \STATE $color[u] \leftarrow WHITE$ \STATE $d[u] \leftarrow \infty$ \STATE $\pi[u] \leftarrow NIL$ \ENDFOR \STATE $color[s] \leftarrow GRAY$ \STATE $d[s] \leftarrow 0$ \STATE $\pi[s] \leftarrow NIL$ \STATE $Q \leftarrow \emptyset$ \STATE \CALL{Enqueue}{$Q, s$} \WHILE{$Q \neq \emptyset$} \STATE $u \leftarrow$ \CALL{Dequeue}{$Q$} \FORALL{$v \in Adj[u]$} \IF{$color[v] = WHITE$} \STATE $color[v] \leftarrow GRAY$ \STATE $d[v] \leftarrow d[u] + 1$ \STATE $\pi[v] \leftarrow u$ \STATE \CALL{Enqueue}{$Q, v$} \ENDIF \ENDFOR \STATE $color[u] \leftarrow BLACK$ \ENDWHILE \ENDPROCEDURE \end{algorithmic} \end{algorithm}
3.4. Example 4: Simple Loop Constructs
[.pseudocode]
....
\begin{algorithm}
\caption{Loop Examples}
\begin{algorithmic}
\PROCEDURE{LoopExamples}{$n$}
\FOR{$i = 0$ \TO $n$}
\PRINT{$i$}
\ENDFOR
\FOR{$j = n$ \DOWNTO $1$}
\PRINT{$j$}
\ENDFOR
\STATE $x \leftarrow 0$
\REPEAT
\STATE $x \leftarrow x + 1$
\PRINT{$x$}
\UNTIL{$x = n$}
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
....
\begin{algorithm} \caption{Loop Examples} \begin{algorithmic} \PROCEDURE{LoopExamples}{$n$} \FOR{$i = 0$ \TO $n$} \PRINT{$i$} \ENDFOR \FOR{$j = n$ \DOWNTO $1$} \PRINT{$j$} \ENDFOR \STATE $x \leftarrow 0$ \REPEAT \STATE $x \leftarrow x + 1$ \PRINT{$x$} \UNTIL{$x = n$} \ENDPROCEDURE \end{algorithmic} \end{algorithm}
4. Mathematical Expressions
Pseudocode.js supports full LaTeX mathematical notation within $…$
delimiters:
-
Variables:
$x, y, z$
-
Subscripts/Superscripts:
$A[i], x^2, log_2(n)$
-
Greek letters:
$\alpha, \beta, \gamma, \pi$
-
Set notation:
$\{1, 2, 3\}, \emptyset, \in, \subset$
-
Mathematical operators:
$\leftarrow, \rightarrow, \leq, \geq, \neq$
-
Functions:
$\lfloor x \rfloor, \lceil x \rceil, \max(a,b)$
5. Comments and Formatting
-
Use
%
for single-line comments -
Use
\COMMENT{text}
for inline comments -
Mathematical expressions are automatically rendered with MathJax
-
Indentation is handled automatically based on control structures
6. Usage Tips
-
Always use the
.pseudocode
class on your literal block -
Wrap math in dollar signs for proper rendering
-
Use proper nesting of control structures
-
End blocks explicitly with appropriate
\END…
commands -
Mathematical expressions are automatically processed by MathJax after pseudocode rendering