Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Inserting an algorithm in LaTeX

📅 2012-Jun-29 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

[caption id="attachment_2652" align="aligncenter" width="401"][ ](http://codeyarns.files.wordpress.com/2012/06/20120629-latex-algorithm.png) A simple algorithm in LaTeX[/caption]

One of the simple and beautiful ways to insert algorithms or procedures into a LaTeX document is using the algorithmicx package. The simple example given below generated the above figure. For more control structures and configurations check out the package documentation.

% Add the packages
\usepackage{algorithm}
\usepackage{algpseudocode}

% Insert the algorithm
\begin{algorithm}
\caption{Compute sum of integers in array}
\label{array-sum}
\begin{algorithmic}[1]
\Procedure{ArraySum}{$A$}
    \State $sum = 0$
    \For {each integer $i$ in $A$}
        \State $sum = sum + i$
    \EndFor
    \State Return $sum$
\EndProcedure
\end{algorithmic}
\end{algorithm}

Tried with: MikTeX 2.9 on Windows 7 64-bit