# How to typeset data structures?

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.

-
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

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}

\lstset{morekeywords=[1]{#1}}}

\lstset{morekeywords=[2]{#1}}}

\lstset{morekeywords=[3]{#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:

\begin{document}

\begin{lstlisting}
procedure Euclidean(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:

\begin{document}

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

\end{document}

-
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

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:

-
Thanks, don't know why I did not think of this. – Arne Oct 12 '11 at 14:24