3 % $Id: mdwlist.dtx,v 1.2 2003/09/05 16:13:44 mdw Exp $
5 % Various list-related things
7 % (c) 1996 Mark Wooding
9 %----- Revision history -----------------------------------------------------
11 % $Log: mdwlist.dtx,v $
12 % Revision 1.2 2003/09/05 16:13:44 mdw
13 % Define `note' conditionally, to avoid clashes with `mdwthm' among others.
15 % Revision 1.1 2002/02/03 20:49:03 mdw
16 % Checkin for new build system.
18 % Revision 1.1 1996/11/19 20:52:26 mdw
24 % \begin{meta-comment} <general public licence>
26 %% mdwlist package -- various list-related things
27 %% Copyright (c) 1996 Mark Wooding
29 %% This program is free software; you can redistribute it and/or modify
30 %% it under the terms of the GNU General Public License as published by
31 %% the Free Software Foundation; either version 2 of the License, or
32 %% (at your option) any later version.
34 %% This program is distributed in the hope that it will be useful,
35 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
36 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 %% GNU General Public License for more details.
39 %% You should have received a copy of the GNU General Public License
40 %% along with this program; if not, write to the Free Software
41 %% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 % \begin{meta-comment} <Package preambles>
46 %<+package>\NeedsTeXFormat{LaTeX2e}
47 %<+package>\ProvidesPackage{mdwlist}
48 %<+package> [1996/05/02 1.1 Various list-related things]
53 %% {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
54 %% 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
55 %% Digits \0\1\2\3\4\5\6\7\8\9
56 %% Exclamation \! Double quote \" Hash (number) \#
57 %% Dollar \$ Percent \% Ampersand \&
58 %% Acute accent \' Left paren \( Right paren \)
59 %% Asterisk \* Plus \+ Comma \,
60 %% Minus \- Point \. Solidus \/
61 %% Colon \: Semicolon \; Less than \<
62 %% Equals \= Greater than \> Question mark \?
63 %% Commercial at \@ Left bracket \[ Backslash \\
64 %% Right bracket \] Circumflex \^ Underscore \_
65 %% Grave accent \` Left brace \{ Vertical bar \|
66 %% Right brace \} Tilde \~}
69 % \begin{meta-comment}
73 \describespackage{mdwlist}
75 \desclabelwidth{80pt}%
76 \desclabelstyle\nextlinelabel%
77 \def\makelabel{\bfseries}%
79 \newenvironment{cmdlist}
80 {\basedescript{\let\makelabel\cmd}}
87 % \section{User guide}
89 % This package provides some vaguely useful list-related commands and
92 % \item A way of building \env{description}-like environments.
93 % \item Commands for making `compacted' versions of list environments
94 % \item A method for suspending and resuming enumerated lists.
97 % \subsection{Description list handling}
99 % Different sorts of description-type lists require different sorts of
100 % formatting: I think that's fairly obvious. There are essentially three
101 % different attributes which should be changable:
103 % \item the indentation of the items being described,
104 % \item the handling of labels which don't fit properly, and
105 % \item the style used to typeset the label text.
107 % The first two items should usually be decided for all description-like
108 % lists in the document, to ensure consistency of appearance. The last
109 % depends much more on the content of the labels.
111 % \DescribeEnv{basedescript}
112 % The \env{basedescript} environment acts as a `skeleton' for description
113 % environments. It takes one argument, which contains declarations to
114 % be performed while constructing the list. I'd consider it unusual for
115 % the \env{basedescript} environment to be used in the main text: it's
116 % intended to be used to build other environments.
118 % The declarations which can be used to define description-type environments
119 % include all of those which are allowed when setting up a list (see the
120 % \LaTeX\ book for information here). Some others, which apply specifically
121 % to description lists, are also provided:
125 % \item \DescribeMacro{\desclabelwidth}
126 % The \syntax{"\\desclabelwidth{"<length>"}"} declaration sets labels
127 % to be left-aligned, with a standard width of \<length>; the item
128 % text is indented by \<length> plus the value of |\labelsep|.
130 % \item \DescribeMacro{\desclabelstyle}
131 % The label style determines how overlong labels are typeset. A style
132 % may be set using the \syntax{"\\desclabelstyle{"<style>"}"}
133 % declaration. The following \<style>s are provided:
135 % \item [\nextlinelabel] If the label is too wide to fit next to the
136 % first line of text, then it is placed on a line by itself;
137 % the main text is started on the next line with the usual
139 % \item [\multilinelabel] The label is typeset in a parbox with the
140 % appropriate width; if it won't fit on one line, then the
141 % text will be split onto subsequent lines.
142 % \item [\pushlabel] If the label is too wide to fit in the space
143 % allocated to it, the start of the item's text will be `pushed'
144 % over to the right to provide space for the label. This is
145 % the standard \LaTeX\ \env{description} behaviour.
148 % \item \DescribeMacro{\makelabel}
149 % The |\makelabel| command is responsible for typesetting a label.
150 % It is given one argument, which is the text given as an argument
151 % to the |\item| command; it should typeset it appropriately. The
152 % text will then be arranged appropriately according to the chosen
153 % label style. This command should be redefined using |\renewcommand|.
158 % \begin{demo}[w]{Various labelling styles}
159 %\begin{basedescript}{\desclabelstyle{\nextlinelabel}}
160 %\item [Short label] This is a short item, although it has quite a
161 % lot of text attached to it.
162 %\item [Slightly longer label text] This is a rather longer piece
163 % of text, with a correspondingly slightly longer label.
166 %\begin{basedescript}{\desclabelstyle{\multilinelabel}}
167 %\item [Short label] This is a short item, although it has quite a
168 % lot of text attached to it.
169 %\item [Slightly longer label text] This is a rather longer piece
170 % of text, with a correspondingly slightly longer label.
173 %\begin{basedescript}{\desclabelstyle{\pushlabel}}
174 %\item [Short label] This is a short item, although it has quite a
175 % lot of text attached to it.
176 %\item [Slightly longer label text] This is a rather longer piece
177 % of text, with a correspondingly slightly longer label.
182 % \DescribeMacro{\defaultdesc}
183 % To allow document designers to control the global appearance of description
184 % lists, the |\defaultdesc| command may be redefined; it is called while
185 % setting up a new \env{basedescript} list, before performing the user's
186 % declarations. By default, it attempts to emulate the standard \LaTeX\
187 % \env{description} environment:\footnote{^^A
188 % This is a slightly sanitised version of the real definition, which is
189 % given in the implementation section of this document.}
191 %\providecommand{\defaultdesc}{%
192 % \desclabelstyle{\pushlabel}%
193 % \renewcommand{\makelabel}[1]{\bfseries##1}%
194 % \setlength{\labelwidth}{0pt}%
197 % Unfortunately, \LaTeX\ doesn't provide a means for overriding a command
198 % which may or may not have been defined yet; in this case, I'd probably
199 % recommend using the \TeX\ primitive |\def| to redefine |\defaultdesc|.
201 % If you want to redefine the \env{description} environment in terms of
202 % the commands in this package, the following method is recommended:
204 %\renewenvironment{description}{%
205 % \begin{basedescript}{%
206 % \renewcommand{\makelabel}[1]{\bfseries##1}%
209 % \end{basedescript}%
212 % This ensures that labels are typeset in bold, as is usual, but other
213 % properties of the list are determined by the overall document style.
215 % \subsection{Compacted lists}
217 % \LaTeX\ tends to leave a certain amount of vertical space between list
218 % items. While this is normally correct for lists in which the items are
219 % several lines long, it tends to look odd if all or almost all the items
220 % are only one line long.
222 % \DescribeMacro{\makecompactlist}
224 % \syntax{"\\makecompactlist{"<new-env-name>"}{"<old-env-name>"}"}
225 % defines a new environment \<new-env-name> to be a `compacted' version of
226 % the existing environment \<old-env-name>; i.e., the two environments are
227 % the same except that the compacted version leaves no space between items
228 % or paragraphs within the list.
230 % \DescribeEnv{itemize*}
231 % \DescribeEnv{enumerate*}
232 % \DescribeEnv{description*}
233 % So that the most common cases are already handled, the package creates
234 % compacted $*$-variants of the \env{itemize}, \env{enumerate} and
235 % \env{description} environments. These were created using the commands
237 %\makecompactlist{itemize*}{itemize}
238 %\makecompactlist{enumerate*}{enumerate}
239 %\makecompactlist{description*}{description}
242 % Some list environments accept arguments. You can pass an argument to a
243 % list environment using an optional argument to its compact variant. For
246 %\begin{foolist*}[{someargument}]
249 % \subsection{Suspending and resuming list environments}
251 % \DescribeMacro{\suspend}
252 % \DescribeMacro{\resume}
253 % The |\suspend| and |\resume| commands allow you to temporarily end a list
254 % environment and then pick it up where you left off. The syntax is fairly
259 % <suspend-cmd> ::= \[[
261 % \begin{stack} \\ "[" <name> "]" \end{stack} "{" <env-name> "}"
264 % <resume-cmd> ::= \[[
266 % \begin{stack} \\ "[" <name> "]" \end{stack} "{" <env-name> "}"
267 % \begin{stack} \\ "[" <text> "]" \end{stack}
272 % The \<env-name> is the name of the environment; this will more often than
273 % not be the \env{enumerate} environment. The \<name> is a magic name you
274 % can use to identify the suspended environment; if you don't specify this,
275 % the environment name is used instead.
277 % \begin{demo}{Suspended environments}
278 %Here's some initial text. It's
279 %not very interesting.
281 %\item This is an item.
282 %\item This is another.
283 %\suspend{enumerate*}
284 %Some more commentry text.
290 % You can pass arguments to a resumed list environment through the second
291 % optional argument of the |\resume| command. If, for example, you're using
292 % David Carlisle's \package{enumerate} package, you could say something like
294 %\begin{enumerate}[\bfseries{Item} i]
298 %Some intervening text.
299 %\resume{enumerate}[{[\bfseries{Item} i]}]
300 %\item Yet another item
306 % \section{Implementation}
312 % \subsection{Description lists}
314 % \subsubsection{Label styles}
316 % \begin{macro}{\nextlinelabel}
318 % The idea here is that if the label is too long to fit in its box, we put
319 % it on its own line and start the text of the item on the next. I've
320 % used |\sbox| here to capture colour changes properly, even though I have
321 % deep moral objections to the use of \LaTeX\ boxing commands. Anyway,
322 % I capture the text in box~0 and compare its width to the amount of space
323 % I have in the label box. If there's enough, I can just unbox the box;
324 % otherwise I build a vbox containing the label text and an empty hbox --
325 % |\baselineskip| glue inserted between the two boxes makes sure we get
326 % the correct spacing between the two lines, and the vboxness of the vbox
327 % ensures that the baseline of my strange thing is the baseline of the
328 % \emph{bottom} box. I then bash the vbox on the nose, so as to make its
329 % width zero, and leave that as the result. Either way, I then add glue
330 % to left align whatever it is I've created.
333 \def\nextlinelabel#1{%
335 \ifdim\wd\z@>\labelwidth%
336 \setbox\z@\vbox{\box\z@\hbox{}}%
348 % \begin{macro}{\multilinelabel}
350 % A different idea -- make the label text wrap around onto the next line if
351 % it's too long. This is really easy, actually. I use a parbox to contain
352 % the label text, set to be ragged right, because there won't be enough
353 % space to do proper justification. There's also a funny hskip there --
354 % this is because \TeX\ only hyphenates things it finds sitting \emph{after}
355 % glue items. The parbox is top-aligned, so the label text and the item
356 % run downwards together. I put the result in box~0, and remove the depth,
357 % so as not to make the top line of the item text look really strange.
359 % All this leaves a little problem, though: if the item text isn't very long,
360 % the label might go further down the page than the main item, and possibly
361 % collide with the label below. I must confess that I'm not actually sure
362 % how to deal with this possibility, so I just hope it doesn't happen.
364 % By the way, I don't have moral objections to |\parbox|.
367 \def\multilinelabel#1{%
369 \parbox[t]\labelwidth{\raggedright\hskip\z@skip#1}%
379 % \begin{macro}{\pushlabel}
381 % Now we implement the old style behaviour -- if the label is too wide, we
382 % just push the first line of the item further over to the right. This
383 % is really very easy indeed -- we just stick some |\hfil| space on the
384 % right hand side (to left align if the label comes up too short). The
385 % `push' behaviour is handled automatically by \LaTeX's item handling.
388 \def\pushlabel#1{{#1}\hfil}
393 % \subsubsection{The main environment}
395 % \begin{macro}{\desclabelstyle}
397 % This is a declaration intended to be used only in the argument to the
398 % \env{basedescript} environment. It sets the label style for the list.
399 % All we do is take the argument and assign it to a magic control sequence
400 % which \env{basedescript} will understand later.
403 \def\desclabelstyle#1{\def\desc@labelstyle{#1}}
408 % \begin{macro}{\desclabelwidth}
410 % We set the label width and various other bits of information which will
411 % make all the bits of the description line up beautifully. We set
412 % |\labelwidth| to the value we're given (using |\setlength|, so that
413 % people can use the \package{calc} package if they so wish), and make
414 % the |\leftmargin| equal $|\labelwidth|+|\labelsep|$.
417 \def\desclabelwidth#1{%
418 \setlength\labelwidth{#1}%
419 \leftmargin\labelwidth%
420 \advance\leftmargin\labelsep%
426 % \begin{environment}{basedescript}
428 % This is the new description environment. It does almost everything you
429 % could want from a description environment, I think. The argument is a
430 % collection of declarations to be performed while setting up the list.
432 % This environment isn't really intended to be used by users -- it's here
433 % so that you can define other description environments in terms of it,
435 % The environment is defined in two bits -- the `start' bit here simply
436 % starts the list and inserts the user declarations in an appropriate
437 % point, although sensible details will be inerted if the argument was
441 \def\basedescript#1{%
444 % We must start the list. If the |\item| command's optional argument is
445 % missing, we should just leave a blank space, I think.
451 % So far, so good. Now put in some default declarations. I'll use a
452 % separate macro for this, so that the global appearance of lists can be
459 % Now we do the user's declarations.
465 % Now set up the other parts of the list. We set |\itemindent| so that the
466 % label is up against the current left margin. (The standard version
467 % actually leaves the label hanging to the left of the margin by a
468 % distance of |\labelsep| for a reason I can't quite comprehend -- there's
469 % an |\hspace{\labelsep}| in the standard |\makelabel| to compensate for
470 % this. Strange\dots)
472 % To make the label start in the right place, the text of the item must
473 % start a distance of $|\labelwidth|+|\labelsep|$ from the (pre-list) left
474 % hand margin; this means that we must set |\itemindent| to be
475 % $|\labelwidth|+|\labelsep|-|\leftmargin|$. Time for some \TeX\ arithmetic.
478 \itemindent\labelwidth%
479 \advance\itemindent\labelsep%
480 \advance\itemindent-\leftmargin%
483 % Now we must set up the label typesetting. We'll take the |\makelabel|
484 % provided by the user, remember it, and then redefine |\makelabel| in
485 % terms of the |\desclabelstyle| and the saved |\makelabel|.
488 \let\desc@makelabel\makelabel%
489 \def\makelabel##1{\desc@labelstyle{\desc@makelabel{##1}}}%
492 % I can't think of anything else which needs doing, so I'll call it a day
500 % Now we define the `end-bit' of the environment. Since all we need to do
501 % is to close the list, we can be ever-so slightly clever and use |\let|.
504 \let\endbasedescript\endlist
507 % Note that with these definitions, the standard \env{description}
508 % environment can be emulated by saying simply:
510 %\renewenvironment{description}{%
511 % \begin{basedescript}{}%
519 % \begin{macro}{\defaultdesc}
521 % Now to set up the standard description appearance. In the absence
522 % of any other declarations, the label will `push' the text out the way if
523 % the text is too long. The standard |\labelsep| and |\leftmargin| are not
524 % our problem. We typeset the label text in bold by default. Also,
525 % |\labelwidth| is cleared to 0\,pt, because this is what \LaTeX's usual
526 % \env{description} does.
529 \providecommand\defaultdesc{%
530 \desclabelstyle\pushlabel%
531 \def\makelabel##1{\bfseries##1}%
538 % \subsubsection{An example}
540 % \begin{environment}{note}
542 % The \env{note} environment is a simple application of the general
543 % description list shown above. It typesets the label (by default, the
544 % text `\textbf{note}') at the left margin, and the note text indented by
545 % the width of the label.
547 % The code is simple -- we take the environment's argument (which may have
548 % been omitted), store it in a box (using |\sbox| again, to handle colour
549 % changes correctly), set the label width from the width of the box, and
550 % then create a single item containing the label text. The text of the
551 % environment then appears in exactly the desired place.
553 % I've not used |\newcommand| here, for the following reasons:
556 % \item I don't like it much, to be honest.
558 % \item Until very recently, |\newcommand| only allowed you to define
559 % `long' commands, where new paragraphs were allowed to be started
560 % in command arguments; this removes a useful check which traps
561 % common errors like missing out `|}|' characters. I'd prefer to
562 % be compatible with older \LaTeX s than to use the new |\newcommand|
563 % which provides a $*$-form to work around this restriction.
568 \ifx\note\@@undefined
569 \def\note{\@ifnextchar[\note@i{\note@i[Note]}}
572 \sbox\z@{\makelabel{#1}}%
573 \desclabelwidth{\wd\z@}%
577 \let\endnote\endbasedescript
584 % \subsection{Compacted environments}
586 % Normal lists tend to have rather too much space between items if all or
587 % most of the item texts are one line or less each. We therefore define
588 % a macro |\makecompactlist| whuch creates `compacted' versions of existing
591 % \begin{macro}{\makecompactlist}
593 % We're given two arguments: the name of the new environment to create, and
594 % the name of the existing list environment to create.
596 % The first thing to do is to ensure that the environment we're creating is
597 % actually valid (i.e., it doesn't exist already, and it has a sensible
598 % name). We can do this with the internal \LaTeX\ macro |\@ifdefinable|.
601 \def\makecompactlist#1#2{%
602 \expandafter\@ifdefinable\csname#1\endcsname%
603 {\makecompactlist@i{#1}{#2}}%
607 % We also ought to ensure that the other environment already exists. This
608 % isn't too tricky. We'll steal \LaTeX's error and message for this.
611 \def\makecompactlist@i#1#2{%
612 \@ifundefined{#2}{\me@err{Environment `#2' not defined}\@ehc}{}%
615 % The main work for starting a compact list is done elsewhere.
618 \@namedef{#1}{\@compact@list{#2}}%
621 % Now to define the end of the environment; this isn't terribly difficult.
624 \expandafter\let\csname end#1\expandafter\endcsname%
625 \csname end#2\endcsname%
628 % That's a compacted environment created. Easy, no?
634 % The general case macro has to try slurping some arguments, calling the
635 % underlying environment, and removing vertical space.
638 \def\@compact@list#1{\@testopt{\@compact@list@i{#1}}{}}
639 \def\@compact@list@i#1[#2]{%
648 % \begin{environment}{itemize*}
649 % \begin{environment}{enumerate*}
650 % \begin{environment}{description*}
652 % Let's build some compacted environments now. These are easy now that
653 % we've done all the work above.
656 \makecompactlist{itemize*}{itemize}
657 \makecompactlist{enumerate*}{enumerate}
658 \makecompactlist{description*}{description}
666 % \subsection{Suspending and resuming lists}
668 % This is nowhere near perfect; it relies a lot on the goodwill of the user,
669 % although it seems to work fairly well.
671 % \begin{macro}{\suspend}
673 % The only thing that needs saving here is the list counter, whose name
674 % is stored in |\@listctr|. When I get a request to save the counter, I'll
675 % build a macro which will restore it when the environment is restored later.
677 % The first thing to do is to handle the optional argument. |\@dblarg| will
678 % sort this out, giving me a copy of the mandatory argument if there's no
679 % optional one provided.
682 \def\suspend{\@dblarg\suspend@i}
685 % That's all we need to do here.
688 \def\suspend@i[#1]#2{%
691 % Now I have a little problem; when I |\end| the environment, it will close
692 % off the grouping level, and the counter value will be forgotten. This is
693 % bad. I'll store all my definitions into a macro, and build the |\end|
694 % command into it; that way, everything will be expanded correctly. This
695 % requires the use of |\edef|, which means I must be a little careful.
701 % The first thing to do is to end the environment. I don't want |\end|
702 % expanded yet, so I'll use |\noexpand|.
708 % Now I must define the `resume' macro. I'll use |\csname| to build the
709 % named identifier into the name, so it won't go wrong (maybe). There's
710 % a little fun here to make the control sequence name but not expand it
714 \def\expandafter\noexpand\csname resume.#1\endcsname{%
717 % The counter name is hidden inside |\@listctr|, so the actual counter is
718 % called `|\csname c@\@listctr\endcsname|'. I'll use |\the| to read its
719 % current value, and assign it to the counter when the macro is used later.
722 \csname c@\@listctr\endcsname\the\csname c@\@listctr\endcsname%
725 % That's all we need to do there. Now close the macros and run them.
736 % \begin{macro}{\resume}
738 % Resuming environments is much easier. Since I use |\csname| to build the
739 % name, nothing happens if you try to resume environments which weren't
740 % suspended. I'll trap this and raise an error. Provide an optional
741 % argument for collecting arguments to the target list.
744 \def\resume{\@dblarg\resume@i}
745 \def\resume@i[#1]#2{\@testopt{\resume@ii{#1}{#2}}{}}
746 \def\resume@ii#1#2[#3]{%
748 \@ifundefined{resume.#1}{\ml@err@resume}{\@nameuse{resume.#1}}%
754 % That's all there is.
760 % \hfill Mark Wooding, \today