2

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.

enter image description here

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

2 Answers 2

3

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:

enter image description here

1
  • @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 \\ ).
    – Zarko
    Feb 5, 2019 at 16:28
2

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 enter image description here

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...

3
  • 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).
    – Zarko
    Feb 5, 2019 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.