slowbox.dtx: Delete spurious `{'.
[mdwtools] / mdwtools.tex
CommitLineData
86f6a31e 1% \begin{meta-comment}
2%
21d3670d 3% $Id: mdwtools.tex,v 1.2 2003/09/05 16:15:33 mdw Exp $
86f6a31e 4%
5% Common declarations for mdwtools.dtx files
6%
7% (c) 1996 Mark Wooding
8%
86f6a31e 9% \end{meta-comment}
10%
11% \begin{meta-comment} <general public licence>
12%%
13%% mdwtools common declarations
14%% Copyright (c) 1996 Mark Wooding
15%%
16%% This program is free software; you can redistribute it and/or modify
17%% it under the terms of the GNU General Public License as published by
18%% the Free Software Foundation; either version 2 of the License, or
19%% (at your option) any later version.
20%%
21%% This program is distributed in the hope that it will be useful,
22%% but WITHOUT ANY WARRANTY; without even the implied warranty of
23%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24%% GNU General Public License for more details.
25%%
26%% You should have received a copy of the GNU General Public License
27%% along with this program; if not, write to the Free Software
28%% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29%%
30% \end{meta-comment}
31%
32% \begin{meta-comment} <file preamble>
33%<*mdwtools>
34\ProvidesFile{mdwtools.tex}
35 [1996/05/10 1.4 Shared definitions for mdwtools .dtx files]
36%</mdwtools>
37% \end{meta-comment}
38%
ba741fe9 39% \CheckSum{804}
86f6a31e 40%% \CharacterTable
41%% {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
42%% 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
43%% Digits \0\1\2\3\4\5\6\7\8\9
44%% Exclamation \! Double quote \" Hash (number) \#
45%% Dollar \$ Percent \% Ampersand \&
46%% Acute accent \' Left paren \( Right paren \)
47%% Asterisk \* Plus \+ Comma \,
48%% Minus \- Point \. Solidus \/
49%% Colon \: Semicolon \; Less than \<
50%% Equals \= Greater than \> Question mark \?
51%% Commercial at \@ Left bracket \[ Backslash \\
52%% Right bracket \] Circumflex \^ Underscore \_
53%% Grave accent \` Left brace \{ Vertical bar \|
54%% Right brace \} Tilde \~}
55%%
56%
57% \section{Introduction and user guide}
58%
59% This file is really rather strange; it gets |\input| by other package
60% documentation files to set up most of the environmental gubbins for them.
61% It handles almost everything, like loading a document class, finding any
62% packages, and building and formatting the title.
63%
64% It also offers an opportunity for users to customise my nice documentation,
65% by using a |mdwtools.cfg| file (not included).
66%
67%
68% \subsection{Declarations}
69%
70% A typical documentation file contains something like
71% \begin{listinglist} \listingsize \obeylines
72% |\input{mdwtools}|
73% \<declarations>
74% |\mdwdoc|
75% \end{listinglist}
76% The initial |\input| reads in this file and sets up the various commands
77% which may be needed. The final |\mdwdoc| actually starts the document,
78% inserting a title (which is automatically generated), a table of
79% contents etc., and reads the documentation file in (using the |\DocInput|
80% command from the \package{doc} package.
81%
82% \subsubsection{Describing packages}
83%
84% \DescribeMacro{\describespackage}
85% \DescribeMacro{\describesclass}
86% \DescribeMacro{\describesfile}
87% \DescribeMacro{\describesfile*}
88% The most important declarations are those which declare what the
89% documentation describes. Saying \syntax{"\\describespackage{<package>}"}
90% loads the \<package> (if necessary) and adds it to the auto-generated
91% title, along with a footnote containing version information. Similarly,
92% |\describesclass| adds a document class name to the title (without loading
93% it -- the document itself must do this, with the |\documentclass| command).
94% For files which aren't packages or classes, use the |\describesfile| or
95% |\describesfile*| command (the $*$-version won't |\input| the file, which
96% is handy for files like |mdwtools.tex|, which are already input).
97%
98% \DescribeMacro{\author}
99% \DescribeMacro{\date}
100% \DescribeMacro{\title}
101% The |\author|, |\date| and |\title| declarations work slightly differently
102% to normal -- they ensure that only the \emph{first} declaration has an
103% effect. (Don't you play with |\author|, please, unless you're using this
104% program to document your own packages.) Using |\title| suppresses the
105% automatic title generation.
106%
107% \DescribeMacro{\docdate}
108% The default date is worked out from the version string of the package or
109% document class whose name is the same as that of the documentation file.
110% You can choose a different `main' file by saying
111% \syntax{"\\docdate{"<file>"}"}.
112%
113% \subsubsection{Contents handling}
114%
115% \DescribeMacro{\addcontents}
116% A documentation file always has a table of contents. Other
117% contents-like lists can be added by saying
118% \syntax{"\\addcontents{"<extension>"}{"<command>"}"}. The \<extension>
119% is the file extension of the contents file (e.g., \lit{lot} for the
120% list of tables); the \<command> is the command to actually typeset the
121% contents file (e.g., |\listoftables|).
122%
123% \subsubsection{Other declarations}
124%
125% \DescribeMacro{\implementation}
126% The \package{doc} package wants you to say
127% \syntax{"\\StopEventually{"<stuff>"}"}' before describing the package
128% implementation. Using |mdwtools.tex|, you just say |\implementation|, and
129% everything works. It will automatically read in the licence text (from
130% |gpl.tex|, and wraps some other things up.
131%
e8e9e5d8 132%
86f6a31e 133% \subsection{Other commands}
134%
135% The |mdwtools.tex| file includes the \package{syntax} and \package{sverb}
136% packages so that they can be used in documentation files. It also defines
137% some trivial commands of its own.
138%
139% \DescribeMacro{\<}
140% Saying \syntax{"\\<"<text>">" is the same as "\\synt{"<text>"}"}; this
141% is a simple abbreviation.
142%
143% \DescribeMacro{\smallf}
144% Saying \syntax{"\\smallf" <number>"/"<number>} typesets a little fraction,
145% like this: \smallf 3/4. It's useful when you want to say that the default
146% value of a length is 2 \smallf 1/2\,pt, or something like that.
147%
148%
149% \subsection{Customisation}
150%
151% You can customise the way that the package documentation looks by writing
152% a file called |mdwtools.cfg|. You can redefine various commands (before
153% they're defined here, even; |mdwtools.tex| checks most of the commands that
154% it defines to make sure they haven't been defined already.
155%
156% \DescribeMacro{\indexing}
157% If you don't want the prompt about whether to generate index files, you
158% can define the |\indexing| command to either \lit{y} or \lit{n}. I'd
159% recommend that you use |\providecommand| for this, to allow further
160% customisation from the command line.
161%
162% \DescribeMacro{\mdwdateformat}
163% If you don't like my date format (maybe you're American or something),
164% you can redefine the |\mdwdateformat| command. It takes three arguments:
165% the year, month and date, as numbers; it should expand to something which
166% typesets the date nicely. The default format gives something like
167% `10 May 1996'. You can produce something rather more exotic, like
168% `10\textsuperscript{th} May \textsc{\romannumeral 1996}' by saying
169%\begin{listing}
170%\newcommand{\mdwdateformat}[3]{%
171% \number#3\textsuperscript{\numsuffix{#3}}\ %
172% \monthname{#2}\ %
173% \textsc{\romannumeral #1}%
174%}
175%\end{listing}
176% \DescribeMacro{\monthname}
177% \DescribeMacro{\numsuffix}
178% Saying \syntax{"\\monthname{"<number>"}"} expands to the name of the
179% numbered month (which can be useful when doing date formats). Saying
180% \syntax{"\\numsuffix{"<number>"}"} will expand to the appropriate suffix
181% (`th' or `rd' or whatever) for the \<number>. You'll have to superscript
182% it yourself, if this is what you want to do. Putting the year number
183% in roman numerals is just pretentious |;-)|.
184%
185% \DescribeMacro{\mdwhook}
186% After all the declarations in |mdwtools.tex|, the command |\mdwhook| is
187% executed, if it exists. This can be set up by the configuration file
188% to do whatever you want.
189%
190% There are lots of other things you can play with; you should look at the
191% implementation section to see what's possible.
192%
193% \implementation
194%
195% \section{Implementation}
196%
197% \begin{macrocode}
198%<*mdwtools>
199% \end{macrocode}
200%
201% The first thing is that I'm not a \LaTeX\ package or anything official
202% like that, so I must enable `|@|' as a letter by hand.
203%
204% \begin{macrocode}
205\makeatletter
206% \end{macrocode}
207%
208% Now input the user's configuration file, if it exists. This is fairly
209% simple stuff.
210%
211% \begin{macrocode}
212\@input{mdwtools.cfg}
213% \end{macrocode}
214%
215% Well, that's the easy bit done.
216%
217%
218% \subsection{Initialisation}
219%
220% Obviously the first thing to do is to obtain a document class. Obviously,
221% it would be silly to do this if a document class has already been loaded,
222% either by the package documentation or by the configuration file.
223%
224% The only way I can think of for finding out if a document class is already
225% loaded is by seeing if the |\documentclass| command has been redefined
226% to raise an error. This isn't too hard, really.
227%
ba741fe9
MW
228% If my \package{strayman} document class is available, then I'd prefer to
229% use that.
230%
86f6a31e 231% \begin{macrocode}
232\ifx\documentclass\@twoclasseserror\else
ba741fe9
MW
233 \IfFileExists{strayman.cls}
234 {\documentclass[a4paper]{strayman}}
235 {\documentclass[a4paper]{ltxdoc}}
86f6a31e 236 \ifx\doneclasses\mdw@undefined\else\doneclasses\fi
237\fi
238% \end{macrocode}
239%
ba741fe9
MW
240% If I can use better fonts, then that would be nice.
241%
242% \begin{macrocode}
243\usepackage[T1]{fontenc}
244\IfFileExists{mdwfonts.sty}
245 {\usepackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}}{}
246% \end{macrocode}
247%
86f6a31e 248% As part of my standard environment, I'll load some of my more useful
249% packages. If they're already loaded (possibly with different options),
250% I'll not try to load them again.
251%
252% \begin{macrocode}
253\@ifpackageloaded{doc}{}{\usepackage{doc}}
254\@ifpackageloaded{syntax}{}{\usepackage[rounded]{syntax}}
255\@ifpackageloaded{sverb}{}{\usepackage{sverb}}
256% \end{macrocode}
257%
ba741fe9
MW
258% If I'm not using the \package{ltxdoc} document class then I'll need some of
259% its definitions. I've no idea why these aren't part of \package{doc}\ldots
86f6a31e 260%
ba741fe9
MW
261% \begin{macro}{\cmd}
262% \begin{macro}{\cs}
263% \begin{macrocode}
264\def\cmd#1{\expandafter\cmd@i\string#1\x}
265\def\cmd@i#1#2\x{\cs{#2}}
266\DeclareRobustCommand\cs[1]{\texttt{\char`\\#1}}
267% \end{macrocode}
268% \end{macro}
269% \end{macro}
270%
271% \begin{macro}{\marg}
272% \begin{macro}{\oarg}
273% \begin{macro}{\parg}
274% \begin{macrocode}
275\def\@arg#1#2#3{\texttt{#1}\meta{#2}\texttt{#3}}
276\def\marg#1{\@arg{\char`\{}{#1}{\char`\}}}
277\def\oarg#1{\@arg{[}{#1}{]}}
278\def\parg#1{\@arg{(}{#1}{)}}
279% \end{macrocode}
280% \end{macro}
281% \end{macro}
282% \end{macro}
283
86f6a31e 284% \subsection{Some macros for interaction}
285%
286% I like the \LaTeX\ star-boxes, although it's a pain having to cope with
287% \TeX's space-handling rules. I'll define a new typing-out macro which
288% makes spaces more significant, and has a $*$-version which doesn't put
289% a newline on the end, and interacts prettily with |\read|.
290%
291% First of all, I need to make spaces active, so I can define things about
292% active spaces.
293%
294% \begin{macrocode}
295\begingroup\obeyspaces
296% \end{macrocode}
297%
298% Now to define the main macro. This is easy stuff. Spaces must be
299% carefully rationed here, though.
300%
301% I'll start a group, make spaces active, and make spaces expand to ordinary
302% space-like spaces. Then I'll look for a star, and pass either |\message|
303% (which doesn't start a newline, and interacts with |\read| well) or
304% |\immediate\write 16| which does a normal write well.
305%
306% \begin{macrocode}
307\gdef\mdwtype{%
308\begingroup\catcode`\ \active\let \space%
309\@ifstar{\mdwtype@i{\message}}{\mdwtype@i{\immediate\write\sixt@@n}}%
310}
311\endgroup
312% \end{macrocode}
313%
314% Now for the easy bit. I have the thing to do, and the thing to do it to,
315% so do that and end the group.
316%
317% \begin{macrocode}
318\def\mdwtype@i#1#2{#1{#2}\endgroup}
319% \end{macrocode}
320%
321%
322% \subsection{Decide on indexing}
323%
324% A configuration file can decide on indexing by defining the |\indexing|
325% macro to either \lit{y} or \lit{n}. If it's not set, then I'll prompt
326% the user.
327%
328% First of all, I want a switch to say whether I'm indexing.
329%
330% \begin{macrocode}
331\newif\ifcreateindex
332% \end{macrocode}
333%
334% Right: now I need to decide how to make progress. If the macro's not set,
335% then I want to set it, and start a row of stars.
336%
337% \begin{macrocode}
338\ifx\indexing\@@undefined
339 \mdwtype{*****************************}
340 \def\indexing{?}
341\fi
342% \end{macrocode}
343%
344% Now enter a loop, asking the user whether to do indexing, until I get
345% a sensible answer.
346%
347% \begin{macrocode}
348\loop
349 \@tempswafalse
350 \if y\indexing\@tempswatrue\createindextrue\fi
351 \if Y\indexing\@tempswatrue\createindextrue\fi
352 \if n\indexing\@tempswatrue\createindexfalse\fi
353 \if N\indexing\@tempswatrue\createindexfalse\fi
354 \if@tempswa\else
355 \mdwtype*{* Create index files? (y/n) *}
356 \read\sixt@@n to\indexing%
357\repeat
358% \end{macrocode}
359%
360% Now, based on the results of that, display a message about the indexing.
361%
362% \begin{macrocode}
363\mdwtype{*****************************}
364\ifcreateindex
365 \mdwtype{* Creating index files *}
366 \mdwtype{* This may take some time *}
367\else
368 \mdwtype{* Not creating index files *}
369\fi
370\mdwtype{*****************************}
371% \end{macrocode}
372%
373% Now I can play with the indexing commands of the \package{doc} package
374% to do whatever it is that the user wants.
375%
376% \begin{macrocode}
377\ifcreateindex
378 \CodelineIndex
379 \EnableCrossrefs
380\else
381 \CodelineNumbered
382 \DisableCrossrefs
383\fi
384% \end{macrocode}
385%
386% And register lots of plain \TeX\ things which shouldn't be indexed.
387% This contains lots of |\if|\dots\ things which don't fit nicely in
388% conditionals, which is a shame. Still, it doesn't matter that much,
389% really.
390%
391% \begin{macrocode}
392\DoNotIndex{\def,\long,\edef,\xdef,\gdef,\let,\global}
393\DoNotIndex{\if,\ifnum,\ifdim,\ifcat,\ifmmode,\ifvmode,\ifhmode,%
394 \iftrue,\iffalse,\ifvoid,\ifx,\ifeof,\ifcase,\else,\or,\fi}
395\DoNotIndex{\box,\copy,\setbox,\unvbox,\unhbox,\hbox,%
396 \vbox,\vtop,\vcenter}
397\DoNotIndex{\@empty,\immediate,\write}
398\DoNotIndex{\egroup,\bgroup,\expandafter,\begingroup,\endgroup}
399\DoNotIndex{\divide,\advance,\multiply,\count,\dimen}
400\DoNotIndex{\relax,\space,\string}
401\DoNotIndex{\csname,\endcsname,\@spaces,\openin,\openout,%
402 \closein,\closeout}
403\DoNotIndex{\catcode,\endinput}
404\DoNotIndex{\jobname,\message,\read,\the,\m@ne,\noexpand}
405\DoNotIndex{\hsize,\vsize,\hskip,\vskip,\kern,\hfil,\hfill,\hss}
406\DoNotIndex{\m@ne,\z@,\z@skip,\@ne,\tw@,\p@}
407\DoNotIndex{\dp,\wd,\ht,\vss,\unskip}
408% \end{macrocode}
409%
410% Last bit of indexing stuff, for now: I'll typeset the index in two columns
411% (the default is three, which makes them too narrow for my tastes).
412%
413% \begin{macrocode}
414\setcounter{IndexColumns}{2}
415% \end{macrocode}
416%
417%
418% \subsection{Selectively defining things}
419%
420% I don't want to tread on anyone's toes if they redefine any of these
421% commands and things in a configuration file. The following definitions
422% are fairly evil, but should do the job OK.
423%
424% \begin{macro}{\@gobbledef}
425%
426% This macro eats the following |\def|inition, leaving not a trace behind.
427%
428% \begin{macrocode}
429\def\@gobbledef#1#{\@gobble}
430% \end{macrocode}
431%
432% \end{macro}
433%
434% \begin{macro}{\tdef}
435% \begin{macro}{\tlet}
436%
437% The |\tdef| command is a sort of `tentative' definition -- it's like
438% |\def| if the control sequence named doesn't already have a definition.
439% |\tlet| does the same thing with |\let|.
440%
441% \begin{macrocode}
442\def\tdef#1{
443 \ifx#1\@@undefined%
444 \expandafter\def\expandafter#1%
445 \else%
446 \expandafter\@gobbledef%
447 \fi%
448}
449\def\tlet#1#2{\ifx#1\@@undefined\let#1=#2\fi}
450% \end{macrocode}
451%
452% \end{macro}
453% \end{macro}
454%
455%
456% \subsection{General markup things}
457%
458% Now for some really simple things. I'll define how to typeset package
459% names and environment names (both in the sans serif font, for now).
460%
461% \begin{macrocode}
5be706c3
MW
462\tdef\package{\textsf}
463\tdef\env{\textsf}
86f6a31e 464% \end{macrocode}
465%
466% I'll define the |\<|\dots|>| shortcut for syntax items suggested in the
467% \package{syntax} package.
468%
469% \begin{macrocode}
470\tdef\<#1>{\synt{#1}}
471% \end{macrocode}
472%
473% And because it's used in a few places (mainly for typesetting lengths),
474% here's a command for typesetting fractions in text.
475%
476% \begin{macrocode}
477\tdef\smallf#1/#2{\ensuremath{^{#1}\!/\!_{#2}}}
478% \end{macrocode}
479%
480%
78cdb9cc
MW
481% \subsection{Custom description lists}
482%
483% For some bizarre reason, the \LaTeX\ \env{description} environment sets
484% |\itemindent| so that the label starts |\labelsep| into the left margin,
485% and the default |\makelabel| must therefore contain a hack to compensate.
486% This is fixed in the \package{strayman} document class, and by the
487% \package{mdwlist} package in this collection. But this introduces a
488% problem: if I want to set a \env{description} list with custom labels, how
489% can I do this without messing up the spacing?
490%
491% Detection of the relevant packages is done in an awfully hacky way, because
492% \LaTeXe\ seems to go out of its way to forget which packages have been
493% loaded at |\begin{document}| time.
494%
495% \begin{macrocode}
496\def\setdescriptionlabel#1{%
497 \if1\ifx\sectindent\xxundefined% strayman?
498 \ifx\defaultdesc\xxundefined% mdwlist?
499 1\else0\fi\else0\fi%
500 \def\makelabel##1{\hskip\labelsep\relax#1}%
501 \else%
502 \def\makelabel##1{#1}%
503 \fi%
504}
505% \end{macrocode}
506%
507%
86f6a31e 508% \subsection{A table environment}
509%
510% \begin{environment}{tab}
511%
512% Most of the packages don't use the (obviously perfect) \package{mdwtab}
513% package, because it's big, and takes a while to load. Here's an
514% environment for typesetting centred tables. The first (optional) argument
515% is some declarations to perform. The mandatory argument is the table
516% preamble (obviously).
517%
518% \begin{macrocode}
519\@ifundefined{tab}{%
520 \newenvironment{tab}[2][\relax]{%
521 \par\vskip2ex%
522 \centering%
523 #1%
524 \begin{tabular}{#2}%
525 }{%
526 \end{tabular}%
527 \par\vskip2ex%
528 }
529}{}
530% \end{macrocode}
531%
532% \end{environment}
533%
534%
535% \subsection{Commenting out of stuff}
536%
537% \begin{environment}{meta-comment}
538%
539% Using |\iffalse|\dots|\fi| isn't much fun. I'll define a gobbling
540% environment using the \package{sverb} stuff.
541%
542% \begin{macrocode}
543\ignoreenv{meta-comment}
544% \end{macrocode}
545%
546% \end{environment}
547%
548%
549% \subsection{Float handling}
550%
551% This gubbins will try to avoid float pages as much as possible, and (with
552% any luck) encourage floats to be put on the same pages as text.
553%
554% \begin{macrocode}
555\def\textfraction{0.1}
556\def\topfraction{0.9}
557\def\bottomfraction{0.9}
558\def\floatpagefraction{0.7}
559% \end{macrocode}
560%
561% Now redefine the default float-placement parameters to allow `here' floats.
562%
563% \begin{macrocode}
564\def\fps@figure{htbp}
565\def\fps@table{htbp}
566% \end{macrocode}
567%
568%
569% \subsection{Other bits of parameter tweaking}
570%
571% Make \env{grammar} environments look pretty, by indenting the left hand
572% sides by a large amount.
573%
574% \begin{macrocode}
575\grammarindent1in
576% \end{macrocode}
577%
578% I don't like being told by \TeX\ that my paragraphs are hard to linebreak:
579% I know this already. This lot should shut \TeX\ up about most problems.
580%
581% \begin{macrocode}
582\sloppy
583\hbadness\@M
584\hfuzz10\p@
585% \end{macrocode}
586%
587% Also make \TeX\ shut up in the index. The \package{multicol} package
588% irritatingly plays with |\hbadness|. This is the best hook I could find
589% for playing with this setting.
590%
591% \begin{macrocode}
592\expandafter\def\expandafter\IndexParms\expandafter{%
593 \IndexParms%
594 \hbadness\@M%
595}
596% \end{macrocode}
597%
598% The other thing I really don't like is `Marginpar moved' warnings. This
599% will get rid of them, and lots of other \LaTeX\ warnings at the same time.
600%
601% \begin{macrocode}
602\let\@latex@warning@no@line\@gobble
603% \end{macrocode}
604%
605% Put some extra space between table rows, please.
606%
607% \begin{macrocode}
608\def\arraystretch{1.2}
609% \end{macrocode}
610%
611% Most of the code is at guard level one, so typeset that in upright text.
612%
613% \begin{macrocode}
614\setcounter{StandardModuleDepth}{1}
615% \end{macrocode}
616%
617%
618% \subsection{Contents handling}
619%
620% I use at least one contents file (the main table of contents) although
621% I may want more. I'll keep a list of contents files which I need to
622% handle.
623%
624% There are two things I need to do to contents files here:
625% \begin{itemize}
626% \item I must typeset the table of contents at the beginning of the
627% document; and
628% \item I want to typeset tables of contents in two columns (using the
629% \package{multicol} package).
630% \end{itemize}
631%
632% The list consists of items of the form
633% \syntax{"\\do{"<extension>"}{"<command>"}"}, where \<extension> is the
634% file extension of the contents file, and \<command> is the command to
635% typeset it.
636%
637% \begin{macro}{\docontents}
638%
639% This is where I keep the list of contents files. I'll initialise it to
640% just do the standard contents table.
641%
642% \begin{macrocode}
643\def\docontents{\do{toc}{\tableofcontents}}
644% \end{macrocode}
645%
646% \end{macro}
647%
648% \begin{macro}{\addcontents}
649%
650% By saying \syntax{"\\addcontents{"<extension>"}{"<command>"}"}, a document
651% can register a new table of contents which gets given the two-column
652% treatment properly. This is really easy to implement.
653%
654% \begin{macrocode}
655\def\addcontents#1#2{%
656 \toks@\expandafter{\docontents\do{#1}{#2}}%
657 \edef\docontents{\the\toks@}%
658}
659% \end{macrocode}
660%
661% \end{macro}
662%
663%
664% \subsection{Finishing it all off}
665%
666% \begin{macro}{\finalstuff}
667%
668% The |\finalstuff| macro is a hook for doing things at the end of the
669% document. Currently, it inputs the licence agreement as an appendix.
670%
671% \begin{macrocode}
672\tdef\finalstuff{\appendix\part*{Appendix}\input{gpl}}
673% \end{macrocode}
674%
675% \end{macro}
676%
677% \begin{macro}{\implementation}
678%
679% The |\implementation| macro starts typesetting the implementation of
680% the package(s). If we're not doing the implementation, it just does
681% this lot and ends the input file.
682%
683% I define a macro with arguments inside the |\StopEventually|, which causes
684% problems, since the code gets put through an extra level of |\def|fing
685% depending on whether the implementation stuff gets typeset or not. I'll
686% store the code I want to do in a separate macro.
687%
688% \begin{macrocode}
689\def\implementation{\StopEventually{\attheend}}
690% \end{macrocode}
691%
692% Now for the actual activity. First, I'll do the |\finalstuff|. Then, if
693% \package{doc}'s managed to find the \package{multicol} package, I'll add
694% the end of the environment to the end of each contents file in the list.
695% Finally, I'll read the index in from its formatted |.ind| file.
696%
697% \begin{macrocode}
698\tdef\attheend{%
699 \finalstuff%
700 \ifhave@multicol%
701 \def\do##1##2{\addtocontents{##1}{\protect\end{multicols}}}%
702 \docontents%
703 \fi%
c55b9cc8 704 \ifx\backmatter\@@undefined\else\backmatter\fi%
86f6a31e 705 \PrintIndex%
706}
707% \end{macrocode}
708%
709% \end{macro}
710%
711%
712% \subsection{File version information}
713%
714% \begin{macro}{\mdwpkginfo}
715%
716% For setting up the automatic titles, I'll need to be able to work out
717% file versions and things. This macro will, given a file name, extract
718% from \LaTeX\ the version information and format it into a sensible string.
719%
720% First of all, I'll put the original string (direct from the
721% |\Provides|\dots\ command). Then I'll pass it to another macro which can
722% parse up the string into its various bits, along with the original
723% filename.
724%
725% \begin{macrocode}
726\def\mdwpkginfo#1{%
727 \edef\@tempa{\csname ver@#1\endcsname}%
728 \expandafter\mdwpkginfo@i\@tempa\@@#1\@@%
729}
730% \end{macrocode}
731%
732% Now for the real business. I'll store the string I build in macros called
733% \syntax{"\\"<filename>"date", "\\"<filename>"version" and
734% "\\"<filename>"info"}, which store the file's date, version and
735% `information string' respectively. (Note that the file extension isn't
736% included in the name.)
737%
738% This is mainly just tedious playing with |\expandafter|. The date format
739% is defined by a separate macro, which can be modified from the
740% configuration file.
741%
742% \begin{macrocode}
743\def\mdwpkginfo@i#1/#2/#3 #4 #5\@@#6.#7\@@{%
744 \expandafter\def\csname #6date\endcsname%
745 {\protect\mdwdateformat{#1}{#2}{#3}}%
746 \expandafter\def\csname #6version\endcsname{#4}%
747 \expandafter\def\csname #6info\endcsname{#5}%
748}
749% \end{macrocode}
750%
751% \end{macro}
752%
753% \begin{macro}{\mdwdateformat}
754%
755% Given three arguments, a year, a month and a date (all numeric), build a
756% pretty date string. This is fairly simple really.
757%
758% \begin{macrocode}
759\tdef\mdwdateformat#1#2#3{\number#3\ \monthname{#2}\ \number#1}
760\def\monthname#1{%
761 \ifcase#1\or%
762 January\or February\or March\or April\or May\or June\or%
763 July\or August\or September\or October\or November\or December%
764 \fi%
765}
766\def\numsuffix#1{%
767 \ifnum#1=1 st\else%
768 \ifnum#1=2 nd\else%
769 \ifnum#1=3 rd\else%
770 \ifnum#1=21 st\else%
771 \ifnum#1=22 nd\else%
772 \ifnum#1=23 rd\else%
773 \ifnum#1=31 st\else%
774 th%
775 \fi\fi\fi\fi\fi\fi\fi%
776}
777% \end{macrocode}
778%
779% \end{macro}
780%
781% \begin{macro}{\mdwfileinfo}
782%
783% Saying \syntax{"\\mdwfileinfo{"<file-name>"}{"<info>"}"} extracts the
784% wanted item of \<info> from the version information for file \<file-name>.
785%
786% \begin{macrocode}
787\def\mdwfileinfo#1#2{\mdwfileinfo@i{#2}#1.\@@}
788\def\mdwfileinfo@i#1#2.#3\@@{\csname#2#1\endcsname}
789% \end{macrocode}
790%
791% \end{macro}
792%
793%
794% \subsection{List handling}
795%
796% There are several other lists I need to build. These macros will do
797% the necessary stuff.
798%
799% \begin{macro}{\mdw@ifitem}
800%
801% The macro \syntax{"\\mdw@ifitem"<item>"\\in"<list>"{"<true-text>"}"^^A
802% "{"<false-text>"}"} does \<true-text> if the \<item> matches any item in
803% the \<list>; otherwise it does \<false-text>.
804%
805% \begin{macrocode}
806\def\mdw@ifitem#1\in#2{%
807 \@tempswafalse%
808 \def\@tempa{#1}%
809 \def\do##1{\def\@tempb{##1}\ifx\@tempa\@tempb\@tempswatrue\fi}%
810 #2%
811 \if@tempswa\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi%
812}
813% \end{macrocode}
814%
815% \end{macro}
816%
817% \begin{macro}{\mdw@append}
818%
819% Saying \syntax{"\\mdw@append"<item>"\\to"<list>} adds the given \<item>
820% to the end of the given \<list>.
821%
822% \begin{macrocode}
823\def\mdw@append#1\to#2{%
824 \toks@{\do{#1}}%
825 \toks\tw@\expandafter{#2}%
826 \edef#2{\the\toks\tw@\the\toks@}%
827}
828% \end{macrocode}
829%
830% \end{macro}
831%
832% \begin{macro}{\mdw@prepend}
833%
834% Saying \syntax{"\\mdw@prepend"<item>"\\to"<list>} adds the \<item> to the
835% beginning of the \<list>.
836%
837% \begin{macrocode}
838\def\mdw@prepend#1\to#2{%
839 \toks@{\do{#1}}%
840 \toks\tw@\expandafter{#2}%
841 \edef#2{\the\toks@\the\toks\tw@}%
842}
843% \end{macrocode}
844%
845% \end{macro}
846%
847% \begin{macro}{\mdw@add}
848%
849% Finally, saying \syntax{"\\mdw@add"<item>"\\to"<list>} adds the \<item>
850% to the list only if it isn't there already.
851%
852% \begin{macrocode}
853\def\mdw@add#1\to#2{\mdw@ifitem#1\in#2{}{\mdw@append#1\to#2}}
854% \end{macrocode}
855%
856% \end{macro}
857%
858%
859% \subsection{Described file handling}
860%
861% I'l maintain lists of packages, document classes, and other files
862% described by the current documentation file.
863%
864% First of all, I'll declare the various list macros.
865%
866% \begin{macrocode}
867\def\dopackages{}
868\def\doclasses{}
869\def\dootherfiles{}
870% \end{macrocode}
871%
872% \begin{macro}{\describespackage}
873%
874% A document file can declare that it describes a package by saying
875% \syntax{"\\describespackage{"<package-name>"}"}. I add the package to
876% my list, read the package into memory (so that the documentation can
877% offer demonstrations of it) and read the version information.
878%
879% \begin{macrocode}
880\def\describespackage#1{%
881 \mdw@ifitem#1\in\dopackages{}{%
882 \mdw@append#1\to\dopackages%
883 \usepackage{#1}%
884 \mdwpkginfo{#1.sty}%
885 }%
886}
887% \end{macrocode}
888%
889% \end{macro}
890%
891% \begin{macro}{\describesclass}
892%
893% By saying \syntax{"\\describesclass{"<class-name>"}"}, a document file
894% can declare that it describes a document class. I'll assume that the
895% document class is already loaded, because it's much too late to load
896% it now.
897%
898% \begin{macrocode}
899\def\describesclass#1{\mdw@add#1\to\doclasses\mdwpkginfo{#1.cls}}
900% \end{macrocode}
901%
902% \end{macro}
903%
904% \begin{macro}{\describesfile}
905%
906% Finally, other `random' files, which don't have the status of real \LaTeX\
907% packages or document classes, can be described by saying \syntax{^^A
908% "\\describesfile{"<file-name>"}" or "\\describesfile*{"<file-name>"}"}.
909% The difference is that the starred version will not |\input| the file.
910%
911% \begin{macrocode}
912\def\describesfile{%
913 \@ifstar{\describesfile@i\@gobble}{\describesfile@i\input}%
914}
915\def\describesfile@i#1#2{%
916 \mdw@ifitem#2\in\dootherfiles{}{%
917 \mdw@add#2\to\dootherfiles%
918 #1{#2}%
919 \mdwpkginfo{#2}%
920 }%
921}
922% \end{macrocode}
923%
924% \end{macro}
925%
926%
927% \subsection{Author and title handling}
928%
929% I'll redefine the |\author| and |\title| commands so that I get told
930% whether I need to do it myself.
931%
932% \begin{macro}{\author}
933%
934% This is easy: I'll save the old meaning, and then redefine |\author| to
935% do the old thing and redefine itself to then do nothing.
936%
937% \begin{macrocode}
938\let\mdw@author\author
939\def\author{\let\author\@gobble\mdw@author}
940% \end{macrocode}
941%
942% \end{macro}
943%
944% \begin{macro}{\title}
945%
946% And oddly enough, I'll do exactly the same thing for the title, except
947% that I'll also disable the |\mdw@buildtitle| command, which constructs
948% the title automatically.
949%
950% \begin{macrocode}
951\let\mdw@title\title
952\def\title{\let\title\@gobble\let\mdw@buildtitle\relax\mdw@title}
953% \end{macrocode}
954%
955% \end{macro}
956%
957% \begin{macro}{\date}
958%
959% This works in a very similar sort of way.
960%
961% \begin{macrocode}
962\def\date#1{\let\date\@gobble\def\today{#1}}
963% \end{macrocode}
964%
965% \end{macro}
966%
967% \begin{macro}{\datefrom}
968%
969% Saying \syntax{"\\datefrom{"<file-name>"}"} sets the document date from
970% the given filename.
971%
972% \begin{macrocode}
973\def\datefrom#1{%
974 \protected@edef\@tempa{\noexpand\date{\csname #1date\endcsname}}%
975 \@tempa%
976}
977% \end{macrocode}
978%
979% \end{macro}
980%
981% \begin{macro}{\docfile}
982%
983% Saying \syntax{"\\docfile{"<file-name>"}"} sets up the file name from which
984% documentation will be read.
985%
986% \begin{macrocode}
987\def\docfile#1{%
988 \def\@tempa##1.##2\@@{\def\@basefile{##1.##2}\def\@basename{##1}}%
989 \edef\@tempb{\noexpand\@tempa#1\noexpand\@@}%
990 \@tempb%
991}
992% \end{macrocode}
993%
994% I'll set up a default value as well.
995%
996% \begin{macrocode}
997\docfile{\jobname.dtx}
998% \end{macrocode}
999%
1000% \end{macro}
1001%
1002%
1003% \subsection{Building title strings}
1004%
1005% This is rather tricky. For each list, I need to build a legible looking
1006% string.
1007%
1008% \begin{macro}{\mdw@addtotitle}
1009%
1010% By saying
1011%\syntax{"\\mdw@addtotitle{"<list>"}{"<command>"}{"<singular>"}{"<plural>"}"}
1012% I can add the contents of a list to the current title string in the
1013% |\mdw@title| macro.
1014%
1015% \begin{macrocode}
1016\tdef\mdw@addtotitle#1#2#3#4{%
1017% \end{macrocode}
1018%
1019% Now to get to work. I need to keep one `lookahead' list item, and a count
1020% of the number of items read so far. I'll keep the lookahead item in
6baaab46
MW
1021% |\@nextitem| and the counter in |\count@|. Things are even worse because
1022% the footnote symbols should appear \emph{after} the separating punctuation,
1023% so we need to delay those by another cycle, hence we have |\@nextnote| and
1024% |\@prevnote|.
86f6a31e 1025%
1026% \begin{macrocode}
1027 \count@\z@%
1028% \end{macrocode}
1029%
1030% Now I'll define what to do for each list item. The |\protect| command is
1031% already set up appropriately for playing with |\edef| commands.
1032%
1033% \begin{macrocode}
1034 \def\do##1{%
1035% \end{macrocode}
1036%
1037% The first job is to add the previous item to the title string. If this
1038% is the first item, though, I'll just add the appropriate \lit{The } or
1039% \lit{ and the } string to the title (this is stored in the |\@prefix|
bc1b2c52
MW
1040% macro). Also maintain a parallel version which doesn't have the footnotes
1041% in: this will be suitable for a running header.
86f6a31e 1042%
1043% \begin{macrocode}
1044 \edef\mdw@title{%
1045 \mdw@title%
1046 \ifcase\count@\@prefix%
1047 \or\@nextitem%
6baaab46 1048 \else,\@prevnote\ \@nextitem%
86f6a31e 1049 \fi%
1050 }%
bc1b2c52
MW
1051 \edef\mdw@runningtitle{%
1052 \mdw@runningtitle%
1053 \ifcase\count@\@prefix%
1054 \or\@nextitem%
1055 \else, \@nextitem%
1056 \fi%
1057 }%
86f6a31e 1058% \end{macrocode}
1059%
6baaab46
MW
1060% That was rather easy. Now I'll set up the |\@previtem| and |\@nextitem|
1061% macros for the next time around the loop.
86f6a31e 1062%
1063% \begin{macrocode}
6baaab46
MW
1064 \edef\@nextitem{\protect#2{##1}}%
1065 \let\@prevnote\@nextnote
1066 \edef\@nextnote{%
86f6a31e 1067 \protect\footnote{%
1068 The \protect#2{##1} #3 is currently at version %
1069 \mdwfileinfo{##1}{version}, dated \mdwfileinfo{##1}{date}.%
f06ea125 1070 }%
86f6a31e 1071 }%
1072% \end{macrocode}
1073%
1074% Finally, I need to increment the counter.
1075%
1076% \begin{macrocode}
1077 \advance\count@\@ne%
1078 }%
1079% \end{macrocode}
1080%
1081% Now execute the list.
1082%
1083% \begin{macrocode}
1084 #1%
1085% \end{macrocode}
1086%
1087% I still have one item left over, unless the list was empty. I'll add
1088% that now.
1089%
1090% \begin{macrocode}
1091 \edef\mdw@title{%
1092 \mdw@title%
1093 \ifcase\count@%
6baaab46
MW
1094 \or\@nextitem\@nextnote\space#3%
1095 \or\@prevnote\ and \@nextitem\@nextnote\space#4%
1096 \else,\@prevnote\ and \@nextitem\@nextnote\space#4%
86f6a31e 1097 \fi%
1098 }%
bc1b2c52
MW
1099 \edef\mdw@runningtitle{%
1100 \mdw@runningtitle%
1101 \ifcase\count@%
1102 \or\@nextitem\space#3%
1103 \or\ and \@nextitem\space#4%
1104 \else,\ and \@nextitem\space#4%
1105 \fi%
1106 }%
86f6a31e 1107% \end{macrocode}
1108%
1109% Finally, if $|\count@| \ne 0$, I must set |\@prefix| to \lit{ and the }.
1110%
1111% \begin{macrocode}
1112 \ifnum\count@>\z@\def\@prefix{ and the }\fi%
1113}
1114% \end{macrocode}
1115%
1116% \end{macro}
1117%
1118% \begin{macro}{\mdw@buildtitle}
1119%
1120% This macro will actually do the job of building the title string.
1121%
1122% \begin{macrocode}
1123\tdef\mdw@buildtitle{%
1124% \end{macrocode}
1125%
1126% First of all, I'll open a group to avoid polluting the namespace with
1127% my gubbins (although the code is now much tidier than it has been in
1128% earlier releases).
1129%
1130% \begin{macrocode}
1131 \begingroup%
1132% \end{macrocode}
1133%
1134% The title building stuff makes extensive use of |\edef|. I'll set
1135% |\protect| appropriately. (For those not in the know,
1136% |\@unexpandable@protect| expands to `|\noexpand\protect\noexpand|',
1137% which prevents expansion of the following macro, and inserts a |\protect|
1138% in front of it ready for the next |\edef|.)
1139%
1140% \begin{macrocode}
1141 \let\@@protect\protect\let\protect\@unexpandable@protect%
1142% \end{macrocode}
1143%
1144% Set up some simple macros ready for the main code.
1145%
1146% \begin{macrocode}
1147 \def\mdw@title{}%
bc1b2c52 1148 \def\mdw@runningtitle{}%
86f6a31e 1149 \def\@prefix{The }%
1150% \end{macrocode}
1151%
1152% Now build the title. This is fun.
1153%
1154% \begin{macrocode}
1155 \mdw@addtotitle\dopackages\package{package}{packages}%
1156 \mdw@addtotitle\doclasses\package{document class}{document classes}%
1157 \mdw@addtotitle\dootherfiles\texttt{file}{files}%
1158% \end{macrocode}
1159%
1160% Now I want to end the group and set the title from my string. The
1161% following hacking will do this.
1162%
1163% \begin{macrocode}
bc1b2c52
MW
1164 \edef\next{%
1165 \endgroup%
1166 \noexpand\title{\noexpand\mdw@titlehack\mdw@title}%
1167 \def\noexpand\@headertitle{\mdw@runningtitle}%
1168 }%
86f6a31e 1169 \next%
1170}
1171% \end{macrocode}
1172%
1173% \end{macro}
1174%
f06ea125
MW
1175% \begin{macro}{\mdw@titlehack}
1176%
1177% Wait! Did you notice that |\mdw@titlehack|? What's that about?
1178%
1179% It turns out that the default document classes hack the footnote insertion
1180% commands to make footnote symbols take up no horizontal space in the title.
1181% Apparently this makes author names look as if they're centred properly when
1182% there are affiliation footnotes. Anyway, \package{doc} perpetuates this
1183% silliness, but it makes a mess of the version markers I insert, so I must
1184% deploy countermeasures.
1185%
1186% \begin{macrocode}
1187\def\mdw@titlehack{\def\@makefnmark{$\m@th^{\@thefnmark}$}}
1188% \end{macrocode}
1189%
1190% \end{macro}
86f6a31e 1191%
1192% \subsection{Starting the main document}
1193%
1194% \begin{macro}{\mdwdoc}
1195%
1196% Once the document preamble has done all of its stuff, it calls the
1197% |\mdwdoc| command, which takes over and really starts the documentation
1198% going.
1199%
1200% \begin{macrocode}
1201\def\mdwdoc{%
1202% \end{macrocode}
1203%
1204% First, I'll construct the title string.
1205%
1206% \begin{macrocode}
1207 \mdw@buildtitle%
1208 \author{Mark Wooding}%
1209% \end{macrocode}
1210%
1211% Set up the date string based on the date of the package which shares
1212% the same name as the current file.
1213%
1214% \begin{macrocode}
1215 \datefrom\@basename%
1216% \end{macrocode}
1217%
1218% Set up verbatim characters after all the packages have started.
1219%
1220% \begin{macrocode}
1221 \shortverb\|%
1222 \shortverb\"%
1223% \end{macrocode}
1224%
1225% Start the document, and put the title in.
1226%
1227% \begin{macrocode}
1228 \begin{document}
c55b9cc8 1229 \ifx\frontmatter\@@undefined\else\frontmatter\fi%
86f6a31e 1230 \maketitle%
1231% \end{macrocode}
1232%
1233% This is nasty. It makes maths displays work properly in demo environments.
1234% \emph{The \LaTeX\ Companion} exhibits the bug which this hack fixes. So
1235% ner.
1236%
1237% \begin{macrocode}
1238 \abovedisplayskip\z@%
1239% \end{macrocode}
1240%
1241% Now start the contents tables. After starting each one, I'll make it
1242% be multicolumnar.
1243%
1244% \begin{macrocode}
1245 \def\do##1##2{%
86f6a31e 1246 \ifhave@multicol\addtocontents{##1}{%
1247 \protect\begin{multicols}{2}%
1248 \hbadness\@M%
1249 }\fi%
b140714e 1250 ##2%
86f6a31e 1251 }%
1252 \docontents%
1253% \end{macrocode}
1254%
1255% Input the main file now.
1256%
1257% \begin{macrocode}
c55b9cc8 1258 \ifx\mainmatter\@@undefined\else\mainmatter\fi%
86f6a31e 1259 \DocInput{\@basefile}%
1260% \end{macrocode}
1261%
1262% That's it. I'm done.
1263%
1264% \begin{macrocode}
1265 \end{document}
1266}
1267% \end{macrocode}
1268%
1269% \end{macro}
1270%
1271%
1272% \subsection{And finally\dots}
1273%
1274% Right at the end I'll put a hook for the configuration file.
1275%
1276% \begin{macrocode}
1277\ifx\mdwhook\@@undefined\else\expandafter\mdwhook\fi
1278% \end{macrocode}
1279%
1280% That's all the code done now. I'll change back to `user' mode, where
1281% all the magic control sequences aren't allowed any more.
1282%
1283% \begin{macrocode}
1284\makeatother
1285%</mdwtools>
1286% \end{macrocode}
1287%
1288% Oh, wait! What if I want to typeset this documentation? Aha. I'll cope
1289% with that by comparing |\jobname| with my filename |mdwtools|. However,
1290% there's some fun here, because |\jobname| contains category-12 letters,
1291% while my letters are category-11. Time to play with |\string| in a messy
1292% way.
1293%
1294% \begin{macrocode}
1295%<*driver>
1296\makeatletter
1297\edef\@tempa{\expandafter\@gobble\string\mdwtools}
1298\edef\@tempb{\jobname}
1299\ifx\@tempa\@tempb
1300 \describesfile*{mdwtools.tex}
1301 \docfile{mdwtools.tex}
1302 \makeatother
1303 \expandafter\mdwdoc
1304\fi
1305\makeatother
1306%</driver>
1307% \end{macrocode}
1308%
1309% That's it. Done!
1310%
1311% \hfill Mark Wooding, \today
1312%
1313% \Finale
1314%
1315\endinput