# How can I typeset algorithms with LaTeX?

I want to write an algorithm using the Latex algorithm generator. Can anyone help me in this case? A sample is given below.

Any sort of help would be highly appreciated.

• Welcome to TeX.SX! What have you tried so far? Please provide a minimal working example (MWE) of your current attempt for us to work on. Commented Jun 12, 2021 at 7:25

You can use the package

\usepackage[options]{algorithm2e}

for instance

Then write your algorithm like

\IncMargin{1em}
\begin{algorithm}[htbp]
\SetKwData{Left}{left}
\SetKwData{This}{this}
\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}
\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
\SetKwComment{comment}{\#}{}
\Input{data}
\Output{result}
\BlankLine

\textbf{STEP 1:}  Load data \\
\textbf{STEP 2:}  Perform task \\

\For{$i$ in range (Number of task)}{
calculate $\Delta$A  = abs( a - b ) \\
\comment{a is something, b is another thing}
\If{ $\Delta$A  $\leq$ threshold}
{do  $\Delta$A  + c  }

}

\caption{simple algorithm}
\end{algorithm}][1]][1]

you should then obtain something like:

For more details do check the documentation, such as :

https://www.ctan.org/pkg/algorithm2e

or

https://www.overleaf.com/learn/latex/Algorithms