<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">%%% ====================================================================
%%%  @LaTeX-file{
%%%     author          = "Scott Pakin",
%%%     version         = "1.0",
%%%     date            = "30 April 2002",
%%%     time            = "19:38:20 MDT",
%%%     filename        = "dtxtut.tex",
%%%     checksum        = "45026 1711 6221 57709",
%%%     email           = "pakin@uiuc.edu (Internet)",
%%%     codetable       = "ISO/ASCII",
%%%     keywords        = "LaTeX, packages, .ins files, .dtx files,
%%%                        literate programming",
%%%     supported       = "yes",
%%%     abstract        = "This tutorial is intended for advanced
%%%                        LaTeX 2e users who want to learn how to
%%%                        create .ins and .dtx files for distributing
%%%                        their homebrewed classes and style files.",
%%%     docstring       = "The checksum field above contains a CRC-16
%%%                        checksum as the first value, followed by
%%%                        the equivalent of the standard UNIX wc
%%%                        (word count) utility output of lines,
%%%                        words, and characters.  This is produced by
%%%                        Robert Solovay's checksum utility.  This file
%%%                        header was produced with the help of Nelson
%%%                        Beebe's filehdr utility.  Both checksum and
%%%                        filehdr are available from CTAN
%%%                        (http://www.ctan.org)."
%%%
%%%  }
%%% ====================================================================

\documentclass[11pt]{ltxguide}
\usepackage{varioref}
\usepackage{makeidx}

\title{How to Package Your \LaTeX{} Package}
\author{Scott Pakin \texttt{&lt;pakin@uiuc.edu&gt;}}
\date{30 April 2002}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Define a term.
\newcommand{\defn}[2][X]{%
  \ifx#1X
    \emph{#2}\index{#2}%
  \else
    \emph{#2}\index{#1}%
  \fi
}

% Make a version of \dots that allows a subsequent line break.
\newcommand{\dotsbrk}{\dots\linebreak[0]}

% If we have url.sty loaded, use that for formatting URLs.
\ifx\url\undefined
\else
  \let\URL=\url
\fi

% Define formatting for "doc" and "docstrip".
\newcommand{\DOC}{\textsf{Doc}\index{Doc@\textsf{Doc}}}
\newcommand{\ds}{\textsf{DocStrip}\index{DocStrip@\textsf{DocStrip}}}

% Customize varioref's messages.
\def\reftextbefore{on the preceding page}
\def\reftextfacebefore{on the preceding page}
\def\reftextcurrent{above}   % We promise not to make any forward references.

% Define some macros to help automate indexing.
\makeindex
{\catcode`\|=0 \catcode`\\=12
 |gdef|bslash{\}}
\newcommand{\defmacro}[1]{%                   % Define a macro.
  \texttt{\bslash#1}%
  \index{#1@\texttt{\string\bslash{}#1}}%
}
\newcommand{\usemacro}[1]{%                   % Use a macro.
  \texttt{\bslash#1}%
  \index{#1@\texttt{\string\bslash{}#1}}%
}
\newcommand{\indexmacro}[1]{%                 % Index a macro (no display).
  \index{#1@\texttt{\string\bslash{}#1}}%
}
{\catcode`\|=12                               % Same as \indexmacro, but
  \gdef\indexmacroBegin#1{%                   % for ranges of text
    \index{#1@\texttt{\string\bslash{}#1}|(}%
  }
  \gdef\indexmacroEnd#1{%
    \index{#1@\texttt{\string\bslash{}#1}|)}%
  }
}
\newcommand{\defthing}[1]{%                   % Define a non-macro thing.
  \texttt{#1}%
  \index{#1@\texttt{#1}}%
}
\newcommand{\usething}[1]{%                   % Use a non-macro thing.
  \texttt{#1}%
  \index{#1@\texttt{#1}}%
}
\newcommand{\indexthing}[1]{%                 % Index a non-macro thing.
  \index{#1@\texttt{#1}}%
}
{\catcode`\|=12                               % Same as \indexthing, but
  \gdef\indexthingBegin#1{%                   % for ranges of text
    \index{#1@\texttt{#1}|(}%
  }
  \gdef\indexthingEnd#1{%
    \index{#1@\texttt{#1}|)}%
  }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\sloppy
\maketitle

\begin{abstract}
  This tutorial is intended for advanced \LaTeXe{} users who want to
  learn how to create |.ins|\index{installer file} and
  |.dtx|\index{documented LaTeX file@documented \LaTeX{} file} files
  for distributing their homebrewed classes and style files.
\end{abstract}


\section{Introduction}

\paragraph{Requirements}
We assume that you already know how to \emph{program} in \LaTeX\@.
That is, you should know how to use \usemacro{newcommand},
\usemacro{newenvironment}, and preferably a smidgen of \TeX\@.  You
should also be familiar with ``\clsguide'', which is available from
CTAN (\URL{http://www.ctan.org}) and comes with most \LaTeXe{}
distributions.  Finally, you should know how to install
packages\index{package} that are shipped as a |.dtx|\index{documented
LaTeX file@documented \LaTeX{} file} file plus a
|.ins|\index{installer file} file.

\index{package|(}
\paragraph{Terminology}
A \defn{style file} (|.sty|) is primarily a collection of macro and
environment definitions.  One or more style files (\eg a main style
file that \usemacro{input}s or \usemacro{RequirePackage}s multiple
helper files) is called a \defn{package}.  Packages are loaded into a
document with \usemacro{usepackage}|{&lt;main .sty file&gt;}|\index{style
file}.  In the rest of this document, we use the notation
``\m{package}'' to represent the name of your package.
\index{package|)}

\paragraph{Motivation}
The important parts of a package\index{package} are the code, the
documentation of the code, and the user documentation.  Using the
\DOC{} and \ds{} programs, it's possible to combine all three of these
into a single, \defn[documented LaTeX file@documented \string\LaTeX{}
file]{documented \LaTeX} (|.dtx|) file.  The primary advantage of a
|.dtx| file is that it enables you to use arbitrary \LaTeX{}
constructs to comment\index{comments} your code.  Hence, macros,
environments, code stanzas, variables, and so forth can be explained
using tables, figures, mathematics, and font changes.  Code can be
organized into sections using \LaTeX's sectioning commands.
\index{indexing|(}
\DOC{} even facilitates generating a unified index that indexes both
macro definitions (in the \LaTeX{} code) and macro descriptions (in
the user documentation).
\index{indexing|)}
This emphasis on writing verbose, nicely typeset
comments\index{comments} for code---essentially treating a program as
a book that describes a set of algorithms---is known as \defn{literate
programming}~\cite{Knuth1984:literate} and has been in use since the
early days of \TeX.

This tutorial will teach you how to write basic |.dtx| files and the
|.ins| files that manipulate them.  Although there is much overlap
with chapter~14 of \emph{The \LaTeX{}
Companion}~\cite{Goossens1994:companion}, this document is structured
as a step-by-step tutorial, while \emph{The \LaTeX{} Companion} is
more reference-like.  Furthermore, this tutorial shows how to write a
single file that serves as both documentation and driver file, which
is a more typical usage of the \DOC{} system than using separate
files.


\index{installer file|(}

\section{The \texttt{.ins} file}
\label{sec:ins-file}

Before writing any code, the first step is to write an
\defn[installer file]{installer} (|.ins|) file.  An installer
file extracts the code from a |.dtx| file, strips off the
comments\index{comments} and documentation, and outputs a
|.sty|\index{style file} file.  The good news is that a |.ins| file is
fairly short and doesn't change significantly from one
package\index{package} to another.

\index{license|(}
|.ins| files usually start with comments\index{comments} specifying
the copyright\index{copyright} and license information:

\begin{verbatim}
    %%
    %% Copyright (C) &lt;year&gt; by &lt;your name&gt;
    %%
    %% This file may be distributed and/or modified under the
    %% conditions of the LaTeX Project Public License, either
    %% version 1.2 of this license or (at your option) any later
    %% version.  The latest version of this license is in:
    %%
    %%    http://www.latex-project.org/lppl.txt
    %%
    %% and version 1.2 or later is part of all distributions of
    %% LaTeX version 1999/12/01 or later.
    %%
\end{verbatim}

\noindent
\index{package|(}
The \LaTeX{} Project Public License\index{LaTeX Project Public
License@\LaTeX{} Project Public License} (LPPL) is the license under
which most packages and \LaTeX{} itself are distributed.  Of course,
you can release your package under any license you want; the LPPL is
merely the most common license for
\LaTeX{} packages.  The LPPL specifies that a user can do whatever he
wants with your package---including sell it and give you nothing in
return.  The only restrictions are that he must give you credit for
your work, and he must change the name of the package if he modifies
anything.
\index{package|)}
\index{license|)}
\index{LPPL|see{\LaTeX{} Project Public License}}

The next step is to load \ds:

\begin{verbatim}
    \input docstrip.tex
\end{verbatim}


\indexmacroBegin{keepsilent} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{decl}
  \defmacro{keepsilent}
\end{decl}

By default, \ds{} gives a line-by-line account of its activity.  These
messages aren't terribly useful, so most people turn them off:

\begin{verbatim}
    \keepsilent
\end{verbatim}

\indexmacroEnd{keepsilent}   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{usedir} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{decl}
  \defmacro{usedir} \arg{directory}
\end{decl}

A system administrator can specify the base directory under which all
\TeX-related files should be installed, \eg |/usr/share/texmf|.  (See
``\usemacro{BaseDirectory}'' in the \ds{} manual.)  The |.ins| file
specifies where its files should be installed relative to that.  The
following is typical:

\begin{verbatim}
    \usedir{tex/latex/&lt;package&gt;}
\end{verbatim}

\indexmacroEnd{usedir}   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{preamble} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\indexmacroBegin{endpreamble}

\begin{decl}
  \defmacro{preamble}    \\
  \m{text}               \\
  \defmacro{endpreamble}
\end{decl}

\index{comments|(}
The next step is to specify a \defn{preamble}, which is a block of
commentary that will be written to the top of every
generated file:

\begin{verbatim}
    \preamble

    This is a generated file.

    Copyright (C) &lt;year&gt; by &lt;your name&gt;

    This file may be distributed and/or modified under the
    conditions of the LaTeX Project Public License, either
    version 1.2 of this license or (at your option) any later
    version.  The latest version of this license is in:

       http://www.latex-project.org/lppl.txt

    and version 1.2 or later is part of all distributions of
    LaTeX version 1999/12/01 or later.

    \endpreamble
\end{verbatim}

\index{comments|)}

\noindent
The preceding preamble would cause |&lt;package&gt;.sty|\index{style file}
to begin as follows:

\begin{verbatim}
    %%
    %% This is file `&lt;package&gt;.sty',
    %% generated with the docstrip utility.
    %%
    %% The original source files were:
    %%
    %% &lt;package&gt;.dtx  (with options: `package')
    %%
    %% This is a generated file.
    %%
    %% Copyright (C) &lt;year&gt; by &lt;your name&gt;
    %%
    %% This file may be distributed and/or modified under the
    %% conditions of the LaTeX Project Public License, either
    %% version 1.2 of this license or (at your option) any later
    %% version.  The latest version of this license is in:
    %%
    %%    http://www.latex-project.org/lppl.txt
    %%
    %% and version 1.2 or later is part of all distributions of
    %% LaTeX version 1999/12/01 or later.
    %%
\end{verbatim}

\indexmacroEnd{endpreamble}
\indexmacroEnd{preamble}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{generate} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\indexmacroBegin{file}
\indexmacroBegin{from}

\begin{decl}
  \defmacro{generate}
    |{|\defmacro{file} |{&lt;style-file&gt;}|
    |{|\defmacro{from} |{&lt;dtx-file&gt;}| |{&lt;tag&gt;}}}|
\end{decl}

We now reach the most important part of a |.ins| file: the
specification of what files to generate from the |.dtx| file.  The
following tells \ds{} to generate |&lt;package&gt;.sty|\index{style file}
from |&lt;package&gt;.dtx| by extracting only those parts marked as
``|package|'' in the |.dtx| file.  (Marking parts of a |.dtx| file is
described in Section~\ref{sec:dtx-file}.)

\label{code:generate}
\begin{verbatim}
    \generate{\file{&lt;package&gt;.sty}{\from{&lt;package&gt;.dtx}{package}}}
\end{verbatim}%
\index{style file}

\noindent
\usemacro{generate} can extract any number of files from a given |.dtx|
file.  It can even extract a single file from multiple |.dtx| files.
See the \ds{} manual for details.

\indexmacroEnd{from}
\indexmacroEnd{file}
\indexmacroEnd{generate}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{Msg} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{decl}
  \defmacro{Msg} \arg{text}
\end{decl}

The next part of a |.ins| file consists of commands to output a
message to the user, telling him what files need to be installed and
reminding him how to produce the user documentation.  The following
set of \usemacro{Msg} commands is typical:

\begin{verbatim}
    \Msg{*********************************************************}
    \Msg{*}
    \Msg{* To finish the installation you have to move the}
    \Msg{* following file into a directory searched by TeX:}
    \Msg{*}
    \Msg{* \space\space &lt;package&gt;.sty}
    \Msg{*}
    \Msg{* To produce the documentation run the file &lt;package&gt;.dtx}
    \Msg{* through LaTeX.}
    \Msg{*}
    \Msg{* Happy TeXing!}
    \Msg{*********************************************************}
\end{verbatim}%
\index{style file}%
\indexmacro{space}

\indexmacroEnd{Msg}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{endbatchfile} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{decl}
  \usemacro{endbatchfile}
\end{decl}

Finally, we tell \ds{} that we've reached the end of the |.ins| file:

\begin{verbatim}
    \endbatchfile
\end{verbatim}

\indexmacroEnd{endbatchfile}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Appendix~\ref{sec:skeleton-ins-file} lists a complete, skeleton |.ins|
file.

\index{installer file|)}
\index{documented LaTeX file@documented \LaTeX{} file|(}


\section{The \texttt{.dtx} file}
\label{sec:dtx-file}

\index{package|(}
A |.dtx| file contains both the commented source code and the user
documentation for the package.  \ds{} normally extracts the source
code proper---no user documentation and no
comments\index{comments}---into the package's |.sty|\index{style file}
file(s).  Running a |.dtx| file through |latex| typesets the user
documentation, which usually also includes a nicely typeset version of
the commented\index{comments} source code.
\index{package|)}

Due to some \DOC{} trickery, a |.dtx| file is actually evaluated
\emph{twice}.  The first time, only a small piece of \LaTeX{} driver
code is evaluated.  The second time, \emph{comments}\index{comments}
in the |.dtx| file are evaluated, as if there were no ``|%|''
preceding them.  This can lead to a good deal of confusion when
writing |.dtx| files and occasionally leads to some awkward
constructions.  Fortunately, once the basic structure of a |.dtx| file
is in place, filling in the code is fairly straightforward.


\subsection{Prologue}

\index{comments|(}

|.dtx| files usually begin with a copyright\index{copyright} and
license\index{license} comment:

\begin{verbatim}
    % \iffalse meta-comment
    %
    % Copyright (C) &lt;year&gt; by &lt;your name&gt;
    %
    % This file may be distributed and/or modified under the
    % conditions of the LaTeX Project Public License, either
    % version 1.2 of this license or (at your option) any later
    % version.  The latest version of this license is in:
    %
    %    http://www.latex-project.org/lppl.txt
    %
    % and version 1.2 or later is part of all distributions of
    % LaTeX version 1999/12/01 or later.
    %
    % \fi
\end{verbatim}

\index{comments|)}

\noindent
The \usemacro{iffalse} and |\fi| are needed because the second time
the |.dtx| file is processed, |%| characters at the beginning of lines
are ignored.  To prevent the
copyright\index{copyright}/license\index{license} from being evaluated
as \LaTeX{} code, we have to surround it with
\usemacro{iffalse}\dotsbrk|\fi|.  Adding ``\usething{meta-comment}''
after ``\usemacro{iffalse}'' is nothing more than a convention for
indicating that the comment\index{comments} is intended to be read by
a human, not by \DOC{}, \ds{}, or \LaTeX.

\indexmacroBegin{NeedsTeXFormat} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\indexmacroBegin{ProvidesPackage}

\begin{decl}
  \defmacro{NeedsTeXFormat}  \arg{format-name} \oarg{release-date} \\
  \defmacro{ProvidesPackage} \arg{package-name} \oarg{release-info}
\end{decl}

The next few lines are also surrounded by
\usemacro{iffalse}\dotsbrk|\fi| so as not to be processed by |latex|
on the second pass through the |.dtx| file.  However, these lines are
intended not for a human reader, but for \ds{} (hence, no
``\usething{meta-comment}''):

\label{code:ProvidesPackage}
\begin{verbatim}
    % \iffalse
    %&lt;&lt;package&gt;&gt;\NeedsTeXFormat{LaTeX2e}[1999/12/01]
    %&lt;&lt;package&gt;&gt;\ProvidesPackage{&lt;package&gt;}
    %&lt;&lt;package&gt;&gt;   [&lt;YYYY&gt;/&lt;MM&gt;/&lt;DD&gt; v&lt;version&gt; &lt;description&gt;]
    %
\end{verbatim}%
\indexmacro{NeedsTeXFormat}%
\indexmacro{ProvidesPackage}

\noindent
(We'll encounter the |\fi| shortly.)

Remember the \usemacro{generate} line in the |.ins| file
(page~\pageref{code:generate})?  It ended with the tag ``|package|''.
This tells \ds{} to write lines that begin with ``|%&lt;&lt;package&gt;&gt;|'' to
the |.sty|\index{style file} file, stripping off the
``|%&lt;&lt;package&gt;&gt;|'' in the process.  Hence, our |.sty|\index{style
file} file will begin with the following:

\begin{verbatim}
    \NeedsTeXFormat{LaTeX2e}[1999/12/01]
    \ProvidesPackage{&lt;package&gt;}
       [&lt;YYYY&gt;/&lt;MM&gt;/&lt;DD&gt; v&lt;version&gt; &lt;description&gt;]
\end{verbatim}%
\indexmacro{NeedsTeXFormat}%
\indexmacro{ProvidesPackage}

\noindent
For example:

\begin{verbatim}
    \NeedsTeXFormat{LaTeX2e}[1999/12/01]
    \ProvidesPackage{skeleton}
       [2002/03/25 v1.0 .dtx skeleton file]
\end{verbatim}%
\label{code:ProvidesPackage-example}%
\indexmacro{NeedsTeXFormat}%
\indexmacro{ProvidesPackage}

\noindent
\index{package|(}
The \usemacro{NeedsTeXFormat} line ensures that the package won't run
under a version of \LaTeXe{} older than what the package was tested
with.  The date and version strings in the \usemacro{ProvidesPackage}
line are used by \DOC{} to set the \usemacro{filedate} and
\usemacro{fileversion} macros.  Note the date\index{date format} format;
\emph{YYYY/MM/DD} is used throughout \LaTeXe{} and should be used in
your packages, as well.
\index{package|)}

\indexmacroEnd{ProvidesPackage}
\indexmacroEnd{NeedsTeXFormat}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{EnableCrossrefs} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\indexmacroBegin{CodelineIndex}
\indexmacroBegin{RecordChanges}
\indexmacroBegin{DocInput}

\begin{decl}
  \defmacro{EnableCrossrefs} \\
  \defmacro{CodelineIndex}   \\
  \defmacro{RecordChanges}   \\
  \defmacro{DocInput} \arg{filename}   \label{desc:CodelineIndex}
\end{decl}

Next comes the only part of the |.dtx| file that isn't commented out
(\ie doesn't begin each line with |%|):

\begin{verbatim}
    %&lt;&lt;*driver&gt;&gt;
    \documentclass{ltxdoc}
    \usepackage{&lt;package&gt;}
    \EnableCrossrefs
    \CodelineIndex
    \RecordChanges
    \begin{document}
      \DocInput{&lt;package&gt;.dtx}
    \end{document}
    %&lt;&lt;/driver&gt;&gt;
    % \fi
\end{verbatim}

The preceding code stanza is what |latex| evaluates on its first pass
through the |.dtx| file.  We'll now examine that stanza line-by-line:

\begin{enumerate}
  \item Putting code between ``|%&lt;&lt;*driver&gt;&gt;|'' and ``|%&lt;&lt;/driver&gt;&gt;|''
  is a shorthand for prefixing each line with ``|%&lt;&lt;driver&gt;&gt;|''.  This
  demarcates the \DOC{} driver\index{driver code} code.

  \item The \usemacro{documentclass} should almost always be
  \usething{ltxdoc}, as that loads \DOC{} and provides a few useful
  macros for formatting program documentation.

\index{package|(}
  \item You should always \usemacro{usepackage} your
  package.  If you don't, \DOC{} won't see the
  package's \usemacro{ProvidesPackage} line and won't know how to set
  \usemacro{filedate} and \usemacro{fileversion} (see
  page~\pageref{code:GetFileInfo}).  This is also where you should
  \usemacro{usepackage} any other packages you need to typeset the
  user documentation.
\index{package|)}

  \item \usemacro{EnableCrossrefs} tells \DOC{} that you want it to
  construct an index for your code---a good idea.  The alternative is
  \usemacro{DisableCrossrefs}, which speeds up processing by a
  negligible amount.

  \item \usemacro{CodelineIndex} tells \DOC{} that the index should
  refer to line numbers instead of page numbers.  (The alternative is
  \usemacro{PageIndex}.)  \usemacro{CodelineIndex} makes index entries
  easier to find at the expense of making the index less consistent
  (because descriptions of macros and environments are always indexed
  by page number).

  \item \label{item:RecordChanges} On page~\pageref{code:changes},
  we'll see how to log the changes made in each revision of the
  package\index{package}.  \usemacro{RecordChanges} tells \DOC{} that
  it should keep aggregate the log entries.

  \item There should be only one command between the
  |\begin{document}| and |\end{document}|: a \usemacro{DocInput} call
  with which the |.dtx| file inputs itself.  This enables a master
  file to \usemacro{DocInput} multiple files in order to produce a
  single document that covers more than one package\index{package} but
  contains a unified index\index{indexing}.  Master documentation
  files are described~\vpageref{item:master-files}.
\end{enumerate}

\indexmacroEnd{DocInput}
\indexmacroEnd{RecordChanges}
\indexmacroEnd{CodelineIndex}
\indexmacroEnd{EnableCrossrefs} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{OnlyDescription} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{decl}
  \defmacro{OnlyDescription} \label{desc:OnlyDescription}
\end{decl}

Another command that sometimes appears in the preamble (\ie before the
|\begin{document}|) is \usemacro{OnlyDescription}, which tells \DOC{}
to typeset only the user documentation, not the package\index{package}
code/comments\index{comments}.  It's usually best to omit
\usemacro{OnlyDescription} (or add it commented out).  A user can
always add it manually or even enable \usemacro{OnlyDescription} for
\emph{all} |.dtx| files by adding the following to his
\defthing{ltxdoc.cfg} file:

\begin{verbatim}
     \AtBeginDocument{\OnlyDescription}
\end{verbatim}%
\indexmacro{AtBeginDocument}


\indexmacroEnd{OnlyDescription}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{CheckSum} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\index{checksumming|(}

\begin{decl}
  \defmacro{CheckSum} \arg{number} \label{decl:CheckSum}
\end{decl}

\DOC{} supports a very simplistic form of document checksumming, to
help ensure that a package\index{package} didn't get corrupted in
transport.  \DOC{} merely counts the number of backslashes that occur
in the code.  If the number matches the checksum, \DOC{} gives a
success message:

\begin{verbatim}
    *******************
    * Checksum passed *
    *******************
\end{verbatim}

\noindent
Otherwise, it says what the correct checksum should be:

\begin{verbatim}
    ! Package doc Error: Checksum not passed (&lt;incorrect&gt;&lt;&lt;&gt;&gt;&lt;correct&gt;).
\end{verbatim}%
\indexthing{Checksum not passed}

\noindent
To specify the checksum in a |.dtx| file, merely add a \usemacro{CheckSum}
statement:

\begin{verbatim}
    % \CheckSum{&lt;number&gt;}
\end{verbatim}

\noindent
If \m{number} is~|0|, or if the |.dtx| file lacks a \usemacro{CheckSum} line
entirely, then \DOC{} outputs the following warning message:

\begin{verbatim}
    **********************************
    * This macro file has no checksum!
    * The checksum should be &lt;number&gt;!
    **********************************
\end{verbatim}

\noindent
During code development, it is convenient to specify |\CheckSum{0}|,
so you don't receive an error message every time you run |latex|.  But
don't forget to replace ``|0|'' with the correct number before
releasing your package\index{package}!

\index{checksumming|)}
\indexmacroEnd{CheckSum}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{CharacterTable} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{decl}
  \defmacro{CharacterTable} \arg{text}
\end{decl}

The second mechanism that \DOC{} uses to ensure that a |.dtx| file is
uncorrupted is a character table.  If you put the following command
verbatim into your |.dtx| file, then \DOC{} will ensure that no
unexpected character translation took place in transport:\footnote{The
character table is commonly prefixed with double percent signs so that
it gets written to the \texttt{.sty}\index{style file} file.  This
seems unnecessary and is therefore shown here with single percent
signs.}

\begin{verbatim}
    % \CharacterTable
    %  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
    %   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
    %   Digits        \0\1\2\3\4\5\6\7\8\9
    %   Exclamation   \!     Double quote  \"     Hash (number) \#
    %   Dollar        \$     Percent       \%     Ampersand     \&amp;
    %   Acute accent  \'     Left paren    \(     Right paren   \)
    %   Asterisk      \*     Plus          \+     Comma         \,
    %   Minus         \-     Point         \.     Solidus       \/
    %   Colon         \:     Semicolon     \;     Less than     \&lt;&lt;
    %   Equals        \=     Greater than  \&gt;&gt;     Question mark \?
    %   Commercial at \@     Left bracket  \[     Backslash     \\
    %   Right bracket \]     Circumflex    \^     Underscore    \_
    %   Grave accent  \`     Left brace    \{     Vertical bar  \|
    %   Right brace   \}     Tilde         \~}
\end{verbatim}

\noindent
A success message looks like this:

\begin{verbatim}
    ***************************
    * Character table correct *
    ***************************
\end{verbatim}

\noindent
and an error message looks like this:

\begin{verbatim}
    ! Package doc Error: Character table corrupted.
\end{verbatim}%
\indexthing{Character table corrupted}

\indexmacroEnd{CharacterTable}     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{changes} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{decl}
  \defmacro{changes} \arg{version} \arg{date} \arg{description}
    \label{code:changes}
\end{decl}

\index{package|(}
On page~\pageref{item:RecordChanges}, we learned that \DOC{} has a
mechanism for recording changes to the package.  The command is
``|\changes{&lt;version&gt;}{&lt;date&gt;}{&lt;description&gt;}|'', and it's common to
use \usemacro{changes} for the initial version of the package, to log the
package's creation date:
\index{package|)}

\begin{verbatim}
    % \changes{v1.0}{2002/03/25}{Initial version}
\end{verbatim}

One nice feature of the \usemacro{changes} command is that it knows
whether it was used internally to a macro/environment definition.  As
Figure~\ref{fig:change-history} shows, top-level changes are prefixed
with ``General:'', and internal changes are prefixed with the name of
the enclosing macro or environment.

\begin{figure}[htbp]
  \centering
  \index{change history}
  \fbox{%
    \begin{minipage}[t]{0.75\textwidth}
      {\normalfont\Large\bfseries Change History\vskip 2.3ex plus 0.2ex}
      v1.0 \\
      \hspace*{2em} General: Top-level comment \dotfill 1 \\
      v1.2j \\
      \hspace*{2em} \texttt{myMacro}: Internal macro comment \dotfill 5
    \end{minipage}%
  }%
  \caption{Sample change history}
  \label{fig:change-history}
\end{figure}

\indexmacroEnd{changes}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{GetFileInfo} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\indexmacroBegin{filedate}
\indexmacroBegin{fileversion}
\indexmacroBegin{fileinfo}

\begin{decl}
  \defmacro{GetFileInfo} \arg{style-file}   \label{code:GetFileInfo} \\[1ex]
  \defmacro{filedate}    \\
  \defmacro{fileversion} \\
  \defmacro{fileinfo}
\end{decl}

Next, we tell \DOC{} to parse the \usemacro{ProvidesPackage} command
(page~\pageref{code:ProvidesPackage}), calling the pieces,
respectively, ``\usemacro{filedate}'', ``\usemacro{fileversion}'', and
``\usemacro{fileinfo}'':

\begin{verbatim}
    % \GetFileInfo{&lt;package&gt;.sty}
\end{verbatim}%
\index{style file}%
\indexmacro{GetFileInfo}

\noindent
For instance, the \usemacro{ProvidesPackage} example
shown~\vpageref{code:ProvidesPackage-example} would be parsed as
follows:

|    |%
\begin{tabular}{@{}l@{\qquad$\equiv$\qquad}l@{}}
  \usemacro{filedate}    &amp; 2002/03/25         \\
  \usemacro{fileversion} &amp; v1.0               \\
  \usemacro{fileinfo}    &amp; .dtx skeleton file
\end{tabular}

\indexmacroEnd{fileinfo}
\indexmacroEnd{fileversion}
\indexmacroEnd{filedate}
\indexmacroEnd{GetFileInfo} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{DoNotIndex} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\index{indexing|(}

\begin{decl}
  \defmacro{DoNotIndex} \arg{macro-name \textnormal{, \dots}}
\end{decl}

When producing an index, \DOC{} normally indexes \emph{every} control
sequence (\ie backslashed word or symbol) in the code.  The problem
with this level of automation is that many control sequences are
uninteresting from the perspective of understanding the code.  For
example, a reader probably doesn't want to see every location where
|\if| is used---or |\the| or |\let| or |\begin| or numerous other
control sequences.

As its name implies, the \usemacro{DoNotIndex} command gives \DOC{} a
list of control\index{control sequences} sequences that should not be
indexed.  \usemacro{DoNotIndex} can be used any number of times, and
it accepts any number of control\index{control sequences} sequence
names:

\begin{verbatim}
    % \DoNotIndex{\#,\$,\%,\&amp;,\@,\\,\{,\},\^,\_,\~,\ }
    % \DoNotIndex{\@ne}
    % \DoNotIndex{\advance,\begingroup,\catcode,\closein}
    % \DoNotIndex{\closeout,\day,\def,\edef,\else,\empty,\endgroup}
\end{verbatim}
\centerline{$\vdots$}

\index{indexing|)}
\indexmacroEnd{DoNotIndex}     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\subsection{User documentation}

We can finally start writing the user documentation.  A typical
beginning looks like this:

\begin{verbatim}
    % \title{The \textsf{&lt;package&gt;} package\thanks{This document
    %   corresponds to \textsf{&lt;package&gt;}~\fileversion,
    %   dated~\filedate.}}
    % \author{&lt;your name&gt; \\ \texttt{&lt;your e-mail address&gt;}}
    %
    % \maketitle
\end{verbatim}%
\indexmacro{title}
\indexmacro{author}
\indexmacro{maketitle}

\noindent
\index{package|(}
The title can certainly be more creative, but note that it's common
for package names to be typeset with \usemacro{textsf} and for
\usemacro{thanks} to be used to specify the package version and date.
This yields one of the advantages of literate\index{literate
programming} programming: Whenever you change the package version (the
optional second argument to
\usemacro{ProvidesPackage}), the user documentation is updated
accordingly.  Of course, you still have to ensure manually that the
user documentation accurately describes the updated package.
\index{package|)}

Write the user documentation as you would any \LaTeX{} document, except
that you have to precede each line with a ``|%|''.  Note that the
\usething{ltxdoc} document class is derived from |article|, so the top-level
sectioning command is \usemacro{section}, not \usemacro{chapter}.


\indexmacroBegin{DescribeMacro} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\indexmacroBegin{DescribeEnv}

\begin{decl}
  \defmacro{DescribeMacro} \arg{macro} \\
  \defmacro{DescribeEnv} \arg{environment}
\end{decl}

\DOC{} provides a couple of commands to help format user
documentation.  If you include
``|\DescribeMacro{&lt;macro&gt;}|''\footnote{``\m{macro}'' should include
the backslash.} within a paragraph, \DOC{} will stick ``\m{macro}'' in
the margin to make it easy for a reader to see.  \DOC{} will also add
\m{macro} to the index and format the corresponding page number to
indicate that this is where the macro is described (as opposed to the
place in the source code where the macro is defined).

\usemacro{DescribeEnv} is the analogous command for describing an environment.
Both \usemacro{DescribeMacro} and \usemacro{DescribeEnv} can be used
multiple times within a paragraph.

\indexmacroEnd{DescribeEnv}
\indexmacroEnd{DescribeMacro}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexmacroBegin{marg} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\indexmacroBegin{oarg}
\indexmacroBegin{parg}
\indexmacroBegin{meta}

\begin{decl}
  \defmacro{marg} \arg{argument} \\
  \defmacro{oarg} \arg{argument} \\
  \defmacro{parg} \arg{argument} \\
  \defmacro{meta} \arg{text}
\end{decl}

The \usething{ltxdoc} document class provides three commands to help
typeset macro and environment syntax
(Table~\ref{tbl:argument-formatting}).  \usemacro{marg} formats
mandatory arguments, \usemacro{oarg} formats optional arguments, and
\usemacro{parg} formats picture arguments.  All three of these utilize
\usemacro{meta} to typeset the argument proper.
\usemacro{meta} is also useful on its own.  For example,
``\texttt{This needs a} |\meta{dimen}|.'' is typeset as ``This needs a
\m{dimen}.''

In addition to those commands, \DOC{} facilitates the typesetting of
macro descriptions by automatically loading the |shortvrb|
package\index{package}.  That lets you use \verb+|+\dots\verb+|+ as a
convenient shorthand for |\verb|\verb+|+\dots\verb+|+.  For instance,
``\verb+|\mymacro|+ |\oarg{pos}| |\marg{width}| |\marg{text}|'' is
typeset as follows:

\begin{verbatim}
    \mymacro [&lt;pos&gt;] {&lt;width&gt;} {&lt;text&gt;}
\end{verbatim}

\noindent
Like \usemacro{verb}, the \verb+|+\dots\verb+|+ shorthand does not work within
\usemacro{footnote} or other fragile macros.

\begin{table}[htbp]
  \centering
  \caption{Argument-formatting commands}
  \label{tbl:argument-formatting}
  \begin{tabular}{@{}ll@{}}
    \hline
    \multicolumn{1}{@{}c}{Command} &amp;
    \multicolumn{1}{c@{}}{Result} \\
    \hline
    \usemacro{marg}|{text}| &amp; |{&lt;text&gt;}| \\
    \usemacro{oarg}|{text}| &amp; |[&lt;text&gt;]| \\
    \usemacro{parg}|{text}| &amp; |(&lt;text&gt;)| \\
    \hline
  \end{tabular}
\end{table}

\indexmacroEnd{meta}
\indexmacroEnd{parg}
\indexmacroEnd{oarg}
\indexmacroEnd{marg}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\subsection{Code and commentary}

\indexmacroBegin{StopEventually} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\indexmacroBegin{Finale}

\begin{decl}
  \defmacro{StopEventually} |{&lt;text&gt;}| \\
  \defmacro{Finale}
\end{decl}

The package's\index{package} source code is delineated by putting it
between \usemacro{StopEventually} and \usemacro{Finale}.  Note that
\usemacro{CheckSum} (page~\pageref{decl:CheckSum}) applies only to
the package's\index{package} source code.  \usemacro{StopEventually}
takes an argument, which is a block of text to typeset after the code.
If \usemacro{OnlyDescription} (page~\pageref{desc:OnlyDescription}) is
specified, then nothing after the \usemacro{StopEventually} will be
output---including text that follows \usemacro{Finale}.
\usemacro{StopEventually}'s \m{text} parameter is therefore the way to
provide a piece of text that should be output regardless of whether or
not a code listing is typeset.  It commonly includes a bibliography
section and/or one or more of the following commands.

\indexmacroEnd{Finale}
\indexmacroEnd{StopEventually}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{decl}
  \defmacro{PrintChanges} \\
  \defmacro{PrintIndex}
\end{decl}

\indexmacroBegin{PrintChanges} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\index{change history|(}

\usemacro{PrintChanges} produces an unnumbered section called
``Change History''.  (See Figure~\vref{fig:change-history}).  The
Change History section aggregates all of the \usemacro{changes}
commands in the |.dtx| file into a single list of per-version
modifications.  This makes it easy to keep track of what changed from
version to version.

\usemacro{PrintChanges} uses \LaTeX's glossary mechanism.  Running
|latex| on |&lt;package&gt;.dtx| produces change-history data in
|&lt;package&gt;.glo|.  To produce the actual change history
(|&lt;package&gt;.gls|), the user should run the \usething{makeindex}
program as follows:

\begin{verbatim}
    makeindex -s gglo.ist -o &lt;package&gt;.gls &lt;package&gt;.glo
\end{verbatim}

\index{change history|)}
\indexmacroEnd{PrintChanges}    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\indexmacroBegin{PrintIndex} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\index{indexing|(}

\usemacro{PrintIndex} produces an unnumbered section called ``Index''.  The
index automatically includes entries for all macros and environments
that are used, defined, or described in the document.  All
environments are additionally listed under ``environments''.
Table~\ref{tbl:index-formatting} illustrates the way that various
entries are formatted.  In that table, ``27'' refers to a page number,
and ``123'' refers to a line number.\footnote{If
\texttt{\string\CodelineIndex} (page~\pageref{desc:CodelineIndex}) is
not used, then ``123'' also refers to a page number.}  Note that
definitions and uses are included in the index only if the document
includes a code listing (\ie \usemacro{OnlyDescription} was not specified).

\begin{table}[htbp]
  \centering
  \caption{Formatting of entries in the index}
  \label{tbl:index-formatting}
  \begin{tabular}{@{}llp{6cm}@{}}
  \hline
  Item        &amp; Function  &amp; Formatting in index                            \\
  \hline
  Macro       &amp; Used      &amp; |\myMacro| \dotfill~123                        \\
  Macro       &amp; Defined   &amp; |\myMacro| \dotfill~\underline{123}            \\
  Macro       &amp; Described &amp; |\myMacro| \dotfill~\textit{27}                \\
  Environment &amp; Defined   &amp; |myEnv| (environment) \dotfill~\underline{123} \\
  Environment &amp; Described &amp; |myEnv| (environment) \dotfill~\textit{27}     \\
  \multicolumn{2}{@{}l}{Other (\ie an explicit \texttt{\string\index})}
                          &amp; myItem \dotfill~27                             \\
  \hline
  \end{tabular}
\end{table}

Running |latex| on |&lt;package&gt;.dtx| produces index data in
|&lt;package&gt;.idx|.  To produce the actual index (|&lt;package&gt;.ind|), the
user should run the \usething{makeindex} program as follows:

\begin{verbatim}
    makeindex -s gind.ist -o &lt;package&gt;.ind &lt;package&gt;.idx
\end{verbatim}

A code index is a nice ``value added'' made possible by
literate\index{literate programming} programming.  It requires
virtually no extra effort and greatly helps code maintainers to find
macro definitions and see what other macros a package\index{package}
depends upon.

\index{indexing|)}
\indexmacroEnd{PrintIndex}   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexthingBegin{macrocode} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{decl}
  |\begin{macrocode}| \\
  \m{code}            \\
  |\end{macrocode}|
\end{decl}

Code fragments listed between |\begin{macrocode}| and
|\end{macrocode}| are extracted verbatim into the |.sty|\index{style
file} file.  When typeset, the code fragments are shown with a running
line counter to make it easy to refer to a specific line.  Here are
some key points to remember about the \usething{macrocode} environment:

\begin{enumerate}
  \item There must be \emph{exactly} four spaces between the ``|%|''
  and the ``|\begin{macrocode}|'' or ``|\end{macrocode}|''.
  Otherwise, \DOC{} won't detect the end of the code
  fragment.\footnote{Trivia: Only the
  \texttt{\string\end\string{macrocode\string}} needs this precise
  spacing and then, only for typesetting the documentation.
  Nevertheless, it's good practice to use
  ``\texttt{\%\textvisiblespace\textvisiblespace\textvisiblespace\textvisiblespace}''
  for the \texttt{\string\begin\string{macrocode\string}}, as well.}

  \item The lines of code within |\begin{macrocode}|\dotsbrk|\end{macrocode}|
  should not begin with ``|%|''.  The code gets written exactly as is
  to the |.ins| file, with no |%|-stripping.
\end{enumerate}

The following is a sample code fragment.  It happens to be a complete
macro definition, but this is not necessary; any fragment of \LaTeX{}
code can appear within a \usething{macrocode} environment.

\label{code:mymacro}
\begin{verbatim}
    %    \begin{macrocode}
    \newcommand{\mymacro}{This is
      a \LaTeX{} macro.}
    %    \end{macrocode}
\end{verbatim}

\noindent
\DOC{} formats the preceding code fragment as follows:

\begin{tabbing}
|    |\={\tiny 9} \=\kill
       \&gt; {\tiny 1} \&gt; |\newcommand{\mymacro}{This is| \\
       \&gt; {\tiny 2} \&gt; |  a \LaTeX{} macro.}|
\end{tabbing}

\noindent
Note that line numbers are unique across the entire program (as
opposed to being local to a single page).  If \usemacro{PrintIndex} is used,
the index will automatically include entries for |\newcommand|,
|\mymacro|, and |\LaTeX|, unless any of these are \usemacro{DoNotIndex}'ed.

\indexthingEnd{macrocode}   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\indexthingBegin{macro} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\indexthingBegin{environment}

\begin{decl}
  |\begin{macro}{&lt;macro&gt;}| \\
  \qquad $\vdots$ \\
  |\end{macro}| \\
  \\
  |\begin{environment}{&lt;environment&gt;}| \\
  \qquad $\vdots$ \\
  |\end{environment}| \\
\end{decl}

The \usething{macro} and \usething{environment} environments are used
to delineate a complete macro or environment definition.
\usething{macro}/\usething{environment} generally contain one or more
\usething{macrocode} environments interspersed with code
documentation.  The following is a more complete version of the
\usething{macrocode} example shown~\vpageref{code:mymacro}.

\begin{verbatim}
    % \begin{macro}{\mymacro}
    % We define a trivial macro, |\mymacro|, to illustrate
    % the use of the |macro| environment.
    %    \begin{macrocode}
    \newcommand{\mymacro}{This is
      a \LaTeX{} macro.}
    %    \end{macrocode}
    % \end{macro}
\end{verbatim}

\noindent
The typeset version is shown below:

\begin{tabbing}
|    \mymacro|\qquad\={\tiny 9} \= \kill
|    \mymacro| \&gt; We define a trivial macro, |\mymacro|, to illustrate the \\
               \&gt; use of the \usething{macro} environment. \\
               \&gt; {\tiny 1} \&gt; |\newcommand{\mymacro}{This is| \\
               \&gt; {\tiny 2} \&gt; |  a \LaTeX{} macro.}|
\end{tabbing}

\DOC{} typesets the macro/environment name in the margin for
increased visibility.  \DOC{} also adds the appropriate entries to the
index.  (See Table~\vref{tbl:index-formatting} for examples of how
these entries are formatted.)  Note that
|\begin{macro}|\dotsbrk|\end{macro}| is not required to indicate a
macro definition.  It can also be used to indicate definitions of
\LaTeX{} datatypes, such as counters, lengths, and boxes:

\begin{verbatim}
    % \begin{macro}{myCounter}
    % This is an example of using the |macro| environment to format
    % something other than a macro.
    %    \begin{macrocode}
    \newcounter{myCounter}
    %    \end{macrocode}
    % \end{macro}
\end{verbatim}%
\indexthing{macrocode}

\usething{macro} and \usething{environment} environments can be nested.
This capability is useful not only for macros that define other
macros, but also when defining a group of related datatypes that share
a description:

\begin{verbatim}
    % \begin{macro}{\thingheight}
    % \begin{macro}{\thingwidth}
    % \begin{macro}{\thingdepth}
    % These lengths keep track of the dimensions of our |\thing|
    % box.  (Actually, we're just trying to show how to nest
    % |macro| environments.)
    %    \begin{macrocode}
    \newlength{\thingheight}
    \newlength{\thingwidth}
    \newlength{\thingdepth}
    %    \end{macrocode}
    % \end{macro}
    % \end{macro}
    % \end{macro}
\end{verbatim}%
\indexthing{macro}%
\indexthing{macrocode}

\noindent
Descriptionless \usething{macro} environments should generally be
avoided, as the formatting is a little ugly; the macro name appears on
its own line, to the left of an ``empty'' description, but the code
doesn't start until the next line.

There can be multiple \usething{macrocode} environments within a
|\begin{macro}|\dotsbrk|\end{macro}| or
|\begin{environment}|\dotsbrk|\end{environment}| block.
\index{comments|(}
This is the mechanism by which code can be commented internally to a
macro/environment.  (It's considered bad style to use ``|%|'' for
comments within a \usething{macrocode} block.)  Here's an example of
the way that a nontrivial macro might be commented:

\begin{verbatim}
    % \begin{macro}{\complexMacro}
    % Pretend that this is a very complex macro that needs
    % to have its various pieces documented.
    %    \begin{macrocode}
    \newcommand{\complexMacro}{%
    %    \end{macrocode}
    % Initialize all of our counters to zero.
    %    \begin{macrocode}
      \setcounter{count@i}{0}%
      \setcounter{count@ii}{0}%
      \setcounter{count@iii}{0}%
      \setcounter{count@iv}{0}%
    %    \end{macrocode}
    % Do some really complicated processing.
    %    \begin{macrocode}
\end{verbatim}%
\indexthing{macrocode}
|                  |$\vdots$
\begin{verbatim}
    %    \end{macrocode}
    % We're all finished now.
    %    \begin{macrocode}
    }
    %    \end{macrocode}
    % \end{macro}
\end{verbatim}%
\indexthing{macrocode}

\indexthingEnd{environment}
\indexthingEnd{macro}   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\index{comments|)}

Appendix~\ref{sec:skeleton-dtx-file} lists a complete, skeleton |.dtx|
file.

\index{documented LaTeX file@documented \LaTeX{} file|)}


\section{Tips, tricks, and recommendations}

\begin{itemize}
  \item Write lots of good documentation!  It really helps others
  understand your code and the package\index{package} as a whole.

  \item Use \LaTeX's sectioning commands to organize the code and
  clarify its structure (\eg |\subsection{|\texttt{Initialization
  macros}|}|, |\subsection{|\texttt{Helper functions}|}|,
  |\subsection{|\texttt{Exported macros and environments}|}|,~\dots).

\index{comments|(}

  \item Although commentary really belongs only in the typeset
  documentation, it is also possible to write comments that are
  visible only in the |.sty|\index{style file} file, in both the
  typeset documentation and the |.sty|\index{style file} file, or only
  in the |.dtx| source.  Table~\ref{tbl:comment-visibility} shows how
  to control comment visibility.

\index{comments|)}

    \begin{table}[hbp]
      \centering
      \caption{Comment visibility}
      \label{tbl:comment-visibility}
      \newcommand{\yes}{Y}%
      \newcommand{\no}{N}%
      \index{style file}%
      \index{comments}%
      \begin{tabular}{@{}ccl@{}}
        \hline
        Appears  &amp; Appears   \\
        in docs  &amp; in |.sty| &amp; \raisebox{1.5ex}[0pt]{Mechanism} \\
        \hline

        \no  &amp; \no  &amp; |% ^^A &lt;comment&gt;| \\[2ex]

        \no  &amp; \yes &amp; |% \iffalse|      \\
             &amp;      &amp; |%% &lt;comment&gt;|    \\
             &amp;      &amp; |% \fi|           \\[2ex]

        \yes &amp; \no  &amp; |% &lt;comment&gt;|     \\[2ex]

        \yes &amp; \yes &amp; |%% &lt;comment&gt;|    \\
        \hline
      \end{tabular}
    \end{table}

  \item All lines between |&lt;&lt;*package&gt;&gt;| and |&lt;&lt;/package&gt;&gt;|, except
  those within a \usething{macrocode} environment, should begin
  with ``|%|''.  Don't use any blank lines; these would get written to
  the |.sty|\index{style file} file (and oughtn't).

  \item Be sure to read ``The \ds{} Program'' and ``The \DOC{} and
  |shortvrb| Packages'', the documentation for \ds{} and \DOC{},
  respectively.  These explain how to do more advanced things with
  |.ins| and |.dtx| files than this tutorial covered.  Some advanced
  topics include the following:

    \begin{itemize} \item Extracting multiple |.sty|\index{style file}
    files from a single |.dtx| file.

      \item Putting different preambles in different
      |.sty|\index{style file} files.

      \item Extracting something other than a |.sty|\index{style file}
      file (\eg a configuration file or a Perl script) from a |.dtx|
      file.

      \item Changing the formatting of the typeset documentation.
    \end{itemize}

  \item You can use \usemacro{index} in the normal way to index things other
  than macros and environments.

  \item If you use Emacs as your text editor, look up the
  |string-rectangle| command.  This helps a great deal with adding a
  ``|%|'' to the beginning of every line in a region.

  \item \label{item:master-files} \DOC{} supports ``master''
  documentation files that typeset multiple |.dtx| files.  The
  advantage is that a set of related |.dtx| files can be typeset with
  continuous section numbering and a single, unified index.  In fact,
  the \LaTeXe{} source code itself is typeset using a master document
  (|source2e.tex|) that includes all of the myriad |.dtx| files that
  comprise \LaTeXe.

  To help produce master documents, the \usething{ltxdoc} class
  provides a command called ``\usemacro{DocInclude}''.
  \usething{ltxdoc}'s \usemacro{DocInclude} is much like \DOC's
  \usemacro{DocInput}---it even uses it internally---but has the
  following additional features.

    \begin{itemize}
      \item \usemacro{PrintIndex} is automatically handled properly.

      \item Every \usemacro{DocInclude}'d file is given a title page.

      \item \usemacro{tableofcontents} works as expected.  |.dtx| filenames
      are used as ``chapter'' names.
    \end{itemize}

  \noindent
  Note that \usemacro{DocInclude}, unlike \usemacro{DocInput}, assumes a |.dtx|
  extension.

  Appendix~\ref{sec:skeleton-master-file} presents a master-document
  skeleton that uses \usemacro{DocInclude} to typeset |&lt;file1&gt;.dtx|,
  |&lt;file2&gt;.dtx|, and |&lt;file3&gt;.dtx| as a single document.  If you
  prefer a more manual approach (\eg if you dislike \usemacro{DocInclude}'s
  file title pages), you can still use \usemacro{DocInput}.
\index{indexing|(}
  Just make sure to redefine \usemacro{PrintIndex} to do nothing;
  otherwise, each file will get its own index.  After all of the
  |.dtx| files have been typeset, call the original
  \usemacro{PrintIndex} command to print a unified index:

\begin{verbatim}
    \begin{document}
      \let\origPrintIndex=\PrintIndex \let\PrintIndex=\relax
      \DocInput{&lt;file1&gt;.dtx}
      \DocInput{&lt;file2&gt;.dtx}
      \DocInput{&lt;file3&gt;.dtx}
      \origPrintIndex
    \end{document}
\end{verbatim}%
\indexmacro{PrintIndex}%
\indexmacro{DocInput}

\index{indexing|)}

\index{package|(}
\index{"@@\texttt{"@}!in macro names|(}
  \item It is good practice for \LaTeX{} programs to use ``|@|''
  within the names of macros, lengths, counters, etc.\ that are
  declared globally, but intended to be used only internally to the
  package.  This prevents a user from inadvertently using such names
  in his own document.\footnote{Within a \LaTeX{} document, ``|@|'' is
  marked as catcode ``other'', not ``letter''.}  Another good practice
  is to prefix all global names that are internal to the package with
  the name of the package (\eg ``|\&lt;package&gt;@thing|'' instead of
  ``|\@thing|'' or---even worse---just ``|\thing|'').  This helps
  avoid inter-package naming conflicts.  Finally, because decimal
  digits are not normally allowed in macro names, it is common to use
  roman\index{roman numerals} numerals instead, for example: |\arg@i|,
  |\arg@ii|, |\arg@iii|, |\arg@iv|, etc.
\index{"@@\texttt{"@}!in macro names|)}
\index{package|)}
\end{itemize}


\appendix

\section{Skeleton files}

This section contains complete skeletons of the types of files
discussed in the rest of the document.  These skeletons can be used as
templates for creating your own packages.\index{package}

\index{installer file|(}

\subsection{A skeleton \texttt{.ins} file}
\label{sec:skeleton-ins-file}

\begin{verbatim}
%%
%% Copyright (C) &lt;year&gt; by &lt;your name&gt;
%%
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.2 of this license or (at your option) any later
%% version.  The latest version of this license is in:
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% and version 1.2 or later is part of all distributions of
%% LaTeX version 1999/12/01 or later.
%%

\input docstrip.tex
\keepsilent

\usedir{tex/latex/&lt;package&gt;}

\preamble

This is a generated file.

Copyright (C) &lt;year&gt; by &lt;your name&gt;

This file may be distributed and/or modified under the
conditions of the LaTeX Project Public License, either
version 1.2 of this license or (at your option) any later
version.  The latest version of this license is in:

   http://www.latex-project.org/lppl.txt

and version 1.2 or later is part of all distributions of
LaTeX version 1999/12/01 or later.

\endpreamble

\generate{\file{&lt;package&gt;.sty}{\from{&lt;package&gt;.dtx}{package}}}

\Msg{*********************************************************}
\Msg{*}
\Msg{* To finish the installation you have to move the}
\Msg{* following file into a directory searched by TeX:}
\Msg{*}
\Msg{* \space\space &lt;package&gt;.sty}
\Msg{*}
\Msg{* To produce the documentation run the file &lt;package&gt;.dtx}
\Msg{* through LaTeX.}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*********************************************************}

\endbatchfile
\end{verbatim}

\index{installer file|)}
\index{documented LaTeX file@documented \LaTeX{} file|(}


\subsection{A skeleton \texttt{.dtx} file}
\label{sec:skeleton-dtx-file}

\begin{verbatim}
% \iffalse meta-comment
%
% Copyright (C) &lt;year&gt; by &lt;your name&gt;
% -----------------------------------
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.2
% of this license or (at your option) any later version.
% The latest version of this license is in:
%
%    http://www.latex-project.org/lppl.txt
%
% and version 1.2 or later is part of all distributions of LaTeX
% version 1999/12/01 or later.
%
% \fi
%
% \iffalse
%&lt;&lt;package&gt;&gt;\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%&lt;&lt;package&gt;&gt;\ProvidesPackage{&lt;package&gt;}
%&lt;&lt;package&gt;&gt;   [&lt;YYYY&gt;/&lt;MM&gt;/&lt;DD&gt; v&lt;version&gt; &lt;brief description&gt;]
%
%&lt;&lt;*driver&gt;&gt;
\documentclass{ltxdoc}
\usepackage{&lt;package&gt;}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{&lt;package&gt;.dtx}
\end{document}
%&lt;&lt;/driver&gt;&gt;
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&amp;
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \&lt;&lt;
%   Equals        \=     Greater than  \&gt;&gt;     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v1.0}{&lt;YYYY&gt;/&lt;MM&gt;/&lt;DD&gt;}{Initial version}
%
% \GetFileInfo{&lt;package&gt;.sty}
%
% \DoNotIndex{&lt;list of control sequences&gt;}
%
% \title{The \textsf{&lt;package&gt;} package\thanks{This document
%   corresponds to \textsf{skeleton}~\fileversion,
%   dated \filedate.}}
% \author{&lt;your name&gt; \\ \texttt{&lt;your e-mail address&gt;}}
%
% \maketitle
%
% \begin{abstract}
%   Put text here.
% \end{abstract}
%
% \section{Introduction}
%
% Put text here.
%
% \section{Usage}
%
% \DescribeMacro{\YOURMACRO}
% Put description of |\YOURMACRO| here.
%
% \DescribeEnv{YOURENV}
% Put description of |YOURENV| here.
%
% \StopEventually{\PrintIndex}
%
% \section{Implementation}
%
% \begin{macro}{\YOURMACRO}
% Put explanation of |\YOURMACRO|'s implementation here.
%    \begin{macrocode}
\newcommand{\YOURMACRO}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{YOURENV}
% Put explanation of |YOURENV|'s implementation here.
%    \begin{macrocode}
\newenvironment{YOURENV}{}{}
%    \end{macrocode}
% \end{environment}
%
% \Finale
\endinput
\end{verbatim}

\index{documented LaTeX file@documented \LaTeX{} file|)}

\subsection{A skeleton master-document file (\texttt{.tex})}
\label{sec:skeleton-master-file}

\begin{verbatim}
\documentclass{ltxdoc}
\usepackage{&lt;file1&gt;}
\usepackage{&lt;file2&gt;}
\usepackage{&lt;file3&gt;}

\title{&lt;title&gt;}
\author{&lt;you&gt;}

\EnableCrossrefs
\CodelineIndex
\RecordChanges

\begin{document}
  \maketitle

  \begin{abstract}
    &lt;abstract&gt;
  \end{abstract}

  \tableofcontents

  \DocInclude{&lt;file1&gt;}
  \DocInclude{&lt;file2&gt;}
  \DocInclude{&lt;file3&gt;}
\end{document}
\end{verbatim}


% The following was generated by BibTeX and slightly touched up by hand.
\vfill
\begin{thebibliography}{1}
  \bibitem{Goossens1994:companion}
  Michel Goossens, Frank Mittelbach, and Alexander Samarin.
  \newblock \emph{The {\LaTeX} Companion}.
  \newblock Addison~Wesley, Reading, Massachusetts, October~1, 1994.
  \newblock ISBN~\mbox{0-201-54199-8}.

  \bibitem{Knuth1984:literate}
  Donald~E. Knuth.
  \newblock Literate programming.
  \newblock \emph{The Computer Journal}, 27(2):97--111, May 1984.
  \newblock British Computer Society. Available from
    \URL{http://www.literateprogramming.com/knuthweb.pdf}.
\end{thebibliography}

% Print an index.
\index{sty@\texttt{.sty}|see{style file}}
\index{ins@\texttt{.ins}|see{installer file}}
\index{dtx@\texttt{.dtx}|see{documented \LaTeX{} file}}
\printindex

\end{document}
</pre></body></html>