5

LaTeX formatting practice is well-established and well-supported by existing packages for three common cases relevant in computer science:

  • Source code listings
  • Established, conventional mathematical notation
  • Standard algorithmic pseudocode

I find I often want to describe things like records (a struct in C parlance) or algebraic data types in a semi-formal notation which is typeset much closer to math mode (proportional fonts, somewhat set off but well integrated with the body text) than source code (fixed-width fonts, jarringly distinct from the body text), but find that general math mode is a mess without imposing a bit more structure and convention.

For example, I might want to describe a concrete instance of some data as a few records:

foo {
    field : value
    field : value
}

bar {
    field : value
    field : value
    field : value
}

or I might want to sketch an algebraic data type like:

type t = Foo(type, type)
       | Bar(type, type, type)

Are there packages, standard features of amsmath, or even simply examples people like and conventions to follow here?

I'm looking for something that would be at home along side, for example, the algorithmic environment for formatting pseudocode and simple math mode for describing pure functions. I am imagining something half-way in between a simplified ML-like notation, and full on amsmath, which is meant to be typeset in a proportional font with high readability, not to look like fixed-width code.

I find my manual attempts are hitting an awkward midpoint where I am making a lot of choices to force raw math mode (or, e.g., the align environment) to do something reasonable, and that things like the curly braces to delimit collections may be a poor choice. I'm hoping not to have to entirely derive my own notation and macros for it from scratch, but have so far failed to find anything preexisting that feels right.

  • amsmath wasn't designed to cater for computer science, and isn't likely to do so in the near future. i hope you can find another compatible package. – barbara beeton Apr 18 '14 at 22:18
  • Might be overkill, or might be exactly what you want: Simple algebraic data types for C has source for a program that can generate the LaTeX code for a provided ADT. No idea about the quality of the code it produces, though. – Mike Renfro Apr 18 '14 at 23:18
2

This task seems well suited for the tabstackengine package. While the package can emulate, in many ways, the behavior of the align style environments in math mode, it is, by default, a text stacking package. Thus, you can use an align style syntax (though in macro, not environment form), but do so in text mode.

Note: in text mode, non-zero gaps are typically specified between columns because, unlike in math mode where relational operators generate their own spacing with respect to surrounding operands, text columns are set "as is", with leading and trailing spaces ignored.

\documentclass{article}
\usepackage{tabstackengine}
\begin{document}
\parindent 0in
\setstacktabbedgap{1ex}% default 0pt
\def\stackalignment{l}
\tabbedLongstack{
Foo &\{&\\
& field A:& value\\
& field AA:& value\\
\}
}\\
\\
\tabbedLongstack{
Longbar &\{&\\
& field B:& value\\
& field BBB:& value\\
& field BB:& value\\
\}
}\\
\\
\setstacktabulargap{1ex}% default \tabcolsep
\tabularLongstack{lcll}{
type t &=&Foo&(type, type)\\
& $|$ & Longbar&(type, type, type)
}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.