This MkDocs plugin allows you to write beautiful pseudocode in your MkDocs documentation using a simplified syntax inspired by LaTeX.
- Render pseudocode blocks with LaTeX-inspired syntax
- Support for common control structures (
if
,for
,while
,repeat
,foreach
) - Math expressions rendered using MathJax
- Nested procedures and calls
- Automatic indentation and keyword highlighting
-
Install the plugin locally:
pip install mkdocs-pseudocode
-
Add the plugin to your
mkdocs.yml
configuration file:plugins: - search - pseudocode
-
Copy CSS file into your
extra.css
file of your Mkdocs projectPlace the
pseudo-code.css
file in thedocs/css/
directory of your MkDocs project.Add the following lines to your
mkdocs.yml
file:extra_css: - css/pseudo-code.css
Write your pseudocode in fenced code blocks with the pseudocode
language identifier:
```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
\ENDFOR
\STATE exchange $A[i]$ with $A[r]$
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
You can customize the appearance of the pseudocode by adding your own CSS rules. The default CSS classes used are:
.ps-root
.ps-algorithm
.ps-algorithmic
.ps-caption
.ps-keyword
.ps-procedure
.ps-if
.ps-for
.ps-while
.ps-repeat
.ps-foreach
.ps-state
.ps-call
.ps-funcname
.ps-indent-{level}
To ensure MathJax expressions are rendered correctly, you may need to add the following JavaScript snippet to your MkDocs theme:
<script>
document.addEventListener("DOMContentLoaded", function() {
if (typeof MathJax !== 'undefined') {
MathJax.typesetPromise();
}
});
</script>
This project is licensed under the MIT License.
Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.
Jules TOPART