# How to write algorithm like this in latex

I want to write an algorithm in a latex. There are various ways to write, but I want to write in the way given below. I have tried but I am not able to do anything similar to the given in the image.

see my code:

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}

Algorithm \\

Input :

Output : \\

1. $T \leftarrow$ an set of size $S$ \\
2. \textbf{if} $x = 0$\\

3. \hspace{1cm} \textbf{return}

\end{document}


Question : Some of the problems, I am facing are the color of the background, line numbering, spacing etc

based on nice @CaptainNabla answer:

\documentclass{article}
\usepackage{geometry}
\usepackage{enumitem}
\usepackage[framemethod=tikz]{mdframed}

\usepackage{lipsum}
\begin{document}

\begin{mdframed}[hidealllines=true,backgroundcolor=gray!20]
\parindent=0pt
\parskip=2pt  % <-- gives more spaces between paragraphs

\underline{\textsc{ALLSUBSETSUMS}$^\#(S,u)$:}

\medskip      % for additional vertical space
\textsf{INPUT:} A set $S$ of $n$ positive integers and an upper bound integer $u$.

\textsf{OUTPUT:} The set of all subset sums with cardinality information of $S$ up to $u$.
\begin{enumerate}[nosep]
\item \textbf{if} $S=\{x\}$
\item \hspace{1cm} \textbf{return} $\{(0,0),(x,1)\}$
\item $T \leftarrow$ an arbitrary subset of $S$ of size $[n/2]$
\item \textbf{return} \textsc{ALLSUBSETSUMS}$^{\#}(T,u)\bigoplus_u$\textsc{ALLSUBSETSUMS}$^{\#}(S\setminus T,u)$
\end{enumerate}
\end{mdframed}

\lipsum[1]

\end{document}


gives:

• @Isla_Bonita, which lines? use of \\  outside of formatting tables is not good practice, for additional space is better to use \smallskip or \medskip or \vspace{<amounnt>} etc. i slightly change answer now (eliminate the use of \\ ). Feb 5, 2019 at 16:28

The background color can be adjusted as stated here. Then its just a matter of playing around with some fonts:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[framemethod=tikz]{mdframed}

\begin{document}

\begin{mdframed}[hidealllines=true,backgroundcolor=gray!20]
\noindent
\underline{\textsc{AllSubsetSums}$$^\#(S,u)$$:}\\

\noindent\textsf{INPUT:} A set $$S$$ of $$n$$ positive integers and an upper bound integer $$u$$.\\
\textsf{OUTPUT:} The set of all subset sums with cardinality information of $$S$$ up to $$u$$.

\begin{enumerate}
\item \textbf{if} $$S=\{x\}$$
\item \hspace{1cm} \textbf{return} $$\{(0,0),(x,1)\}$$
\item $$T \leftarrow$$ an arbitrary subset of $$S$$ of size $$[n/2]$$
\item \textbf{return} \textsc{AllSubsetSums}$$^\#(T,u)\oplus_u$$\textsc{AllSubsetSums}$$^\#(S\setminus T,u)$$
\end{enumerate}
\end{mdframed}

\end{document}


which gives

Its not perfect and requires still a lot of fiddling, especially if you have a lot of different algorithms. Then it might be better to redefine the paragraph command...

• Thanks for the answer. Text format seems different in the attached pic and also the dimension of the algorithm seems different.
– user132638
Feb 5, 2019 at 8:58
• I agree it is not perfect. I edited a little bit to be more closer to the original, though I am sure there is still room for improvement ;-) Feb 5, 2019 at 9:31
• @Isla_Bonita, the format is the same as you in your mwe (minimal working example) in question (which in turn not provide the size of of images which you show). Feb 5, 2019 at 9:36