latex
Written by Daniel Herber on March 23, 2017.
The code for AlgorithmStyle is available at [link](blogs/latex/post_5/AlgorithmStyle.tex).
---
### The Style
Writing algorithms or pseudocode has a large amount of stylistic freedom. I have developed the following style for the [algorithm2e](https://www.ctan.org/pkg/algorithm2e) package based on my personal tastes. Hopefully this post will serve as a good starting point for your own style.
To use the style, simply include ```AlgorithmStyle.tex``` in your preamble (or copy the source code):
```tex
\input{AlgorithmStyle}
```
The source code for the style is:
```tex
% local packages
\usepackage{algorithm2e}
\usepackage{xcolor}
%----------------------------------
% custom style commands
%----------------------------------
% variable style command
\newcommand{\xvar}[1]{\textsf{#1}}
% horizontal alignment command
\newcommand{\xvbox}[2]{\makebox[#1][l]{#2}}
%----------------------------------
% set algorithm2e styles
%----------------------------------
% change algorithm font size
\SetAlFnt{\footnotesize}
% change algorithm caption style
\newcommand{\xAlCapSty}[1]{\small\sffamily\bfseries\MakeUppercase{#1}}
\SetAlCapSty{xAlCapSty}
% comment style (algorithms)
\newcommand{\xCommentSty}[1]{\scriptsize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{xCommentSty}
% change line number style
\newcommand\mynlfont[1]{\scriptsize\sffamily{#1}}
\SetNlSty{mynlfont}{}{}
% add the line numbers
\LinesNumbered
% comments right justified
\SetSideCommentRight
% don't print semicolon
\DontPrintSemicolon
% ruled algorithm
\RestyleAlgo{algoruled}
```
This style can be combined with my previous post on [variable width algorithms](http://www.danielherber.com/latex.php?option=post_4).
---
### Example
To illustrate this style, we will use one of the algorithms from [link](http://systemdesign.illinois.edu/publications/Her16b.pdf).
PDF: [example](blogs/latex/post_5/post_5_example.pdf)
PNG:
[](blogs/latex/post_5/algorithm.png){data-lightbox="blog_imgs" data-title="PNG"}
PNG with annotations:
[](blogs/latex/post_5/algorithm_annotations.png){data-lightbox="blog_imgs" data-title="PNG with annotations"}
Code for the example:
```tex
\documentclass{article}
% custom algorithm style
\input{AlgorithmStyle}
\begin{document}
% algorithm code (for this example)
\begin{algorithm}
% functions
\SetKwFunction{cumprod}{cumprod}
\SetKwFunction{length}{length}
\SetKwFunction{zeros}{zeros}
\SetKwFunction{ceil}{ceil}
% input/ouput names
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
% caption
\caption{Creation of edge set for a specific perfect matching number.\label{alg:singlepm}}
\Input{%
\xvbox{2mm}{$\xvar{N}$} -- number of vertices (should be even) \\
\xvbox{2mm}{$\xvar{I}$} -- perfect matching number, integer between 1 and $(\xvar{N}-1)!!$
}
\Output{%
\xvbox{2mm}{$\xvar{E}$} -- vector of edges in sequential pairs
}
\BlankLine % blank line for spacing
% start of the pseudocode
\xvbox{2mm}{$\xvar{J}$} $\leftarrow$ $[ 1,3,5,\dots,\xvar{N}-1 ]$ \tcc*{odd numbers from 1 to N-1}
\xvbox{2mm}{$\xvar{P}$} $\leftarrow$ $[ 1,\cumprod(\xvar{J}) ]$ \tcc*{cumulative double factorial}
\xvbox{2mm}{$\xvar{V}$} $\leftarrow$ $[ 1,2,\dots,\xvar{N} ]$ \tcc*{create initial list of available vertices}
\For{$\xvar{j}\leftarrow$ $\xvar{J}$ }{
\xvbox{1mm}{$\xvar{q}$} $\leftarrow$ $(\xvar{N}+1-\xvar{j})/2$ \tcc*{index for 2nd to last entry in P}
\xvbox{1mm}{$\xvar{I}$} $\leftarrow$ $\ceil\big(\xvar{I}/\xvar{P}(\xvar{q})\big)$ \tcc*{calculate smaller vertex index}
$\xvar{E}(\xvar{j})$ $\leftarrow$ $\xvar{V}(\xvar{end})$ \tcc*{assign largest remaining value}
remove element $\xvar{V}(\xvar{end})$ \tcc*{remove largest remaining value}
$\xvar{E}(\xvar{j}+1)$ $\leftarrow$ $\xvar{V}(\xvar{i})$ \tcc*{assign smaller selected value}
remove element $\xvar{V}(\xvar{i})$ \tcc*{remove the smaller selected value}
\xvbox{1mm}{$\xvar{I}$} $\leftarrow$ $\xvar{I} - \big( (\xvar{i} - 1) \times \xvar{P} (\xvar{q}) \big)$ \tcc*{subtract to get index in subgraph with 2 vertices removed}
} % end for j
\end{algorithm}
\end{document}
```