Literate MAGMA Programming

Magma-TeX is a collection of TEX / LaTEX macros to support "literate MAGMA programming".

A literate programming style encourages good documentation by integrating program code with comments in such a way that the comments become the primary focus. The file can be processed with a typesetting system such as TEX or LaTEX to produce printable output and the code can be extracted and passed to the relevant compiler or interpreter. Even better, in some cases, the file itself can be passed to the compiler or interpreter.

MAGMA is a programming language for computational algebra, and to quote the MAGMA home page:

"MAGMA is a large, well-supported software package designed to solve computationally hard problems in algebra, number theory, geometry and combinatorics. It provides a mathematically rigorous environment for computing with algebraic, number-theoretic, combinatoric and geometric objects."

Magma-TeX was inspired by lambdaTeX, Patryk Zadarnowski's collection of plain TEX macros for processing literate Haskell scripts.

Files that use Magma-TeX can be processed with TEX or LaTEX to produce printable output and the code can be extracted and loaded into MAGMA. The executable code is recovered by taking only the lines beginning with ">", and deleting the first character of each of those lines.

Magma-TeX takes a fairly minimalist position in the world of literate programming. There is no attempt to emulate the much more elaborate WEB or CWEB systems of Knuth. In particular, there are no facilities to rearrange the order of code fragments.

Installing the files

Download either the zip file or the tar/gzip file. The archive contains four files:

To generate the TEX and LaTEX macros, run latex on MagmaTeX.ins. This creates the files MagmaTeX.tex and MagmaTeX.sty. They should be moved to wherever it is you keep TEX and LaTEX macro files on your system. To complete the installation you probably need to rebuild the TEX file names database, either by running mktexlsr from a command prompt or some other maintenance utility.

To produce the documentation, run latex on MagmaTeX.dtx.

The file DGraphs.lm is an example of a literate MAGMA script.

Using the macros

If you are using LaTEX and want line numbers printed in the margin next to the MAGMA code, include the line
in the preamble of your document.

If you would like to see the MAGMA prompt character ( > ) printed at the left of each line, use the prompts package option:

By default, an identifier given as the string name of a Greek letter will be typeset as a symbol rather than as a word. To turn off this behaviour, use the nogreek package option.

If you are using plain TEX, put the line

\input MagmaTeX
near the top of the document.

Write your code using the usual MAGMA syntax with no additional markup except for > at the beginning of each line. When you process the file with tex or latex, the program text will be typeset with appropriate syntax highlighting. The only thing you need to know is that the characters <, >, and " are active. That is, they represent TEX macros and do not have their usual meanings. (But their usual meanings are restored in math mode.)

There are a few other macros defined in the package that might be useful but probably will be rarely used — check the documentation for the details.

Using the code

Currently [July, 2010], the MAGMA system does not support the direct loading of literate scripts and so a preprocessor is needed to extract the program code. The Python program provided with this distribution does this. The command
python script.lm
creates a file script.m containing the MAGMA code. Any lines at the start of the file beginning with two percent characters will be converted to MAGMA comments and transferred to the output file. (See DGraphs.lm for an example.)

Don Taylor 19 July, 2010