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 using algpseudocode for typesetting my algorithms. However, I am missing support for typesetting data structures, such as structs, classes or similar things. Can algpseudocode or some other package I can use alongside do this?

A small example of what I want to typeset:

struct {
    float x,y,z;
} Vec3f;

Something like this. It should fit in at least slightly with the visual of the algpseudocode package. The latter has only commands for imperative controls like for, while, if and generic states. But no data structure support. The syntax above is C, but I don't insist on that. Pseudocode is good enough.

share|improve this question
1  
What do you expect? Can you give a small example? –  Marco Daniel Oct 12 '11 at 12:47
    
Ok, a small example of what kind of text I want to typeset is above. –  Arne Oct 12 '11 at 12:51
add comment

2 Answers 2

up vote 5 down vote accepted

Mico's earlier answer is perfect if you want to typset C code. But if you would like something which is more similar visually with the algpseudocode package, listings can do this for you too; and you can take advantage of the literate programming substitutions provided by listings to make coding easier for you.

Preamble.

\documentclass{article}
\usepackage{listings}
\usepackage{amssymb}

\lstdefinelanguage{algpseudocode}{
  keywordstyle=[1]{\keywordstyle},
  keywordstyle=[2]{\operatorstyle},
  keywordstyle=[3]{\typestyle},
  keywordstyle=[4]{\functionstyle},
  identifierstyle={\identifierstyle},
  keywords=[1]{%
    begin,end,%
    program,procedure,function,subroutine,%
    while,do,for,to,next,repeat,until,loop,continue,endwhile,endfor,endloop,%
    if,then,else,endif,%
    return},
  literate={-}{$-$}1 {^}{$^\wedge$}1
           {>}{{$>$\ }}1 {<}{{$<$\ }}1
           {>=}{{$\geqslant$\ }}1 {<=}{{$\leqslant$\ }}1 
           {:=}{{$\gets$\ }}1 {!=}{{$\ne$\ }}1 {<>}{{$\ne$\ }}1
           {->}{{$\;\to\;$}}1
           {&&}{{\keywordstyle and\ }}4 {{||}}{{\keywordstyle or\ }}3
           {;}{\hspace{0.2em};}2 {,}{\hspace{0.2em},}2,
}

\lstset{%
  language={algpseudocode},
  columns=fullflexible,
  numbers=left,
  numberstyle=\scriptsize,
}

The above goes towards defining a "language" with syntax hilighting similar to algpseudocode. We define several types of "keywords" to be hilighted in different ways to taste (for actual language keywords, mathematical primitive operations, data types, and procedure/function names). We only define a single list of keywords; the others we will add as needed.

We describe the syntax hilighting in terms of different macros to be defined later. The rest of the special presentation of the pseudocode is taken up by a definition of how certain character combinations are to be typeset: for instance, both <> and != will by typeset as the mathematical symbol \ne. This is easily customized and will allow you to easily write code which is typeset in a literate manner.

Helper macros.

\newcommand\keywordstyle{\rmfamily\bfseries\upshape}
\newcommand\operatorstyle{\rmfamily\mdseries\upshape}
\newcommand\typestyle{\rmfamily\mdseries\upshape}
\newcommand\functionstyle{\rmfamily\mdseries\scshape}

\newcommand\identifierstyle{\rmfamily\mdseries\itshape}

\newcommand\addkeywords[1]{%
  \lstset{morekeywords=[1]{#1}}}

\newcommand\addoperators[1]{%
  \lstset{morekeywords=[2]{#1}}}

\newcommand\addtypes[1]{%
  \lstset{morekeywords=[3]{#1}}}

\newcommand\addfunctions[1]{%
  \lstset{morekeywords=[4]{#1}}}

These macros fix the style of different sorts of keywords/identifiers/what-have-you, and also allow you to easily add more identifiers. (If you wanted procedure names to be typeset specially when they are used in the code, you could define a fourth class of identifier for them, and use a similar macro to build a list of such procedure names.)

Sample documents.

1. simple pseudocode:

Sample pseudocode

\begin{document}

\addfunction{Euclid}
\addoperator{mod}

\begin{lstlisting}
procedure Euclid(a,b)
  r := a mod b
  while r != 0 do
    a := b
    b := r
    r := a mod b
  end while
  return b
end procedure 
\end{lstlisting}

\end{document}

2. some pseudo-C:

Sample pseudo-C code

\begin{document}

\addtypes{struct,int,intnode}

\begin{lstlisting}
struct {
  int val;
  struct intnode* ptr;
} intnode;
\end{lstlisting}

\end{document}
share|improve this answer
    
Also a very nice answer. I will try this as well. –  Arne Oct 12 '11 at 15:08
    
+1 for calling my answer "perfect"! :-) –  Mico Oct 12 '11 at 16:30
add comment

Have you tried the listings package? With this package, one would typeset your code as:

\documentclass{article}
\usepackage{listings}
\begin{document}
\lstset{language=C}
\begin{lstlisting}
struct {
    float x,y,z;
} Vec3f;
\end{lstlisting}
\end{document}

and obtain:

enter image description here

share|improve this answer
    
Thanks, don't know why I did not think of this. –  Arne Oct 12 '11 at 14:24
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.