TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
I don't think you can do it with algorithmicx, but you can with algorithm2e. – Jubobs Apr 23 '14 at 8:55
I can use algorithm2e instead of algorithmicx so it's perfect – Romain Picot Apr 23 '14 at 8:58
As jubobs said algorithm2e package can do it: ctan.org/tex-archive/macros/latex/contrib/algorithm2e – Romain Picot Apr 23 '14 at 9:17
up vote 9 down vote accepted

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

share|improve this answer
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 – Romain Picot Apr 23 '14 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}
share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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