# How to write algorithm in Latex

Tags:

Categories:

Updated:

While writing any research article in Latex, so often we demonstrate an algorithm according to our proposed model, workflow or architecture.

It can be done so easily using the algorithm or algorithm2e package in article documentclass. Some demo codes including the loops, functions and comments are given below.

## Code using algorithm package

\documentclass[a4paper]{article}

\usepackage[margin=1.5in]{geometry}    % For margin alignment
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage{arevmath}     % For math symbols
\usepackage[noend]{algpseudocode}

\title{Algorithm template}
\author{Roy}

\date{\today}    % Today's date

\begin{document}
\maketitle
\section{Demo code}

\begin{algorithm}
\begin{algorithmic}

\Procedure{Roy}{$a,b$}       \Comment{This is a test}
\State System Initialization
\If{$condition = True$}
\State Do this
\If{$Condition \geq 1$}
\State Do that
\ElsIf{$Condition \neq 5$}
\State Do another
\State Do that as well
\Else
\State Do otherwise
\EndIf
\EndIf

\While{$something \not= 0$}  \Comment{put some comments here}
\State $var1 \leftarrow var2$  \Comment{another comment}
\State $var3 \leftarrow var4$
\EndWhile  \label{roy's loop}
\EndProcedure

\end{algorithmic}
\end{algorithm}

\end{document}


You will find the code in Overleaf.

The output will look like the following-

## Code using algorithm2e package

\documentclass{article}
\usepackage{xcolor}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}

\title{Another algorithm template}
\author{Roy}

%%% Coloring the comment as blue
\newcommand\mycommfont{\footnotesize\ttfamily\textcolor{blue}{#1}}

\SetKwInput{KwInput}{Input}                % Set the Input
\SetKwInput{KwOutput}{Output}              % set the Output

\begin{document}
\maketitle

\begin{algorithm}[!ht]
\DontPrintSemicolon

\KwData{Testing set $x$}
$\sum_{i=1}^{\infty} := 0$ \tcp*{this is a comment}
\tcc{Now this is an if...else conditional loop}
\If{Condition 1}
{
Do something    \tcp*{this is another comment}
\If{sub-Condition}
{Do a lot}
}
\ElseIf{Condition 2}
{
Do Otherwise \;
\tcc{Now this is a for loop}
\For{sequence}
{
loop instructions
}
}
\Else
{
Do the rest
}

\tcc{Now this is a While loop}
\While{Condition}
{
Do something\;
}

\caption{Example code}
\end{algorithm}

\end{document}


You will find the code in Overleaf.

The output will look like the following-

## Write Function within Algorithm

\begin{algorithm}[H]
\SetKwInput{KwInput}{Input}                % Set the Input
\SetKwInput{KwOutput}{Output}              % set the Output
\DontPrintSemicolon

\KwData{Testing set $x$}

% Set Function Names
\SetKwFunction{FMain}{Main}
\SetKwFunction{FSum}{Sum}
\SetKwFunction{FSub}{Sub}

% Write Function with word Function''
\SetKwProg{Fn}{Function}{:}{}
\Fn{\FSum{$first$, $second$}}{
a = first\;
b = second\;
sum = first + second\;
\KwRet sum\;
}
\;

% Write Function with word Def''
\SetKwProg{Fn}{Def}{:}{}
\Fn{\FSub{$first$, $second$}}{
a = first\;
b = second\;
sum = first - second\;
\KwRet sum\;
}
\;

\SetKwProg{Fn}{Function}{:}{\KwRet}
\Fn{\FMain}{
a = 5\;
b = 10\;
Sum(5, 10)\;
Sub(5, 10)\;
print Sum, Sub\;
\KwRet 0\;
}
\end{algorithm}


You will find the code in Overleaf.

The output will look like the following-