[Tex/LaTex] Package to use for documenting a struct

packagespseudocode

There exists a number of pseudo-code packages for LaTeX. Can any of these describe things like a linked-list node in C:

struct Node
   {
   Node* parent;
// other stuff
   };

or do I have to invent some custom macros

Best Answer

One possible approach with algorithm2e. I defined a "program-like" block MyStruct that begins with the keyword Struct, then typesets the structure name, then outputs keyword contains, and the end of the structure is denoted with keyword end:

\SetKwProg{MyStruct}{Struct}{ contains}{end}

All of this can be customized to your heart's content.

Usage within an algorithm is something like:

\MyStruct{Node}{
  Node* parent\;
  int size\;
  \tcp{other stuff}
}

Here's a complete simple example and output:

\documentclass{article}
\usepackage{algorithm2e}
\SetAlgoLined
\SetKwProg{MyStruct}{Struct}{ contains}{end}

\begin{document}
\begin{algorithm}[H]
\MyStruct{Node}{
  Node* parent\;
  int size\;
  \tcp{other stuff}
}
\end{algorithm}
\end{document}

enter image description here

This definition respects the global package options and also local switches, so you could use for example [noend,noline] as package options to disable typesetting of the end keyword(s) and the vertical line delimiting the block:

\documentclass{article}
\usepackage[noend,noline]{algorithm2e}
\SetAlgoLined
\SetKwProg{MyStruct}{Struct}{ contains}{end}

\begin{document}
\begin{algorithm}[H]
\MyStruct{Node}{
  Node* parent\;
  int size\;
  \tcp{other stuff}
}
\end{algorithm}
\end{document}

enter image description here