Documented LaTeX sources (.dtx files)

LaTeX2e, and most contributed macro packages, are now written in a literate programming style, with source and documentation in the same file. This format, known as ‘doc’, in fact originated before the days of the LaTeX project as one of the “Mainz” series of packages. A documented source file conventionally has the suffix .dtx, and will normally be ‘stripped’ before use with LaTeX; an installation file (.ins) is normally provided, to automate this process of removing comments for speed of loading. To read the comments, you can run LaTeX on the .dtx file to produce a nicely formatted version of the documented code. Several packages can be included in one .dtx file (they’re sorted out by the .ins file), with conditional sections, and there are facilities for indexes of macros, etc.

Anyone can write .dtx files; the format is explained in The LaTeX Companion, and a tutorial is available from CTAN (which comes with skeleton .dtx and .ins files).

Composition of .dtx files is supported in emacs by Matt Swift’s swiftex system: it provides a doc-tex mode which treats .dtx files rather better than AUC-TeX manages.

Another useful way of generating .dtx files is to write the documentation and the code separately, and then to combine them using the makedtx system. This technique has particular value in that the documentation file can be used separately to generate HTML output; it is often quite difficult to make LaTeX to HTML conversion tools deal with .dtx files, since they use an unusual class file.

The .dtx files are not used by LaTeX after they have been processed to produce .sty or .cls (or whatever) files. They need not be kept with the working system; however, for many packages the .dtx file is the primary source of documentation, so you may want to keep .dtx files elsewhere.

An interesting sideline to the story of .dtx files is the docmfp package, which extends the model of the doc package to Metafont and MetaPost, thus permitting documented distribution of bundles containing code for Metafont and MetaPost together with related LaTeX code.

clsguide.pdf
macros/latex/doc/clsguide.pdf
docmfp.sty
macros/latex/contrib/docmfp (gzipped tar, browse)
docstrip.tex
Part of the LaTeX distribution
DTX tutorial
info/dtxtut (gzipped tar, browse)
makedtx
support/makedtx (gzipped tar, browse)
swiftex.el
support/emacs-modes/swiftex (gzipped tar, browse)

This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=dtx