TransWikia.com

How to format a pseudocode algorithm

TeX - LaTeX Asked by user_777 on May 15, 2021

I would like to know how to format a pseudocode algorithm like the one shown in the picture below. I would like to see an example of Tex/Latex code that would mimic the style, formatting and design of the pseudocode illustrated on this picture. I know how to write simple pseudocode algorithms, but i don’t know how to

  1. Align the pseudocode with an item “Some text..”
  2. How to write a pseudocode with Input and Output exactly below the procedure/function so that they are not numbered and aligned with procedure/function
  3. How to use the block braces in form of “vertical lines”

enter image description here

My attempt

documentclass[a4paper]{article}

usepackage[english]{babel}
usepackage[utf8]{inputenc}
usepackage{amsmath}
usepackage{amsfonts}
usepackage{graphicx}
usepackage[colorinlistoftodos]{todonotes}
usepackage{algorithm}
usepackage{algpseudocode}

usepackage{geometry}
 geometry{
 a4paper,
 total={210mm,297mm},
 left=20mm,
 right=20mm,
 top=20mm,
 bottom=20mm,
 }

begin{document}
begin{enumerate}

 item Some text goes here . . .
  begin{algorithm}
   caption{Merge Sort}
    begin{algorithmic}[1]
      Function{Merge}{$A,p,q,r$}Comment{Where A - array, p - left, q - middle, r - right}

        State ${n_1} = q - p + 1$
        State ${n_2} = r - q$
        State Let $L[1 ldots {n_1} + 1]$ and $R[1 ldots {n_2} + 1]$ be new arrays
        For{$i = 1$ to ${n_1}$}
            State $L[i] = A[p + i - 1]$
        EndFor
        For{$j = 1$ to ${n_2}$}
            State $R[i] = A[q + j]$
        EndFor
        State $L[{n_1} + 1] =  infty $
        State $R[{n_2} + 1] =  infty $
        State $i = 1$
        State $j = 1$
        For{$k = p$ to $r$}
            If {$L[i] < R[j]$}
                State $A[k] = L[i]$
                State $i = i + 1$
            ElsIf {$L[i] > R[j]$}
                State $A[k] = R[j]$
                State $j = j + 1$
            Else
                State $A[k] = - infty$ Comment{We mark the duplicates with the largest negative integer}
                State $j = j + 1$
            EndIf
        EndFor
       EndFunction

end{algorithmic}
end{algorithm}
end{enumerate}
end{document}

My result
enter image description here

Comments

  1. As you can see i don’t know how to align the algorithm with the itemized text.
  2. I don’t know how to place Input and Output words below the function, so that they are not numbered and aligned with a function.
  3. I like more the style of vertical line block, rather than just condition:end.

I’m new to writing pseudocode algorithms with Latex, but i suspect the style and formatting i’m looking for is in the package algorithm2e. Can someone show me how to achieve the following result:

enter image description here

I want to learn to write pseudocode algorithm in the same style as in the picture above.

One Answer

Here it is:

enter image description here

Code:

documentclass{article}
usepackage{amsmath}
usepackage[linesnumbered,ruled]{algorithm2e}

begin{document}
begin{algorithm}
    SetKwInOut{Input}{Input}
    SetKwInOut{Output}{Output}

    underline{function Euclid} $(a,b)$;
    Input{Two nonnegative integers $a$ and $b$}
    Output{$gcd(a,b)$}
    eIf{$b=0$}
      {
        return $a$;
      }
      {
        return Euclid$(b,amod b)$;
      }
    caption{Euclid's algorithm for finding the greatest common divisor of two nonnegative integers}
end{algorithm}
end{document} 

Correct answer by karlkoeller on May 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP