Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Inserting an algorithm in LaTeX

📅 2012-Jun-29 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ algorithm, latex ⬩ 📚 Archive

A simple algorithm in LaTeX

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


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧