TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
up vote 6 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.



  literate={-}{$-$}1 {^}{$^\wedge$}1
           {>}{{$>$\ }}1 {<}{{$<$\ }}1
           {>=}{{$\geqslant$\ }}1 {<=}{{$\leqslant$\ }}1 
           {:=}{{$\gets$\ }}1 {!=}{{$\ne$\ }}1 {<>}{{$\ne$\ }}1
           {&&}{{\keywordstyle and\ }}4 {{||}}{{\keywordstyle or\ }}3
           {;}{\hspace{0.2em};}2 {,}{\hspace{0.2em},}2,


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.







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



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 


2. some pseudo-C:

Sample pseudo-C code



struct {
  int val;
  struct intnode* ptr;
} intnode;

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

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

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

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

Your Answer


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.