I want to write some pseudo code. After few research I found algorithmicx with this post: Write pseudo code in latex

But I want vertical line as this:

function toto: 
| for i = 0 , i < 10, i++ 
|  | print i * i = i^2 
|  | if i is a prime number 
|  |  | print prime number 
|  | end if 
|  end for 
end function

According to the documentation the package does not provide this (or I missed it)

Does someone know how to do this?


2 Answers 2


No package needed except fontenc for setting things like < properly. Parameters \lindent, \rthk, and \rindent control the space to the left of the rule, the rule thickness, and the space to the right of the rule, respectively.

After several failed attempts to allow special characters, I decided that for pseudo code, you might be able to survive without the use of special characters. The macro I introduce is \block{pre\\code}{indented\\code}{post\\code}.

EDITED to \ignorespaces and \unskip, so that % are not needed everywhere.

function toto:\\
  for i = 0 , i < 10, i++
    if i is a prime number
      print prime number\\
      then do something else
    end if
    some intermediate stuff\\
    if i is a superprime number
      print superprime number\\
      then really do something amazing
    end if
  end for
finish up\\
end function

enter image description here

  • 1
    The good thingss with your solution is the possibility to have a lot of kind of exotic loop or name if you want to change it. Really interesting Apr 23, 2014 at 13:39

As jubobs said algorithm2e package can do it:


\for{i = 0, i < 10, i++}{
    print i * i
    \eIf{i is a prime number}
        {Prime number}
        {Not a prime number}

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.