1 % \begin{meta-comment} <general public licence>
3 %% mdwtools common declarations
4 %% Copyright (c) 1996, 2002, 2003, 2020 Mark Wooding
6 %% This program is free software; you can redistribute it and/or modify
7 %% it under the terms of the GNU General Public License as published by
8 %% the Free Software Foundation; either version 2 of the License, or
9 %% (at your option) any later version.
11 %% This program is distributed in the hope that it will be useful,
12 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
13 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 %% GNU General Public License for more details.
16 %% You should have received a copy of the GNU General Public License
17 %% along with this program; if not, write to the Free Software
18 %% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 % \begin{meta-comment} <file preamble>
24 \ProvidesFile{mdwtools.tex}
25 [1996/05/10 1.4 Shared definitions for mdwtools .dtx files]
31 %% {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
32 %% 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
33 %% Digits \0\1\2\3\4\5\6\7\8\9
34 %% Exclamation \! Double quote \" Hash (number) \#
35 %% Dollar \$ Percent \% Ampersand \&
36 %% Acute accent \' Left paren \( Right paren \)
37 %% Asterisk \* Plus \+ Comma \,
38 %% Minus \- Point \. Solidus \/
39 %% Colon \: Semicolon \; Less than \<
40 %% Equals \= Greater than \> Question mark \?
41 %% Commercial at \@ Left bracket \[ Backslash \\
42 %% Right bracket \] Circumflex \^ Underscore \_
43 %% Grave accent \` Left brace \{ Vertical bar \|
44 %% Right brace \} Tilde \~}
47 % \section{Introduction and user guide}
49 % This file is really rather strange; it gets |\input| by other package
50 % documentation files to set up most of the environmental gubbins for them.
51 % It handles almost everything, like loading a document class, finding any
52 % packages, and building and formatting the title.
54 % It also offers an opportunity for users to customise my nice documentation,
55 % by using a |mdwtools.cfg| file (not included).
58 % \subsection{Declarations}
60 % A typical documentation file contains something like
61 % \begin{listinglist} \listingsize \obeylines
66 % The initial |\input| reads in this file and sets up the various commands
67 % which may be needed. The final |\mdwdoc| actually starts the document,
68 % inserting a title (which is automatically generated), a table of
69 % contents etc., and reads the documentation file in (using the |\DocInput|
70 % command from the \package{doc} package.
72 % \subsubsection{Describing packages}
74 % \DescribeMacro{\describespackage}
75 % \DescribeMacro{\describesclass}
76 % \DescribeMacro{\describesfile}
77 % \DescribeMacro{\describesfile*}
78 % The most important declarations are those which declare what the
79 % documentation describes. Saying \syntax{"\\describespackage{<package>}"}
80 % loads the \<package> (if necessary) and adds it to the auto-generated
81 % title, along with a footnote containing version information. Similarly,
82 % |\describesclass| adds a document class name to the title (without loading
83 % it -- the document itself must do this, with the |\documentclass| command).
84 % For files which aren't packages or classes, use the |\describesfile| or
85 % |\describesfile*| command (the $*$-version won't |\input| the file, which
86 % is handy for files like |mdwtools.tex|, which are already input).
88 % \DescribeMacro{\author}
89 % \DescribeMacro{\date}
90 % \DescribeMacro{\title}
91 % The |\author|, |\date| and |\title| declarations work slightly differently
92 % to normal -- they ensure that only the \emph{first} declaration has an
93 % effect. (Don't you play with |\author|, please, unless you're using this
94 % program to document your own packages.) Using |\title| suppresses the
95 % automatic title generation.
97 % \DescribeMacro{\docdate}
98 % The default date is worked out from the version string of the package or
99 % document class whose name is the same as that of the documentation file.
100 % You can choose a different `main' file by saying
101 % \syntax{"\\docdate{"<file>"}"}.
103 % \subsubsection{Contents handling}
105 % \DescribeMacro{\addcontents}
106 % A documentation file always has a table of contents. Other
107 % contents-like lists can be added by saying
108 % \syntax{"\\addcontents{"<extension>"}{"<command>"}"}. The \<extension>
109 % is the file extension of the contents file (e.g., \lit{lot} for the
110 % list of tables); the \<command> is the command to actually typeset the
111 % contents file (e.g., |\listoftables|).
113 % \subsubsection{Other declarations}
115 % \DescribeMacro{\implementation}
116 % The \package{doc} package wants you to say
117 % \syntax{"\\StopEventually{"<stuff>"}"}' before describing the package
118 % implementation. Using |mdwtools.tex|, you just say |\implementation|, and
119 % everything works. It will automatically read in the licence text (from
120 % |gpl.tex|, and wraps some other things up.
123 % \subsection{Other commands}
125 % The |mdwtools.tex| file includes the \package{syntax} and \package{sverb}
126 % packages so that they can be used in documentation files. It also defines
127 % some trivial commands of its own.
130 % Saying \syntax{"\\<"<text>">" is the same as "\\synt{"<text>"}"}; this
131 % is a simple abbreviation.
133 % \DescribeMacro{\smallf}
134 % Saying \syntax{"\\smallf" <number>"/"<number>} typesets a little fraction,
135 % like this: \smallf 3/4. It's useful when you want to say that the default
136 % value of a length is 2 \smallf 1/2\,pt, or something like that.
139 % \subsection{Customisation}
141 % You can customise the way that the package documentation looks by writing
142 % a file called |mdwtools.cfg|. You can redefine various commands (before
143 % they're defined here, even; |mdwtools.tex| checks most of the commands that
144 % it defines to make sure they haven't been defined already.
146 % \DescribeMacro{\indexing}
147 % If you don't want the prompt about whether to generate index files, you
148 % can define the |\indexing| command to either \lit{y} or \lit{n}. I'd
149 % recommend that you use |\providecommand| for this, to allow further
150 % customisation from the command line.
152 % \DescribeMacro{\mdwdateformat}
153 % If you don't like my date format (maybe you're American or something),
154 % you can redefine the |\mdwdateformat| command. It takes three arguments:
155 % the year, month and date, as numbers; it should expand to something which
156 % typesets the date nicely. The default format gives something like
157 % `10 May 1996'. You can produce something rather more exotic, like
158 % `10\textsuperscript{th} May \textsc{\romannumeral 1996}' by saying
160 %\newcommand{\mdwdateformat}[3]{%
161 % \number#3\textsuperscript{\numsuffix{#3}}\ %
163 % \textsc{\romannumeral #1}%
166 % \DescribeMacro{\monthname}
167 % \DescribeMacro{\numsuffix}
168 % Saying \syntax{"\\monthname{"<number>"}"} expands to the name of the
169 % numbered month (which can be useful when doing date formats). Saying
170 % \syntax{"\\numsuffix{"<number>"}"} will expand to the appropriate suffix
171 % (`th' or `rd' or whatever) for the \<number>. You'll have to superscript
172 % it yourself, if this is what you want to do. Putting the year number
173 % in roman numerals is just pretentious |;-)|.
175 % \DescribeMacro{\mdwhook}
176 % After all the declarations in |mdwtools.tex|, the command |\mdwhook| is
177 % executed, if it exists. This can be set up by the configuration file
178 % to do whatever you want.
180 % There are lots of other things you can play with; you should look at the
181 % implementation section to see what's possible.
185 % \section{Implementation}
191 % The first thing is that I'm not a \LaTeX\ package or anything official
192 % like that, so I must enable `|@|' as a letter by hand.
198 % Now input the user's configuration file, if it exists. This is fairly
202 \@input{mdwtools.cfg}
205 % Well, that's the easy bit done.
208 % \subsection{Initialisation}
210 % Obviously the first thing to do is to obtain a document class. Obviously,
211 % it would be silly to do this if a document class has already been loaded,
212 % either by the package documentation or by the configuration file.
214 % The only way I can think of for finding out if a document class is already
215 % loaded is by seeing if the |\documentclass| command has been redefined
216 % to raise an error. This isn't too hard, really.
218 % If my \package{strayman} document class is available, then I'd prefer to
222 \ifx\documentclass\@twoclasseserror\else
223 \IfFileExists{strayman.cls}
224 {\documentclass[a4paper]{strayman}}
225 {\documentclass[a4paper]{ltxdoc}}
226 \ifx\doneclasses\mdw@undefined\else\doneclasses\fi
230 % If I can use better fonts, then that would be nice.
233 \usepackage[T1]{fontenc}
234 \IfFileExists{mdwfonts.sty}
235 {\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}}{}
238 % As part of my standard environment, I'll load some of my more useful
239 % packages. If they're already loaded (possibly with different options),
240 % I'll not try to load them again.
243 \@ifpackageloaded{doc}{}{\usepackage{doc}}
244 \@ifpackageloaded{syntax}{}{\usepackage[rounded]{syntax}}
245 \@ifpackageloaded{sverb}{}{\usepackage{sverb}}
248 % If I'm not using the \package{ltxdoc} document class then I'll need some of
249 % its definitions. I've no idea why these aren't part of \package{doc}\ldots
251 % \begin{macro}{\cmd}
254 \def\cmd#1{\expandafter\cmd@i\string#1\x}
255 \def\cmd@i#1#2\x{\cs{#2}}
256 \DeclareRobustCommand\cs[1]{\texttt{\char`\\#1}}
261 % \begin{macro}{\marg}
262 % \begin{macro}{\oarg}
263 % \begin{macro}{\parg}
265 \def\@arg#1#2#3{\texttt{#1}\meta{#2}\texttt{#3}}
266 \def\marg#1{\@arg{\char`\{}{#1}{\char`\}}}
267 \def\oarg#1{\@arg{[}{#1}{]}}
268 \def\parg#1{\@arg{(}{#1}{)}}
274 % \subsection{Some macros for interaction}
276 % I like the \LaTeX\ star-boxes, although it's a pain having to cope with
277 % \TeX's space-handling rules. I'll define a new typing-out macro which
278 % makes spaces more significant, and has a $*$-version which doesn't put
279 % a newline on the end, and interacts prettily with |\read|.
281 % First of all, I need to make spaces active, so I can define things about
285 \begingroup\obeyspaces
288 % Now to define the main macro. This is easy stuff. Spaces must be
289 % carefully rationed here, though.
291 % I'll start a group, make spaces active, and make spaces expand to ordinary
292 % space-like spaces. Then I'll look for a star, and pass either |\message|
293 % (which doesn't start a newline, and interacts with |\read| well) or
294 % |\immediate\write 16| which does a normal write well.
298 \begingroup\catcode`\ \active\let \space%
299 \@ifstar{\mdwtype@i{\message}}{\mdwtype@i{\immediate\write\sixt@@n}}%
304 % Now for the easy bit. I have the thing to do, and the thing to do it to,
305 % so do that and end the group.
308 \def\mdwtype@i#1#2{#1{#2}\endgroup}
312 % \subsection{Decide on indexing}
314 % A configuration file can decide on indexing by defining the |\indexing|
315 % macro to either \lit{y} or \lit{n}. If it's not set, then I'll prompt
318 % First of all, I want a switch to say whether I'm indexing.
324 % Right: now I need to decide how to make progress. If the macro's not set,
325 % then I want to set it, and start a row of stars.
328 \ifx\indexing\@@undefined
329 \mdwtype{*****************************}
334 % Now enter a loop, asking the user whether to do indexing, until I get
340 \if y\indexing\@tempswatrue\createindextrue\fi
341 \if Y\indexing\@tempswatrue\createindextrue\fi
342 \if n\indexing\@tempswatrue\createindexfalse\fi
343 \if N\indexing\@tempswatrue\createindexfalse\fi
345 \mdwtype*{* Create index files? (y/n) *}
346 \read\sixt@@n to\indexing%
350 % Now, based on the results of that, display a message about the indexing.
353 \mdwtype{*****************************}
355 \mdwtype{* Creating index files *}
356 \mdwtype{* This may take some time *}
358 \mdwtype{* Not creating index files *}
360 \mdwtype{*****************************}
363 % Now I can play with the indexing commands of the \package{doc} package
364 % to do whatever it is that the user wants.
376 % And register lots of plain \TeX\ things which shouldn't be indexed.
377 % This contains lots of |\if|\dots\ things which don't fit nicely in
378 % conditionals, which is a shame. Still, it doesn't matter that much,
382 \DoNotIndex{\def,\long,\edef,\xdef,\gdef,\let,\global}
383 \DoNotIndex{\if,\ifnum,\ifdim,\ifcat,\ifmmode,\ifvmode,\ifhmode,%
384 \iftrue,\iffalse,\ifvoid,\ifx,\ifeof,\ifcase,\else,\or,\fi}
385 \DoNotIndex{\box,\copy,\setbox,\unvbox,\unhbox,\hbox,%
386 \vbox,\vtop,\vcenter}
387 \DoNotIndex{\@empty,\immediate,\write}
388 \DoNotIndex{\egroup,\bgroup,\expandafter,\begingroup,\endgroup}
389 \DoNotIndex{\divide,\advance,\multiply,\count,\dimen}
390 \DoNotIndex{\relax,\space,\string}
391 \DoNotIndex{\csname,\endcsname,\@spaces,\openin,\openout,%
393 \DoNotIndex{\catcode,\endinput}
394 \DoNotIndex{\jobname,\message,\read,\the,\m@ne,\noexpand}
395 \DoNotIndex{\hsize,\vsize,\hskip,\vskip,\kern,\hfil,\hfill,\hss}
396 \DoNotIndex{\m@ne,\z@,\z@skip,\@ne,\tw@,\p@}
397 \DoNotIndex{\dp,\wd,\ht,\vss,\unskip}
400 % Last bit of indexing stuff, for now: I'll typeset the index in two columns
401 % (the default is three, which makes them too narrow for my tastes).
404 \setcounter{IndexColumns}{2}
408 % \subsection{Selectively defining things}
410 % I don't want to tread on anyone's toes if they redefine any of these
411 % commands and things in a configuration file. The following definitions
412 % are fairly evil, but should do the job OK.
414 % \begin{macro}{\@gobbledef}
416 % This macro eats the following |\def|inition, leaving not a trace behind.
419 \def\@gobbledef#1#{\@gobble}
424 % \begin{macro}{\tdef}
425 % \begin{macro}{\tlet}
427 % The |\tdef| command is a sort of `tentative' definition -- it's like
428 % |\def| if the control sequence named doesn't already have a definition.
429 % |\tlet| does the same thing with |\let|.
434 \expandafter\def\expandafter#1%
436 \expandafter\@gobbledef%
439 \def\tlet#1#2{\ifx#1\@@undefined\let#1=#2\fi}
446 % \subsection{General markup things}
448 % Now for some really simple things. I'll define how to typeset package
449 % names and environment names (both in the sans serif font, for now).
452 \tdef\package{\textsf}
456 % I'll define the |\<|\dots|>| shortcut for syntax items suggested in the
457 % \package{syntax} package.
460 \tdef\<#1>{\synt{#1}}
463 % And because it's used in a few places (mainly for typesetting lengths),
464 % here's a command for typesetting fractions in text.
467 \tdef\smallf#1/#2{\ensuremath{^{#1}\!/\!_{#2}}}
471 % \subsection{Custom description lists}
473 % For some bizarre reason, the \LaTeX\ \env{description} environment sets
474 % |\itemindent| so that the label starts |\labelsep| into the left margin,
475 % and the default |\makelabel| must therefore contain a hack to compensate.
476 % This is fixed in the \package{strayman} document class, and by the
477 % \package{mdwlist} package in this collection. But this introduces a
478 % problem: if I want to set a \env{description} list with custom labels, how
479 % can I do this without messing up the spacing?
481 % Detection of the relevant packages is done in an awfully hacky way, because
482 % \LaTeXe\ seems to go out of its way to forget which packages have been
483 % loaded at |\begin{document}| time.
486 \def\setdescriptionlabel#1{%
487 \if1\ifx\sectindent\xxundefined% strayman?
488 \ifx\defaultdesc\xxundefined% mdwlist?
490 \def\makelabel##1{\hskip\labelsep\relax#1}%
492 \def\makelabel##1{#1}%
498 % \subsection{A table environment}
500 % \begin{environment}{tab}
502 % Most of the packages don't use the (obviously perfect) \package{mdwtab}
503 % package, because it's big, and takes a while to load. Here's an
504 % environment for typesetting centred tables. The first (optional) argument
505 % is some declarations to perform. The mandatory argument is the table
506 % preamble (obviously).
510 \newenvironment{tab}[2][\relax]{%
525 % \subsection{Commenting out of stuff}
527 % \begin{environment}{meta-comment}
529 % Using |\iffalse|\dots|\fi| isn't much fun. I'll define a gobbling
530 % environment using the \package{sverb} stuff.
533 \ignoreenv{meta-comment}
539 % \subsection{Float handling}
541 % This gubbins will try to avoid float pages as much as possible, and (with
542 % any luck) encourage floats to be put on the same pages as text.
545 \def\textfraction{0.1}
546 \def\topfraction{0.9}
547 \def\bottomfraction{0.9}
548 \def\floatpagefraction{0.7}
551 % Now redefine the default float-placement parameters to allow `here' floats.
554 \def\fps@figure{htbp}
559 % \subsection{Other bits of parameter tweaking}
561 % Make \env{grammar} environments look pretty, by indenting the left hand
562 % sides by a large amount.
568 % I don't like being told by \TeX\ that my paragraphs are hard to linebreak:
569 % I know this already. This lot should shut \TeX\ up about most problems.
577 % Also make \TeX\ shut up in the index. The \package{multicol} package
578 % irritatingly plays with |\hbadness|. This is the best hook I could find
579 % for playing with this setting.
582 \expandafter\def\expandafter\IndexParms\expandafter{%
588 % The other thing I really don't like is `Marginpar moved' warnings. This
589 % will get rid of them, and lots of other \LaTeX\ warnings at the same time.
592 \let\@latex@warning@no@line\@gobble
595 % Put some extra space between table rows, please.
598 \def\arraystretch{1.2}
601 % Most of the code is at guard level one, so typeset that in upright text.
604 \setcounter{StandardModuleDepth}{1}
608 % \subsection{Contents handling}
610 % I use at least one contents file (the main table of contents) although
611 % I may want more. I'll keep a list of contents files which I need to
614 % There are two things I need to do to contents files here:
616 % \item I must typeset the table of contents at the beginning of the
618 % \item I want to typeset tables of contents in two columns (using the
619 % \package{multicol} package).
622 % The list consists of items of the form
623 % \syntax{"\\do{"<extension>"}{"<command>"}"}, where \<extension> is the
624 % file extension of the contents file, and \<command> is the command to
627 % \begin{macro}{\docontents}
629 % This is where I keep the list of contents files. I'll initialise it to
630 % just do the standard contents table.
633 \def\docontents{\do{toc}{\tableofcontents}}
638 % \begin{macro}{\addcontents}
640 % By saying \syntax{"\\addcontents{"<extension>"}{"<command>"}"}, a document
641 % can register a new table of contents which gets given the two-column
642 % treatment properly. This is really easy to implement.
645 \def\addcontents#1#2{%
646 \toks@\expandafter{\docontents\do{#1}{#2}}%
647 \edef\docontents{\the\toks@}%
654 % \subsection{Finishing it all off}
656 % \begin{macro}{\finalstuff}
658 % The |\finalstuff| macro is a hook for doing things at the end of the
659 % document. Currently, it inputs the licence agreement as an appendix.
662 \tdef\finalstuff{\appendix\part*{Appendix}\input{gpl}}
667 % \begin{macro}{\implementation}
669 % The |\implementation| macro starts typesetting the implementation of
670 % the package(s). If we're not doing the implementation, it just does
671 % this lot and ends the input file.
673 % I define a macro with arguments inside the |\StopEventually|, which causes
674 % problems, since the code gets put through an extra level of |\def|fing
675 % depending on whether the implementation stuff gets typeset or not. I'll
676 % store the code I want to do in a separate macro.
679 \def\implementation{\StopEventually{\attheend}}
682 % Now for the actual activity. First, I'll do the |\finalstuff|. Then, if
683 % \package{doc}'s managed to find the \package{multicol} package, I'll add
684 % the end of the environment to the end of each contents file in the list.
685 % Finally, I'll read the index in from its formatted |.ind| file.
691 \def\do##1##2{\addtocontents{##1}{\protect\end{multicols}}}%
694 \ifx\backmatter\@@undefined\else\backmatter\fi%
702 % \subsection{File version information}
704 % \begin{macro}{\mdwpkginfo}
706 % For setting up the automatic titles, I'll need to be able to work out
707 % file versions and things. This macro will, given a file name, extract
708 % from \LaTeX\ the version information and format it into a sensible string.
710 % First of all, I'll put the original string (direct from the
711 % |\Provides|\dots\ command). Then I'll pass it to another macro which can
712 % parse up the string into its various bits, along with the original
717 \edef\@tempa{\csname ver@#1\endcsname}%
718 \expandafter\mdwpkginfo@i\@tempa\@@#1\@@%
722 % Now for the real business. I'll store the string I build in macros called
723 % \syntax{"\\"<filename>"date", "\\"<filename>"version" and
724 % "\\"<filename>"info"}, which store the file's date, version and
725 % `information string' respectively. (Note that the file extension isn't
726 % included in the name.)
728 % This is mainly just tedious playing with |\expandafter|. The date format
729 % is defined by a separate macro, which can be modified from the
730 % configuration file.
733 \def\mdwpkginfo@i#1/#2/#3 #4 #5\@@#6.#7\@@{%
734 \expandafter\def\csname #6date\endcsname%
735 {\protect\mdwdateformat{#1}{#2}{#3}}%
736 \expandafter\def\csname #6version\endcsname{#4}%
737 \expandafter\def\csname #6info\endcsname{#5}%
743 % \begin{macro}{\mdwdateformat}
745 % Given three arguments, a year, a month and a date (all numeric), build a
746 % pretty date string. This is fairly simple really.
749 \tdef\mdwdateformat#1#2#3{\number#3\ \monthname{#2}\ \number#1}
752 January\or February\or March\or April\or May\or June\or%
753 July\or August\or September\or October\or November\or December%
765 \fi\fi\fi\fi\fi\fi\fi%
771 % \begin{macro}{\mdwfileinfo}
773 % Saying \syntax{"\\mdwfileinfo{"<file-name>"}{"<info>"}"} extracts the
774 % wanted item of \<info> from the version information for file \<file-name>.
777 \def\mdwfileinfo#1#2{\mdwfileinfo@i{#2}#1.\@@}
778 \def\mdwfileinfo@i#1#2.#3\@@{\csname#2#1\endcsname}
784 % \subsection{List handling}
786 % There are several other lists I need to build. These macros will do
787 % the necessary stuff.
789 % \begin{macro}{\mdw@ifitem}
791 % The macro \syntax{"\\mdw@ifitem"<item>"\\in"<list>"{"<true-text>"}"^^A
792 % "{"<false-text>"}"} does \<true-text> if the \<item> matches any item in
793 % the \<list>; otherwise it does \<false-text>.
796 \def\mdw@ifitem#1\in#2{%
799 \def\do##1{\def\@tempb{##1}\ifx\@tempa\@tempb\@tempswatrue\fi}%
801 \if@tempswa\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi%
807 % \begin{macro}{\mdw@append}
809 % Saying \syntax{"\\mdw@append"<item>"\\to"<list>} adds the given \<item>
810 % to the end of the given \<list>.
813 \def\mdw@append#1\to#2{%
815 \toks\tw@\expandafter{#2}%
816 \edef#2{\the\toks\tw@\the\toks@}%
822 % \begin{macro}{\mdw@prepend}
824 % Saying \syntax{"\\mdw@prepend"<item>"\\to"<list>} adds the \<item> to the
825 % beginning of the \<list>.
828 \def\mdw@prepend#1\to#2{%
830 \toks\tw@\expandafter{#2}%
831 \edef#2{\the\toks@\the\toks\tw@}%
837 % \begin{macro}{\mdw@add}
839 % Finally, saying \syntax{"\\mdw@add"<item>"\\to"<list>} adds the \<item>
840 % to the list only if it isn't there already.
843 \def\mdw@add#1\to#2{\mdw@ifitem#1\in#2{}{\mdw@append#1\to#2}}
849 % \subsection{Described file handling}
851 % I'l maintain lists of packages, document classes, and other files
852 % described by the current documentation file.
854 % First of all, I'll declare the various list macros.
862 % \begin{macro}{\describespackage}
864 % A document file can declare that it describes a package by saying
865 % \syntax{"\\describespackage{"<package-name>"}"}. I add the package to
866 % my list, read the package into memory (so that the documentation can
867 % offer demonstrations of it) and read the version information.
870 \def\describespackage#1{%
871 \mdw@ifitem#1\in\dopackages{}{%
872 \mdw@append#1\to\dopackages%
881 % \begin{macro}{\describesclass}
883 % By saying \syntax{"\\describesclass{"<class-name>"}"}, a document file
884 % can declare that it describes a document class. I'll assume that the
885 % document class is already loaded, because it's much too late to load
889 \def\describesclass#1{\mdw@add#1\to\doclasses\mdwpkginfo{#1.cls}}
894 % \begin{macro}{\describesfile}
896 % Finally, other `random' files, which don't have the status of real \LaTeX\
897 % packages or document classes, can be described by saying \syntax{^^A
898 % "\\describesfile{"<file-name>"}" or "\\describesfile*{"<file-name>"}"}.
899 % The difference is that the starred version will not |\input| the file.
903 \@ifstar{\describesfile@i\@gobble}{\describesfile@i\input}%
905 \def\describesfile@i#1#2{%
906 \mdw@ifitem#2\in\dootherfiles{}{%
907 \mdw@add#2\to\dootherfiles%
917 % \subsection{Author and title handling}
919 % I'll redefine the |\author| and |\title| commands so that I get told
920 % whether I need to do it myself.
922 % \begin{macro}{\author}
924 % This is easy: I'll save the old meaning, and then redefine |\author| to
925 % do the old thing and redefine itself to then do nothing.
928 \let\mdw@author\author
929 \def\author{\let\author\@gobble\mdw@author}
934 % \begin{macro}{\title}
936 % And oddly enough, I'll do exactly the same thing for the title, except
937 % that I'll also disable the |\mdw@buildtitle| command, which constructs
938 % the title automatically.
942 \def\title{\let\title\@gobble\let\mdw@buildtitle\relax\mdw@title}
947 % \begin{macro}{\date}
949 % This works in a very similar sort of way.
952 \def\date#1{\let\date\@gobble\def\today{#1}}
957 % \begin{macro}{\datefrom}
959 % Saying \syntax{"\\datefrom{"<file-name>"}"} sets the document date from
960 % the given filename.
964 \protected@edef\@tempa{\noexpand\date{\csname #1date\endcsname}}%
971 % \begin{macro}{\docfile}
973 % Saying \syntax{"\\docfile{"<file-name>"}"} sets up the file name from which
974 % documentation will be read.
978 \def\@tempa##1.##2\@@{\def\@basefile{##1.##2}\def\@basename{##1}}%
979 \edef\@tempb{\noexpand\@tempa#1\noexpand\@@}%
984 % I'll set up a default value as well.
987 \docfile{\jobname.dtx}
993 % \subsection{Building title strings}
995 % This is rather tricky. For each list, I need to build a legible looking
998 % \begin{macro}{\mdw@addtotitle}
1001 %\syntax{"\\mdw@addtotitle{"<list>"}{"<command>"}{"<singular>"}{"<plural>"}"}
1002 % I can add the contents of a list to the current title string in the
1003 % |\mdw@title| macro.
1006 \tdef\mdw@addtotitle#1#2#3#4{%
1009 % Now to get to work. I need to keep one `lookahead' list item, and a count
1010 % of the number of items read so far. I'll keep the lookahead item in
1011 % |\@nextitem| and the counter in |\count@|. Things are even worse because
1012 % the footnote symbols should appear \emph{after} the separating punctuation,
1013 % so we need to delay those by another cycle, hence we have |\@nextnote| and
1020 % Now I'll define what to do for each list item. The |\protect| command is
1021 % already set up appropriately for playing with |\edef| commands.
1027 % The first job is to add the previous item to the title string. If this
1028 % is the first item, though, I'll just add the appropriate \lit{The } or
1029 % \lit{ and the } string to the title (this is stored in the |\@prefix|
1030 % macro). Also maintain a parallel version which doesn't have the footnotes
1031 % in: this will be suitable for a running header.
1036 \ifcase\count@\@prefix%
1038 \else,\@prevnote\ \@nextitem%
1041 \edef\mdw@runningtitle{%
1043 \ifcase\count@\@prefix%
1050 % That was rather easy. Now I'll set up the |\@previtem| and |\@nextitem|
1051 % macros for the next time around the loop.
1054 \edef\@nextitem{\protect#2{##1}}%
1055 \let\@prevnote\@nextnote
1058 The \protect#2{##1} #3 is currently at version %
1059 \mdwfileinfo{##1}{version}, dated \mdwfileinfo{##1}{date}.%
1064 % Finally, I need to increment the counter.
1067 \advance\count@\@ne%
1071 % Now execute the list.
1077 % I still have one item left over, unless the list was empty. I'll add
1084 \or\@nextitem\@nextnote\space#3%
1085 \or\@prevnote\ and \@nextitem\@nextnote\space#4%
1086 \else,\@prevnote\ and \@nextitem\@nextnote\space#4%
1089 \edef\mdw@runningtitle{%
1092 \or\@nextitem\space#3%
1093 \or\ and \@nextitem\space#4%
1094 \else,\ and \@nextitem\space#4%
1099 % Finally, if $|\count@| \ne 0$, I must set |\@prefix| to \lit{ and the }.
1102 \ifnum\count@>\z@\def\@prefix{ and the }\fi%
1108 % \begin{macro}{\mdw@buildtitle}
1110 % This macro will actually do the job of building the title string.
1113 \tdef\mdw@buildtitle{%
1116 % First of all, I'll open a group to avoid polluting the namespace with
1117 % my gubbins (although the code is now much tidier than it has been in
1118 % earlier releases).
1124 % The title building stuff makes extensive use of |\edef|. I'll set
1125 % |\protect| appropriately. (For those not in the know,
1126 % |\@unexpandable@protect| expands to `|\noexpand\protect\noexpand|',
1127 % which prevents expansion of the following macro, and inserts a |\protect|
1128 % in front of it ready for the next |\edef|.)
1131 \let\@@protect\protect\let\protect\@unexpandable@protect%
1134 % Set up some simple macros ready for the main code.
1138 \def\mdw@runningtitle{}%
1142 % Now build the title. This is fun.
1145 \mdw@addtotitle\dopackages\package{package}{packages}%
1146 \mdw@addtotitle\doclasses\package{document class}{document classes}%
1147 \mdw@addtotitle\dootherfiles\texttt{file}{files}%
1150 % Now I want to end the group and set the title from my string. The
1151 % following hacking will do this.
1156 \noexpand\title{\noexpand\mdw@titlehack\mdw@title}%
1157 \def\noexpand\@headertitle{\mdw@runningtitle}%
1165 % \begin{macro}{\mdw@titlehack}
1167 % Wait! Did you notice that |\mdw@titlehack|? What's that about?
1169 % It turns out that the default document classes hack the footnote insertion
1170 % commands to make footnote symbols take up no horizontal space in the title.
1171 % Apparently this makes author names look as if they're centred properly when
1172 % there are affiliation footnotes. Anyway, \package{doc} perpetuates this
1173 % silliness, but it makes a mess of the version markers I insert, so I must
1174 % deploy countermeasures.
1177 \def\mdw@titlehack{\def\@makefnmark{$\m@th^{\@thefnmark}$}}
1182 % \subsection{Starting the main document}
1184 % \begin{macro}{\mdwdoc}
1186 % Once the document preamble has done all of its stuff, it calls the
1187 % |\mdwdoc| command, which takes over and really starts the documentation
1194 % First, I'll construct the title string.
1198 \author{Mark Wooding}%
1201 % Set up the date string based on the date of the package which shares
1202 % the same name as the current file.
1205 \datefrom\@basename%
1208 % Set up verbatim characters after all the packages have started.
1215 % Start the document, and put the title in.
1219 \ifx\frontmatter\@@undefined\else\frontmatter\fi%
1223 % This is nasty. It makes maths displays work properly in demo environments.
1224 % \emph{The \LaTeX\ Companion} exhibits the bug which this hack fixes. So
1228 \abovedisplayskip\z@%
1231 % Now start the contents tables. After starting each one, I'll make it
1236 \ifhave@multicol\addtocontents{##1}{%
1237 \protect\begin{multicols}{2}%
1245 % Input the main file now.
1248 \ifx\mainmatter\@@undefined\else\mainmatter\fi%
1249 \DocInput{\@basefile}%
1252 % That's it. I'm done.
1262 % \subsection{And finally\dots}
1264 % Right at the end I'll put a hook for the configuration file.
1267 \ifx\mdwhook\@@undefined\else\expandafter\mdwhook\fi
1270 % That's all the code done now. I'll change back to `user' mode, where
1271 % all the magic control sequences aren't allowed any more.
1278 % Oh, wait! What if I want to typeset this documentation? Aha. I'll cope
1279 % with that by comparing |\jobname| with my filename |mdwtools|. However,
1280 % there's some fun here, because |\jobname| contains category-12 letters,
1281 % while my letters are category-11. Time to play with |\string| in a messy
1287 \edef\@tempa{\expandafter\@gobble\string\mdwtools}
1288 \edef\@tempb{\jobname}
1290 \describesfile*{mdwtools.tex}
1291 \docfile{mdwtools.tex}
1301 % \hfill Mark Wooding, \today