1 % \begin{meta-comment} <general public licence>
3 %% mdwlist package -- various list-related things
4 %% Copyright (c) 1996, 2002, 2003 Mark Wooding
6 %% This file is part of the `mdwtools' LaTeX package collection.
8 %% `mdwtools' is free software: you can redistribute it and/or modify it
9 %% under the terms of the GNU General Public License as published by the
10 %% Free Software Foundation; either version 2 of the License, or (at your
11 %% option) any later version.
13 %% `mdwtools' is distributed in the hope that it will be useful, but
14 %% WITHOUT ANY WARRANTY; without even the implied warranty of
15 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 %% General Public License for more details.
18 %% You should have received a copy of the GNU General Public License
19 %% along with `mdwtools'. If not, write to the Free Software Foundation,
20 %% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 % \begin{meta-comment} <Package preambles>
25 %<+package>\NeedsTeXFormat{LaTeX2e}
26 %<+package>\ProvidesPackage{mdwlist}
27 %<+package> [2020/09/06 1.14.0 Various list-related things]
32 %% {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
33 %% 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
34 %% Digits \0\1\2\3\4\5\6\7\8\9
35 %% Exclamation \! Double quote \" Hash (number) \#
36 %% Dollar \$ Percent \% Ampersand \&
37 %% Acute accent \' Left paren \( Right paren \)
38 %% Asterisk \* Plus \+ Comma \,
39 %% Minus \- Point \. Solidus \/
40 %% Colon \: Semicolon \; Less than \<
41 %% Equals \= Greater than \> Question mark \?
42 %% Commercial at \@ Left bracket \[ Backslash \\
43 %% Right bracket \] Circumflex \^ Underscore \_
44 %% Grave accent \` Left brace \{ Vertical bar \|
45 %% Right brace \} Tilde \~}
48 % \begin{meta-comment}
52 \describespackage{mdwlist}
54 \desclabelwidth{80pt}%
55 \desclabelstyle\nextlinelabel%
56 \def\makelabel{\bfseries}%
58 \newenvironment{cmdlist}
59 {\basedescript{\let\makelabel\cmd}}
66 % \section{User guide}
68 % This package provides some vaguely useful list-related commands and
71 % \item A way of building \env{description}-like environments.
72 % \item Commands for making `compacted' versions of list environments
73 % \item A method for suspending and resuming enumerated lists.
76 % \subsection{Description list handling}
78 % Different sorts of description-type lists require different sorts of
79 % formatting: I think that's fairly obvious. There are essentially three
80 % different attributes which should be changable:
82 % \item the indentation of the items being described,
83 % \item the handling of labels which don't fit properly, and
84 % \item the style used to typeset the label text.
86 % The first two items should usually be decided for all description-like
87 % lists in the document, to ensure consistency of appearance. The last
88 % depends much more on the content of the labels.
90 % \DescribeEnv{basedescript}
91 % The \env{basedescript} environment acts as a `skeleton' for description
92 % environments. It takes one argument, which contains declarations to
93 % be performed while constructing the list. I'd consider it unusual for
94 % the \env{basedescript} environment to be used in the main text: it's
95 % intended to be used to build other environments.
97 % The declarations which can be used to define description-type environments
98 % include all of those which are allowed when setting up a list (see the
99 % \LaTeX\ book for information here). Some others, which apply specifically
100 % to description lists, are also provided:
104 % \item \DescribeMacro{\desclabelwidth}
105 % The \syntax{"\\desclabelwidth{"<length>"}"} declaration sets labels
106 % to be left-aligned, with a standard width of \<length>; the item
107 % text is indented by \<length> plus the value of |\labelsep|.
109 % \item \DescribeMacro{\desclabelstyle}
110 % The label style determines how overlong labels are typeset. A style
111 % may be set using the \syntax{"\\desclabelstyle{"<style>"}"}
112 % declaration. The following \<style>s are provided:
114 % \item [\nextlinelabel] If the label is too wide to fit next to the
115 % first line of text, then it is placed on a line by itself;
116 % the main text is started on the next line with the usual
118 % \item [\multilinelabel] The label is typeset in a parbox with the
119 % appropriate width; if it won't fit on one line, then the
120 % text will be split onto subsequent lines.
121 % \item [\pushlabel] If the label is too wide to fit in the space
122 % allocated to it, the start of the item's text will be `pushed'
123 % over to the right to provide space for the label. This is
124 % the standard \LaTeX\ \env{description} behaviour.
127 % \item \DescribeMacro{\makelabel}
128 % The |\makelabel| command is responsible for typesetting a label.
129 % It is given one argument, which is the text given as an argument
130 % to the |\item| command; it should typeset it appropriately. The
131 % text will then be arranged appropriately according to the chosen
132 % label style. This command should be redefined using |\renewcommand|.
137 % \begin{demo}[w]{Various labelling styles}
138 %\begin{basedescript}{\desclabelstyle{\nextlinelabel}}
139 %\item [Short label] This is a short item, although it has quite a
140 % lot of text attached to it.
141 %\item [Slightly longer label text] This is a rather longer piece
142 % of text, with a correspondingly slightly longer label.
145 %\begin{basedescript}{\desclabelstyle{\multilinelabel}}
146 %\item [Short label] This is a short item, although it has quite a
147 % lot of text attached to it.
148 %\item [Slightly longer label text] This is a rather longer piece
149 % of text, with a correspondingly slightly longer label.
152 %\begin{basedescript}{\desclabelstyle{\pushlabel}}
153 %\item [Short label] This is a short item, although it has quite a
154 % lot of text attached to it.
155 %\item [Slightly longer label text] This is a rather longer piece
156 % of text, with a correspondingly slightly longer label.
161 % \DescribeMacro{\defaultdesc}
162 % To allow document designers to control the global appearance of description
163 % lists, the |\defaultdesc| command may be redefined; it is called while
164 % setting up a new \env{basedescript} list, before performing the user's
165 % declarations. By default, it attempts to emulate the standard \LaTeX\
166 % \env{description} environment:\footnote{^^A
167 % This is a slightly sanitised version of the real definition, which is
168 % given in the implementation section of this document.}
170 %\providecommand{\defaultdesc}{%
171 % \desclabelstyle{\pushlabel}%
172 % \renewcommand{\makelabel}[1]{\bfseries##1}%
173 % \setlength{\labelwidth}{0pt}%
176 % Unfortunately, \LaTeX\ doesn't provide a means for overriding a command
177 % which may or may not have been defined yet; in this case, I'd probably
178 % recommend using the \TeX\ primitive |\def| to redefine |\defaultdesc|.
180 % If you want to redefine the \env{description} environment in terms of
181 % the commands in this package, the following method is recommended:
183 %\renewenvironment{description}{%
184 % \begin{basedescript}{%
185 % \renewcommand{\makelabel}[1]{\bfseries##1}%
188 % \end{basedescript}%
191 % This ensures that labels are typeset in bold, as is usual, but other
192 % properties of the list are determined by the overall document style.
194 % \subsection{Compacted lists}
196 % \LaTeX\ tends to leave a certain amount of vertical space between list
197 % items. While this is normally correct for lists in which the items are
198 % several lines long, it tends to look odd if all or almost all the items
199 % are only one line long.
201 % \DescribeMacro{\makecompactlist}
203 % \syntax{"\\makecompactlist{"<new-env-name>"}{"<old-env-name>"}"}
204 % defines a new environment \<new-env-name> to be a `compacted' version of
205 % the existing environment \<old-env-name>; i.e., the two environments are
206 % the same except that the compacted version leaves no space between items
207 % or paragraphs within the list.
209 % \DescribeEnv{itemize*}
210 % \DescribeEnv{enumerate*}
211 % \DescribeEnv{description*}
212 % So that the most common cases are already handled, the package creates
213 % compacted $*$-variants of the \env{itemize}, \env{enumerate} and
214 % \env{description} environments. These were created using the commands
216 %\makecompactlist{itemize*}{itemize}
217 %\makecompactlist{enumerate*}{enumerate}
218 %\makecompactlist{description*}{description}
221 % Some list environments accept arguments. You can pass an argument to a
222 % list environment using an optional argument to its compact variant. For
225 %\begin{foolist*}[{someargument}]
228 % \subsection{Suspending and resuming list environments}
230 % \DescribeMacro{\suspend}
231 % \DescribeMacro{\resume}
232 % The |\suspend| and |\resume| commands allow you to temporarily end a list
233 % environment and then pick it up where you left off. The syntax is fairly
238 % <suspend-cmd> ::= \[[
240 % \begin{stack} \\ "[" <name> "]" \end{stack} "{" <env-name> "}"
243 % <resume-cmd> ::= \[[
245 % \begin{stack} \\ "[" <name> "]" \end{stack} "{" <env-name> "}"
246 % \begin{stack} \\ "[" <text> "]" \end{stack}
251 % The \<env-name> is the name of the environment; this will more often than
252 % not be the \env{enumerate} environment. The \<name> is a magic name you
253 % can use to identify the suspended environment; if you don't specify this,
254 % the environment name is used instead.
256 % \begin{demo}{Suspended environments}
257 %Here's some initial text. It's
258 %not very interesting.
260 %\item This is an item.
261 %\item This is another.
262 %\suspend{enumerate*}
263 %Some more commentry text.
269 % You can pass arguments to a resumed list environment through the second
270 % optional argument of the |\resume| command. If, for example, you're using
271 % David Carlisle's \package{enumerate} package, you could say something like
273 %\begin{enumerate}[\bfseries{Item} i]
277 %Some intervening text.
278 %\resume{enumerate}[{[\bfseries{Item} i]}]
279 %\item Yet another item
285 % \section{Implementation}
291 % \subsection{Description lists}
293 % \subsubsection{Label styles}
295 % \begin{macro}{\nextlinelabel}
297 % The idea here is that if the label is too long to fit in its box, we put
298 % it on its own line and start the text of the item on the next. I've
299 % used |\sbox| here to capture colour changes properly, even though I have
300 % deep moral objections to the use of \LaTeX\ boxing commands. Anyway,
301 % I capture the text in box~0 and compare its width to the amount of space
302 % I have in the label box. If there's enough, I can just unbox the box;
303 % otherwise I build a vbox containing the label text and an empty hbox --
304 % |\baselineskip| glue inserted between the two boxes makes sure we get
305 % the correct spacing between the two lines, and the vboxness of the vbox
306 % ensures that the baseline of my strange thing is the baseline of the
307 % \emph{bottom} box. I then bash the vbox on the nose, so as to make its
308 % width zero, and leave that as the result. Either way, I then add glue
309 % to left align whatever it is I've created.
312 \def\nextlinelabel#1{%
314 \ifdim\wd\z@>\labelwidth%
315 \setbox\z@\vbox{\box\z@\hbox{}}%
327 % \begin{macro}{\multilinelabel}
329 % A different idea -- make the label text wrap around onto the next line if
330 % it's too long. This is really easy, actually. I use a parbox to contain
331 % the label text, set to be ragged right, because there won't be enough
332 % space to do proper justification. There's also a funny hskip there --
333 % this is because \TeX\ only hyphenates things it finds sitting \emph{after}
334 % glue items. The parbox is top-aligned, so the label text and the item
335 % run downwards together. I put the result in box~0, and remove the depth,
336 % so as not to make the top line of the item text look really strange.
338 % All this leaves a little problem, though: if the item text isn't very long,
339 % the label might go further down the page than the main item, and possibly
340 % collide with the label below. I must confess that I'm not actually sure
341 % how to deal with this possibility, so I just hope it doesn't happen.
343 % By the way, I don't have moral objections to |\parbox|.
346 \def\multilinelabel#1{%
348 \parbox[t]\labelwidth{\raggedright\hskip\z@skip#1}%
358 % \begin{macro}{\pushlabel}
360 % Now we implement the old style behaviour -- if the label is too wide, we
361 % just push the first line of the item further over to the right. This
362 % is really very easy indeed -- we just stick some |\hfil| space on the
363 % right hand side (to left align if the label comes up too short). The
364 % `push' behaviour is handled automatically by \LaTeX's item handling.
367 \def\pushlabel#1{{#1}\hfil}
372 % \subsubsection{The main environment}
374 % \begin{macro}{\desclabelstyle}
376 % This is a declaration intended to be used only in the argument to the
377 % \env{basedescript} environment. It sets the label style for the list.
378 % All we do is take the argument and assign it to a magic control sequence
379 % which \env{basedescript} will understand later.
382 \def\desclabelstyle#1{\def\desc@labelstyle{#1}}
387 % \begin{macro}{\desclabelwidth}
389 % We set the label width and various other bits of information which will
390 % make all the bits of the description line up beautifully. We set
391 % |\labelwidth| to the value we're given (using |\setlength|, so that
392 % people can use the \package{calc} package if they so wish), and make
393 % the |\leftmargin| equal $|\labelwidth|+|\labelsep|$.
396 \def\desclabelwidth#1{%
397 \setlength\labelwidth{#1}%
398 \leftmargin\labelwidth%
399 \advance\leftmargin\labelsep%
405 % \begin{environment}{basedescript}
407 % This is the new description environment. It does almost everything you
408 % could want from a description environment, I think. The argument is a
409 % collection of declarations to be performed while setting up the list.
411 % This environment isn't really intended to be used by users -- it's here
412 % so that you can define other description environments in terms of it,
414 % The environment is defined in two bits -- the `start' bit here simply
415 % starts the list and inserts the user declarations in an appropriate
416 % point, although sensible details will be inerted if the argument was
420 \def\basedescript#1{%
423 % We must start the list. If the |\item| command's optional argument is
424 % missing, we should just leave a blank space, I think.
430 % So far, so good. Now put in some default declarations. I'll use a
431 % separate macro for this, so that the global appearance of lists can be
438 % Now we do the user's declarations.
444 % Now set up the other parts of the list. We set |\itemindent| so that the
445 % label is up against the current left margin. (The standard version
446 % actually leaves the label hanging to the left of the margin by a
447 % distance of |\labelsep| for a reason I can't quite comprehend -- there's
448 % an |\hspace{\labelsep}| in the standard |\makelabel| to compensate for
449 % this. Strange\dots)
451 % To make the label start in the right place, the text of the item must
452 % start a distance of $|\labelwidth|+|\labelsep|$ from the (pre-list) left
453 % hand margin; this means that we must set |\itemindent| to be
454 % $|\labelwidth|+|\labelsep|-|\leftmargin|$. Time for some \TeX\ arithmetic.
457 \itemindent\labelwidth%
458 \advance\itemindent\labelsep%
459 \advance\itemindent-\leftmargin%
462 % Now we must set up the label typesetting. We'll take the |\makelabel|
463 % provided by the user, remember it, and then redefine |\makelabel| in
464 % terms of the |\desclabelstyle| and the saved |\makelabel|.
467 \let\desc@makelabel\makelabel%
468 \def\makelabel##1{\desc@labelstyle{\desc@makelabel{##1}}}%
471 % I can't think of anything else which needs doing, so I'll call it a day
479 % Now we define the `end-bit' of the environment. Since all we need to do
480 % is to close the list, we can be ever-so slightly clever and use |\let|.
483 \let\endbasedescript\endlist
486 % Note that with these definitions, the standard \env{description}
487 % environment can be emulated by saying simply:
489 %\renewenvironment{description}{%
490 % \begin{basedescript}{}%
498 % \begin{macro}{\defaultdesc}
500 % Now to set up the standard description appearance. In the absence
501 % of any other declarations, the label will `push' the text out the way if
502 % the text is too long. The standard |\labelsep| and |\leftmargin| are not
503 % our problem. We typeset the label text in bold by default. Also,
504 % |\labelwidth| is cleared to 0\,pt, because this is what \LaTeX's usual
505 % \env{description} does.
508 \providecommand\defaultdesc{%
509 \desclabelstyle\pushlabel%
510 \def\makelabel##1{\bfseries##1}%
517 % \subsubsection{An example}
519 % \begin{environment}{note}
521 % The \env{note} environment is a simple application of the general
522 % description list shown above. It typesets the label (by default, the
523 % text `\textbf{note}') at the left margin, and the note text indented by
524 % the width of the label.
526 % The code is simple -- we take the environment's argument (which may have
527 % been omitted), store it in a box (using |\sbox| again, to handle colour
528 % changes correctly), set the label width from the width of the box, and
529 % then create a single item containing the label text. The text of the
530 % environment then appears in exactly the desired place.
532 % I've not used |\newcommand| here, for the following reasons:
535 % \item I don't like it much, to be honest.
537 % \item Until very recently, |\newcommand| only allowed you to define
538 % `long' commands, where new paragraphs were allowed to be started
539 % in command arguments; this removes a useful check which traps
540 % common errors like missing out `|}|' characters. I'd prefer to
541 % be compatible with older \LaTeX s than to use the new |\newcommand|
542 % which provides a $*$-form to work around this restriction.
547 \ifx\note\@@undefined
548 \def\note{\@ifnextchar[\note@i{\note@i[Note]}}
551 \sbox\z@{\makelabel{#1}}%
552 \desclabelwidth{\wd\z@}%
556 \let\endnote\endbasedescript
563 % \subsection{Compacted environments}
565 % Normal lists tend to have rather too much space between items if all or
566 % most of the item texts are one line or less each. We therefore define
567 % a macro |\makecompactlist| whuch creates `compacted' versions of existing
570 % \begin{macro}{\makecompactlist}
572 % We're given two arguments: the name of the new environment to create, and
573 % the name of the existing list environment to create.
575 % The first thing to do is to ensure that the environment we're creating is
576 % actually valid (i.e., it doesn't exist already, and it has a sensible
577 % name). We can do this with the internal \LaTeX\ macro |\@ifdefinable|.
580 \def\makecompactlist#1#2{%
581 \expandafter\@ifdefinable\csname#1\endcsname%
582 {\makecompactlist@i{#1}{#2}}%
586 % We also ought to ensure that the other environment already exists. This
587 % isn't too tricky. We'll steal \LaTeX's error and message for this.
590 \def\makecompactlist@i#1#2{%
591 \@ifundefined{#2}{\me@err{Environment `#2' not defined}\@ehc}{}%
594 % The main work for starting a compact list is done elsewhere.
597 \@namedef{#1}{\@compact@list{#2}}%
600 % Now to define the end of the environment; this isn't terribly difficult.
603 \expandafter\let\csname end#1\expandafter\endcsname%
604 \csname end#2\endcsname%
607 % That's a compacted environment created. Easy, no?
613 % The general case macro has to try slurping some arguments, calling the
614 % underlying environment, and removing vertical space.
617 \def\@compact@list#1{\@testopt{\@compact@list@i{#1}}{}}
618 \def\@compact@list@i#1[#2]{%
627 % \begin{environment}{itemize*}
628 % \begin{environment}{enumerate*}
629 % \begin{environment}{description*}
631 % Let's build some compacted environments now. These are easy now that
632 % we've done all the work above.
635 \makecompactlist{itemize*}{itemize}
636 \makecompactlist{enumerate*}{enumerate}
637 \makecompactlist{description*}{description}
645 % \subsection{Suspending and resuming lists}
647 % This is nowhere near perfect; it relies a lot on the goodwill of the user,
648 % although it seems to work fairly well.
650 % \begin{macro}{\suspend}
652 % The only thing that needs saving here is the list counter, whose name
653 % is stored in |\@listctr|. When I get a request to save the counter, I'll
654 % build a macro which will restore it when the environment is restored later.
656 % The first thing to do is to handle the optional argument. |\@dblarg| will
657 % sort this out, giving me a copy of the mandatory argument if there's no
658 % optional one provided.
661 \def\suspend{\@dblarg\suspend@i}
664 % That's all we need to do here.
667 \def\suspend@i[#1]#2{%
670 % Now I have a little problem; when I |\end| the environment, it will close
671 % off the grouping level, and the counter value will be forgotten. This is
672 % bad. I'll store all my definitions into a macro, and build the |\end|
673 % command into it; that way, everything will be expanded correctly. This
674 % requires the use of |\edef|, which means I must be a little careful.
680 % The first thing to do is to end the environment. I don't want |\end|
681 % expanded yet, so I'll use |\noexpand|.
687 % Now I must define the `resume' macro. I'll use |\csname| to build the
688 % named identifier into the name, so it won't go wrong (maybe). There's
689 % a little fun here to make the control sequence name but not expand it
693 \def\expandafter\noexpand\csname resume.#1\endcsname{%
696 % The counter name is hidden inside |\@listctr|, so the actual counter is
697 % called `|\csname c@\@listctr\endcsname|'. I'll use |\the| to read its
698 % current value, and assign it to the counter when the macro is used later.
701 \csname c@\@listctr\endcsname\the\csname c@\@listctr\endcsname%
704 % That's all we need to do there. Now close the macros and run them.
715 % \begin{macro}{\resume}
717 % Resuming environments is much easier. Since I use |\csname| to build the
718 % name, nothing happens if you try to resume environments which weren't
719 % suspended. I'll trap this and raise an error. Provide an optional
720 % argument for collecting arguments to the target list.
723 \def\resume{\@dblarg\resume@i}
724 \def\resume@i[#1]#2{\@testopt{\resume@ii{#1}{#2}}{}}
725 \def\resume@ii#1#2[#3]{%
727 \@ifundefined{resume.#1}{\ml@err@resume}{\@nameuse{resume.#1}}%
733 % That's all there is.
739 % \hfill Mark Wooding, \today