How to write algorithm like this in latex












3















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










share|improve this question





























    3















    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










    share|improve this question



























      3












      3








      3


      1






      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










      share|improve this question
















      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







      algorithms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 5 at 8:03









      Andrew

      30.6k34381




      30.6k34381










      asked Feb 5 at 7:59









      I_wil_break_wallI_wil_break_wall

      946




      946






















          2 Answers
          2






          active

          oldest

          votes


















          4














          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}$^{#}(Ssetminus T,u)$
          end{enumerate}
          end{mdframed}

          lipsum[1]

          end{document}


          gives:



          enter image description here






          share|improve this answer


























          • @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 at 16:28





















          3














          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)):}\

          noindenttextsf{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}(^#(Ssetminus 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...






          share|improve this answer


























          • Thanks for the answer. Text format seems different in the attached pic and also the dimension of the algorithm seems different.

            – Isla_Bonita
            Feb 5 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 ;-)

            – CaptainNabla
            Feb 5 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 at 9:36











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "85"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f473440%2fhow-to-write-algorithm-like-this-in-latex%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          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}$^{#}(Ssetminus T,u)$
          end{enumerate}
          end{mdframed}

          lipsum[1]

          end{document}


          gives:



          enter image description here






          share|improve this answer


























          • @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 at 16:28


















          4














          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}$^{#}(Ssetminus T,u)$
          end{enumerate}
          end{mdframed}

          lipsum[1]

          end{document}


          gives:



          enter image description here






          share|improve this answer


























          • @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 at 16:28
















          4












          4








          4







          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}$^{#}(Ssetminus T,u)$
          end{enumerate}
          end{mdframed}

          lipsum[1]

          end{document}


          gives:



          enter image description here






          share|improve this answer















          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}$^{#}(Ssetminus T,u)$
          end{enumerate}
          end{mdframed}

          lipsum[1]

          end{document}


          gives:



          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 5 at 16:35

























          answered Feb 5 at 9:36









          ZarkoZarko

          124k866163




          124k866163













          • @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 at 16:28





















          • @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 at 16:28



















          @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 at 16:28







          @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 at 16:28













          3














          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)):}\

          noindenttextsf{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}(^#(Ssetminus 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...






          share|improve this answer


























          • Thanks for the answer. Text format seems different in the attached pic and also the dimension of the algorithm seems different.

            – Isla_Bonita
            Feb 5 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 ;-)

            – CaptainNabla
            Feb 5 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 at 9:36
















          3














          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)):}\

          noindenttextsf{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}(^#(Ssetminus 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...






          share|improve this answer


























          • Thanks for the answer. Text format seems different in the attached pic and also the dimension of the algorithm seems different.

            – Isla_Bonita
            Feb 5 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 ;-)

            – CaptainNabla
            Feb 5 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 at 9:36














          3












          3








          3







          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)):}\

          noindenttextsf{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}(^#(Ssetminus 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...






          share|improve this answer















          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)):}\

          noindenttextsf{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}(^#(Ssetminus 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...







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 5 at 9:29

























          answered Feb 5 at 8:23









          CaptainNablaCaptainNabla

          435




          435













          • Thanks for the answer. Text format seems different in the attached pic and also the dimension of the algorithm seems different.

            – Isla_Bonita
            Feb 5 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 ;-)

            – CaptainNabla
            Feb 5 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 at 9:36



















          • Thanks for the answer. Text format seems different in the attached pic and also the dimension of the algorithm seems different.

            – Isla_Bonita
            Feb 5 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 ;-)

            – CaptainNabla
            Feb 5 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 at 9:36

















          Thanks for the answer. Text format seems different in the attached pic and also the dimension of the algorithm seems different.

          – Isla_Bonita
          Feb 5 at 8:58





          Thanks for the answer. Text format seems different in the attached pic and also the dimension of the algorithm seems different.

          – Isla_Bonita
          Feb 5 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 ;-)

          – CaptainNabla
          Feb 5 at 9:31





          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 ;-)

          – CaptainNabla
          Feb 5 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 at 9:36





          @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 at 9:36


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f473440%2fhow-to-write-algorithm-like-this-in-latex%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?