Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
1  
I don't think you can do it with algorithmicx, but you can with algorithm2e. –  Jubobs Apr 23 at 8:55
    
I can use algorithm2e instead of algorithmicx so it's perfect –  Romain Picot Apr 23 at 8:58

2 Answers 2

up vote 6 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.

\documentclass{article}
\usepackage[T1]{fontenc}
\def\lindent{.5ex}
\def\rindent{2ex}
\def\rthk{1.5pt}
\newcommand\block[3]{\unskip%
  \noindent\parbox[b]{\textwidth}{\strut\ignorespaces#1\strut}\\
  \setbox0=\hbox{\parbox[b]{\textwidth}{\strut\ignorespaces#2\strut}}%
  \hspace*{\lindent}\rule[-.5\dp\strutbox]{\rthk}{\ht0}\hspace{\rindent}\box0\\%
  \parbox[b]{\textwidth}{\strut\ignorespaces#3\strut}%
\ignorespaces}
\begin{document}
\block{
function toto:\\
setup
}{
  \block{
  for i = 0 , i < 10, i++
  }{
    \block{
    if i is a prime number
    }{
      print prime number\\
      then do something else
    }{
    end if
    }\\
    some intermediate stuff\\
    \block{
    if i is a superprime number
    }{
      print superprime number\\
      then really do something amazing
    }{
    end if
    }
  }{
  end for
  }
}{
finish up\\
end function
}
\end{document}

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 at 13:39

As jubobs said algorithm2e package can do it:

http://www.ctan.org/tex-archive/macros/latex/contrib/algorithm2e

share|improve this answer

Your Answer

 
discard

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.