# Write pseudo algorithm in LaTeX

I am trying to write pseudocode in my paper. Here are the snippet and the image like what I want. Can someone please help me to format it?

\begin{algorithm}[H]
\DontPrintSemicolon
$D_{obs}{(f,Nx,Ny,x,y)\leftarrow D_{obs}(t,Nx,Ny,x,y) \text{by 1-D forward FFT}}}$

$D_{0}\leftarrow D_{obs}$

$for f \leftarrow 1,2,...,F$

$do$

$for f \leftarrow 1,2,...,F$

$do$

${{D^f}_n} \leftarrow a_{n}{{D^f}_{obs}} + (1 - a_{n})Sf_{d}{D^f}_{n-1}+ (1 - S})f_{d}{D^f}_{n-1}$

$\Vert{{D^f}_n} - {D^f}_{n-1}{\Vert^2}_F \leq \varepsilon$

$\text{then return {D^f}_n}$

$\text{then return {D^f}_{n_{max}}}$

$\text{then return {D_{recovered}}}$

${D_{recovered}}(t,Nx,Ny,x,y)\leftarrow {D_{recovered}}(f,Nx,Ny,x,y) \text {by 1-D inerse FFT}$

\caption{Example code}
\end{algorithm}


When I run my Latex code I get this code :

It should be like this:

Currently, it looks messed up.

• How about something like this? Oct 23 '19 at 5:53
• I would simply like to write the algorithm above in my paper. I have tried several times from the code that I submitted but I cannot do it. Oct 23 '19 at 6:09

The problem here is that you do not use the features of your algorithm package. In order to get something like what you want, you have to use a package like algorithm2e and use its command to have the indentation and the keywords displayed right.

An example of use would be this one, rendering something close to what you expect and resembling what was proposed in a comment:

\documentclass{article}

\usepackage{amsmath}
\usepackage[ruled]{algorithm2e}

\begin{document}

\begin{algorithm}[H]
\DontPrintSemicolon
$\mathbf{D}_{obs}(f,Nx,Ny,x,y)\leftarrow \mathbf{D}_{obs}(t,Nx,Ny,x,y)$ by 1-D forward FFT\;
$\mathbf{D}_0\leftarrow\mathbf{D}_{obs}$\;
\For{$f\leftarrow 1,2,\dots,F$}{
\For{$f\leftarrow 1,2,\dots,F$}{
$\mathbf{D}^f_n \leftarrow a_n\mathbf{D}^f_{obs} + (1 - a_n)\mathcal{SF}_{d}\mathbf{D}^f_{n-1} + (1 - \mathcal{S})\mathcal{F}_{d}\mathbf{D}^f_{n-1}$\;
\If{$\left\|\mathbf{D}^f_n - \mathbf{D}^f_{n-1}\right\|_F^2\leq\epsilon$}{
\textbf{return} $\mathbf{D}^f_n$\;
}
\textbf{return} $\mathbf{D}^f_{n_{max}}$\;
}
\textbf{return} $\mathbf{D}_{recovered}$\;
}
$\mathbf{D}_{recovered}(t,Nx,Ny,x,y)\leftarrow \mathbf{D}_{recovered}(f,Nx,Ny,x,y)$ by 1-D inverse FFT\;

\caption{Example code}
\end{algorithm}

\end{document}


If you want to change the style with which the keywords are displayed, the way the rules on the left are written, the fact that the line numbers are displayed or not, I redirect you to the algorithm2e documentation.

• Thanks you very much dear KersouMan. Oct 23 '19 at 7:05