Which package to use for writing algorithms?

I am looking for a package that will allow me to write pseudocode of algorithms. The pseudocode may include mathematical symbols, like \forall, \in and subscripts. But I would like also to "talk" about variables contained in the pseudocode in my text, so be able to say "In figure ... variable foobar corresponds .." and make the word foobar have the same appearance as in the pseudocode example.

I found that with the listings package I can add mathematical symbols by using: \begin{lstlisting}[mathescape]. The problem with this package is that I don't know how to make just one word have the same appearance as the words appearing inside \begin{lstlisting} ... \end{lstlisting}.

Any ideas how to do this? Or perhaps of another package that will help solve the above mentioned problem?

-
You may want to look at the packages algorithm2e and algorithmic – egreg Jul 21 '12 at 9:57
Not really pseudo-code, but I like the old fashioned Nassi–Shneiderman diagrams. See struktex – knut Jul 21 '12 at 14:32
@egreg Thank you for answering. By passing the documentations of the two packages you suggest I see how I can add math symbols to my code. But I cannot see how to solve the second problem: "make just one word have the same appearance as the words appearing". Any ideas for this? – foobar Jul 21 '12 at 14:58
@equality Do you mean a new keyword? – egreg Jul 21 '12 at 15:26
The inline equivalent of \begin{lstlisting}\end{lstlisting} is \lstinline{} – knittl Jul 22 '12 at 8:39

The lstlisting environment is part of the “listings” package.

Additionally to providing an environment for listings (\begin{lstlisting}) and a command to import code from files (\lstinputlisting), it also provides a command to highlight code inline: \lstinline.

Be aware, that \lstinline has some limitations though (and does not obay all options from lstset)

-

Using any one of algorithm2e, algorithmic or algorithmicx would allow you to typeset pseudocode with your own definitions for variables and keywords. Here is a small example using algpseudocode (provided by algorithmicx):

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\newcommand{\var}[1]{{\ttfamily#1}}% variable
\begin{document}
\begin{algorithm}[t]
\caption{Euclid’s algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$ and \var{foobar}\label{foobar}
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
In Algorithm~\ref{euclid}, variable \var{foobar} (in line~\ref{foobar}), corresponds to\ldots
\end{document}


Since the contents of the algorithmic environment is treated as-is, formatting inside of it is left up to the user to a great extend. Consequently, "variable formatting" can easily be done using a macro that is typeset equivalently inside/outside algorithmic. The above example defines \var{<variable>} that sets its contents in typewriter font.

-
thanks for your answer [+1]:). As I was already using the listings package, I accepted the answer below – foobar Jul 23 '12 at 17:46