# How to write algorithm in Latex

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}[1]

\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[1]{\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-

If you find this post helpful, and want to support this blog, you can or

You can find a comprehensive list of Latex resources in the following post:

Latex Resources in a Nutshell

If you are a new Latex user, check out this post: 20 Most Common Mistakes Made by New Latex Users

You can find all Latex oriented posts of mine in: https://shantoroy.com/categories/#latex

Some of those are included here.

## Promotions and Referrals (US Residents Only)

• Chime: Open a Checking account at Chime using my referral link and get $100 after your employer deposit paycheck of minimum$200 within the first 45 days.
• Rakuten: Get $30 after you spend$30 at Rakuten select stores after you use my referral link to open an account.
• Chase Freedom Credit Card: Earn $200 cash back with Chase Freedom Unlimited or Chase Freedom Flex credit card. I can be rewarded if you apply using my referral link and are approved for the card. • Chase Checking Account: Get$200 when you open a checking account using my referral link after your first salary is deposited.
• Discover: Earn $50 cash back with Discover when you apply using my referral link and are approved for the card. • Amex Blue Cash Preferred: Earn$250 as statement credit when you spend \$3000 in first six months. Apply using my referral link.

Tags:

Categories:

Updated: