Skip to main content

The PreTeXt Guide

Section 3.31 Literate Programming

If you know what literate programming is, then you may not be surprised to learn that PreTeXt provides excellent support primarily with two additional elements: <fragment> and <fragref>.
If you have never heard of literate programming, it is a way to mix code and documentation for a computer program in a single file. But rather than typographically simple code comments, you have the full power of every other feature in PreTeXt and all of the possible output formats. Furthermore, you can arrange your code in an order that might make more sense to a human (top-down, bottom-up, a mixture, or …) and PreTeXt will rearrange the code into an order that the compiler or interpreter understands.
So the idea is that from one file you get a program for the compiler, and a beautiful, typeset explanation for a human reader in any format PreTeXt supports. An accessible introduction is Knuth’s description of his WEB system for the Pascal programming language [2], or many more resources are at the literate programming site
. Full details on the PreTeXt implementation are at Section 4.40.