# pseudo code with vertical line

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?

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}


• 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:

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

\begin{algorithm}[H]
\SetAlgoLined
\KwData{None}
\KwResult{None}
\for{i = 0, i < 10, i++}{
print i * i
\eIf{i is a prime number}
{Prime number}
{Not a prime number}
}
\end{algorithm}


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.