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 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?

share|improve this question
3  
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? –  equality Jul 21 '12 at 14:58
    
@equality Do you mean a new keyword? –  egreg Jul 21 '12 at 15:26
1  
The inline equivalent of \begin{lstlisting}\end{lstlisting} is \lstinline{} –  knittl Jul 22 '12 at 8:39
show 4 more comments

2 Answers

up vote 3 down vote accepted

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)

share|improve this answer
add comment

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

enter image description here

\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.

share|improve this answer
    
thanks for your answer [+1]:). As I was already using the listings package, I accepted the answer below –  equality Jul 23 '12 at 17:46
add comment

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.