2

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

2
  • An algorithm package or just typesetting the code? (listings, for example, for the second case)
    – user31729
    Commented Feb 16, 2015 at 12:12
  • ChristianHupfer: The former, otherwise I would have tagged the question differently.
    – user877329
    Commented Feb 16, 2015 at 12:15

1 Answer 1

2

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

2
  • Shouldn't I define a keyword pointer so it becomes less C specific?
    – user877329
    Commented Feb 17, 2015 at 13:02
  • You can define whatever you want. You listed almost zero requirements, so as I said, this is just one possible approach. Commented Feb 17, 2015 at 13:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .