# Typesetting Algorithms in reStructuredText

In Blog.

tags: Blogrsttypesetting

This blog is written in rst, coming from LaTex I often want to add math equations and this is straightforward to achieve using MathJax. Latex offers excellent packages for typesetting algorithms. Unfortunately, no rst equivalent seems to exist. The solution I'm trying at the moment is create small fragments in LaTex convert them to SVG and add them as these as images to the blog. Here are the steps I'm currently experimenting with. The first state is create a standalone latex document which just contains the markup to typeset the algorithm.

```
\documentclass{standalone}
\usepackage{varwidth}
\usepackage{algorithm} %ctan.org\pkg\algorithms
\usepackage{algpseudocode}
\begin{document}
\begin{varwidth}{\linewidth}
\par\noindent
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}
\State $r\gets a\bmod b$
\While{$r\not=0$}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile
\State \textbf{return} $b$
\EndProcedure
\end{algorithmic}
\end{varwidth}
\end{document}
```

This next stage is compile the fragments and convert it to svg using the following:

```
pdflatex tmp.tex; pdf2svg tmp.pdf tmp.svg
```

The resuling svg figure can be included as an image in the rst document.

```
.. image:: tmp.svg
```

To yield the following:

Surrounding the algorithmic in an algorithm environment causes compilation errors. In addition the document doesn't scale around comments, which are instead cropped off. It would be nice to add some sort of custom role that accepts the algorithm markup and automates the entire process. The final feature I'm missing form this blog is and rst equivalent of bibtex.