How to typeset data structures?

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


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}

\addfunctions{Euclidean}
\addoperators{mod}

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

Sample pseudo-C code

\begin{document}

\addtypes{struct,int,intnode}

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

\end{document}

Tags:

Algorithms