[Tex/LaTex] Listings, Literate Programming, and Beautiful Typography

listingspseudocodetypography

Now that I'm procrastinating with tweaking LaTeX, instead of finishing my Ph.D. thesis, I'm wondering what's the most beautiful way to typeset source code (C#, C++, LISP, XML, BNF) in LaTeX. Currently, I'm using the listings package, and the literate option, to search&replace some characters with mathematical equivalent symbols, such as:

\lstset{ %
  ...
  literate= {+}{{$+$}}1 {*}{{$*$}}1 {=}{{$\gets$}}1 
            {<=}{{$\leq$}}1 {>=}{{$\geq$}}1 {!=}{{$\neq$}}1 
            {==}{{$\equiv$}}1 {=>}{{$\leadsto$}}1
}

Although one can find some inspiration for symbols in CWEB, I'm wondering if there exists any standard symbology for common programming (and domain specific) languages. Or maybe more appropriate packages (btw, I'm not looking for fancy syntax coloring, which would ruin my two-colored document).


Since there was some users concerned if this should be regarded as a good practice, I think this question deserves some further explanation. Overall, I have to both agree and disagree them (and I think a lot of the literate programming community too). Here's when:

  1. I absolutely would agree in cases one is writing a book on the language, a tutorial, a recipe, patterns, something where it is intended the reader to try that code on the computer. This can provoke confusion on the reader (where is that <- sign?), and hinder their reading pattern (you know, when you glance at hundreds of lines of code very quickly).
  2. But in thesis, papers, and other scientific publications, most of the snippets are illustrative; almost as if they were pseudo-code. Readability is improved, especially because lots of two/three character symbols (which can vary depending on the language) are compressed to a more symbolic notation. Now, it is true that most of you will suggest: "then why don't you you pseudo-code?" Well… First, because the pseudo-code provided by the algorithmic environment is nice for simplifying imperative languages, but it fails for things like LISP, XML and BNF. Second, because it takes a lot of time converting them, and it quickly falls out of sync.

Still, I maintain my original question: "examples of beautiful typography of source code".

Best Answer

When typesetting C code in non-monospaced fonts, I've certainly found the "literate programming" options useful.

What I've used them for is specifying typography for symbol pairs that aren't very readable in the standard non-monospaced font -- not replacing them with a different symbol, but adjusting to make the same symbols more readable. For instance, "--" doesn't look right in Times, so I've used literate={--}{{\texttt{--}} to make that look better.

Meanwhile, when one's using illustrative pseudocode in discussions of algorithms that are detached from details of programming language syntax, I do think it's useful to use non-programming symbols. For one thing, it helps make it clear that what you are writing is not compilable C code -- even if it has a C-like syntax, and is really just C with irrelevant bits removed. (Or Lisp, or whatever.) And, since the "irrelevant bits" are going to vary from discussion to discussion, I haven't found that one specific pseudocode format applies to everything.

Related Question