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





 \item Some text goes here . . .
   \caption{Merge Sort}
      \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]$
        \For{$j = 1$ to ${n_2}$}
            \State $R[i] = A[q + j]$
        \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$
                \State $A[k] = - \infty$ \Comment{We mark the duplicates with the largest negative integer}
                \State $j = j + 1$


My result enter image description here


  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.

  • I want to get the exact style as on the picture. For example, how can i write "Input" and "Output" below the function. In other words i want to see the Latex code that will mimic the style, design and formatting of the pseudocode illustrated on the picture.
    – user_777
    Oct 5, 2014 at 2:51
  • @user_777: How about, as a starting point, show us some effort from your end via a minimal working example (MWE). Start by looking into the algorithm2e package.
    – Werner
    Oct 5, 2014 at 4:48
  • @Werner Updated my post and showed my efforts...
    – user_777
    Oct 6, 2014 at 8:23

1 Answer 1


Here it is:

enter image description here




    \underline{function Euclid} $(a,b)$\;
    \Input{Two nonnegative integers $a$ and $b$}
        return $a$\;
        return Euclid$(b,a\mod b)$\;
    \caption{Euclid's algorithm for finding the greatest common divisor of two nonnegative integers}

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.