1 % \begin{meta-comment} <general public licence>
3 %% mdwlist package -- various list-related things
4 %% Copyright (c) 1996, 2002, 2003 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} <Package preambles>
23 %<+package>\NeedsTeXFormat{LaTeX2e}
24 %<+package>\ProvidesPackage{mdwlist}
25 %<+package> [2020/09/06 1.14.0 Various list-related things]
30 %% {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
31 %% 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
32 %% Digits \0\1\2\3\4\5\6\7\8\9
33 %% Exclamation \! Double quote \" Hash (number) \#
34 %% Dollar \$ Percent \% Ampersand \&
35 %% Acute accent \' Left paren \( Right paren \)
36 %% Asterisk \* Plus \+ Comma \,
37 %% Minus \- Point \. Solidus \/
38 %% Colon \: Semicolon \; Less than \<
39 %% Equals \= Greater than \> Question mark \?
40 %% Commercial at \@ Left bracket \[ Backslash \\
41 %% Right bracket \] Circumflex \^ Underscore \_
42 %% Grave accent \` Left brace \{ Vertical bar \|
43 %% Right brace \} Tilde \~}
46 % \begin{meta-comment}
50 \describespackage{mdwlist}
52 \desclabelwidth{80pt}%
53 \desclabelstyle\nextlinelabel%
54 \def\makelabel{\bfseries}%
56 \newenvironment{cmdlist}
57 {\basedescript{\let\makelabel\cmd}}
64 % \section{User guide}
66 % This package provides some vaguely useful list-related commands and
69 % \item A way of building \env{description}-like environments.
70 % \item Commands for making `compacted' versions of list environments
71 % \item A method for suspending and resuming enumerated lists.
74 % \subsection{Description list handling}
76 % Different sorts of description-type lists require different sorts of
77 % formatting: I think that's fairly obvious. There are essentially three
78 % different attributes which should be changable:
80 % \item the indentation of the items being described,
81 % \item the handling of labels which don't fit properly, and
82 % \item the style used to typeset the label text.
84 % The first two items should usually be decided for all description-like
85 % lists in the document, to ensure consistency of appearance. The last
86 % depends much more on the content of the labels.
88 % \DescribeEnv{basedescript}
89 % The \env{basedescript} environment acts as a `skeleton' for description
90 % environments. It takes one argument, which contains declarations to
91 % be performed while constructing the list. I'd consider it unusual for
92 % the \env{basedescript} environment to be used in the main text: it's
93 % intended to be used to build other environments.
95 % The declarations which can be used to define description-type environments
96 % include all of those which are allowed when setting up a list (see the
97 % \LaTeX\ book for information here). Some others, which apply specifically
98 % to description lists, are also provided:
102 % \item \DescribeMacro{\desclabelwidth}
103 % The \syntax{"\\desclabelwidth{"<length>"}"} declaration sets labels
104 % to be left-aligned, with a standard width of \<length>; the item
105 % text is indented by \<length> plus the value of |\labelsep|.
107 % \item \DescribeMacro{\desclabelstyle}
108 % The label style determines how overlong labels are typeset. A style
109 % may be set using the \syntax{"\\desclabelstyle{"<style>"}"}
110 % declaration. The following \<style>s are provided:
112 % \item [\nextlinelabel] If the label is too wide to fit next to the
113 % first line of text, then it is placed on a line by itself;
114 % the main text is started on the next line with the usual
116 % \item [\multilinelabel] The label is typeset in a parbox with the
117 % appropriate width; if it won't fit on one line, then the
118 % text will be split onto subsequent lines.
119 % \item [\pushlabel] If the label is too wide to fit in the space
120 % allocated to it, the start of the item's text will be `pushed'
121 % over to the right to provide space for the label. This is
122 % the standard \LaTeX\ \env{description} behaviour.
125 % \item \DescribeMacro{\makelabel}
126 % The |\makelabel| command is responsible for typesetting a label.
127 % It is given one argument, which is the text given as an argument
128 % to the |\item| command; it should typeset it appropriately. The
129 % text will then be arranged appropriately according to the chosen
130 % label style. This command should be redefined using |\renewcommand|.
135 % \begin{demo}[w]{Various labelling styles}
136 %\begin{basedescript}{\desclabelstyle{\nextlinelabel}}
137 %\item [Short label] This is a short item, although it has quite a
138 % lot of text attached to it.
139 %\item [Slightly longer label text] This is a rather longer piece
140 % of text, with a correspondingly slightly longer label.
143 %\begin{basedescript}{\desclabelstyle{\multilinelabel}}
144 %\item [Short label] This is a short item, although it has quite a
145 % lot of text attached to it.
146 %\item [Slightly longer label text] This is a rather longer piece
147 % of text, with a correspondingly slightly longer label.
150 %\begin{basedescript}{\desclabelstyle{\pushlabel}}
151 %\item [Short label] This is a short item, although it has quite a
152 % lot of text attached to it.
153 %\item [Slightly longer label text] This is a rather longer piece
154 % of text, with a correspondingly slightly longer label.
159 % \DescribeMacro{\defaultdesc}
160 % To allow document designers to control the global appearance of description
161 % lists, the |\defaultdesc| command may be redefined; it is called while
162 % setting up a new \env{basedescript} list, before performing the user's
163 % declarations. By default, it attempts to emulate the standard \LaTeX\
164 % \env{description} environment:\footnote{^^A
165 % This is a slightly sanitised version of the real definition, which is
166 % given in the implementation section of this document.}
168 %\providecommand{\defaultdesc}{%
169 % \desclabelstyle{\pushlabel}%
170 % \renewcommand{\makelabel}[1]{\bfseries##1}%
171 % \setlength{\labelwidth}{0pt}%
174 % Unfortunately, \LaTeX\ doesn't provide a means for overriding a command
175 % which may or may not have been defined yet; in this case, I'd probably
176 % recommend using the \TeX\ primitive |\def| to redefine |\defaultdesc|.
178 % If you want to redefine the \env{description} environment in terms of
179 % the commands in this package, the following method is recommended:
181 %\renewenvironment{description}{%
182 % \begin{basedescript}{%
183 % \renewcommand{\makelabel}[1]{\bfseries##1}%
186 % \end{basedescript}%
189 % This ensures that labels are typeset in bold, as is usual, but other
190 % properties of the list are determined by the overall document style.
192 % \subsection{Compacted lists}
194 % \LaTeX\ tends to leave a certain amount of vertical space between list
195 % items. While this is normally correct for lists in which the items are
196 % several lines long, it tends to look odd if all or almost all the items
197 % are only one line long.
199 % \DescribeMacro{\makecompactlist}
201 % \syntax{"\\makecompactlist{"<new-env-name>"}{"<old-env-name>"}"}
202 % defines a new environment \<new-env-name> to be a `compacted' version of
203 % the existing environment \<old-env-name>; i.e., the two environments are
204 % the same except that the compacted version leaves no space between items
205 % or paragraphs within the list.
207 % \DescribeEnv{itemize*}
208 % \DescribeEnv{enumerate*}
209 % \DescribeEnv{description*}
210 % So that the most common cases are already handled, the package creates
211 % compacted $*$-variants of the \env{itemize}, \env{enumerate} and
212 % \env{description} environments. These were created using the commands
214 %\makecompactlist{itemize*}{itemize}
215 %\makecompactlist{enumerate*}{enumerate}
216 %\makecompactlist{description*}{description}
219 % Some list environments accept arguments. You can pass an argument to a
220 % list environment using an optional argument to its compact variant. For
223 %\begin{foolist*}[{someargument}]
226 % \subsection{Suspending and resuming list environments}
228 % \DescribeMacro{\suspend}
229 % \DescribeMacro{\resume}
230 % The |\suspend| and |\resume| commands allow you to temporarily end a list
231 % environment and then pick it up where you left off. The syntax is fairly
236 % <suspend-cmd> ::= \[[
238 % \begin{stack} \\ "[" <name> "]" \end{stack} "{" <env-name> "}"
241 % <resume-cmd> ::= \[[
243 % \begin{stack} \\ "[" <name> "]" \end{stack} "{" <env-name> "}"
244 % \begin{stack} \\ "[" <text> "]" \end{stack}
249 % The \<env-name> is the name of the environment; this will more often than
250 % not be the \env{enumerate} environment. The \<name> is a magic name you
251 % can use to identify the suspended environment; if you don't specify this,
252 % the environment name is used instead.
254 % \begin{demo}{Suspended environments}
255 %Here's some initial text. It's
256 %not very interesting.
258 %\item This is an item.
259 %\item This is another.
260 %\suspend{enumerate*}
261 %Some more commentry text.
267 % You can pass arguments to a resumed list environment through the second
268 % optional argument of the |\resume| command. If, for example, you're using
269 % David Carlisle's \package{enumerate} package, you could say something like
271 %\begin{enumerate}[\bfseries{Item} i]
275 %Some intervening text.
276 %\resume{enumerate}[{[\bfseries{Item} i]}]
277 %\item Yet another item
283 % \section{Implementation}
289 % \subsection{Description lists}
291 % \subsubsection{Label styles}
293 % \begin{macro}{\nextlinelabel}
295 % The idea here is that if the label is too long to fit in its box, we put
296 % it on its own line and start the text of the item on the next. I've
297 % used |\sbox| here to capture colour changes properly, even though I have
298 % deep moral objections to the use of \LaTeX\ boxing commands. Anyway,
299 % I capture the text in box~0 and compare its width to the amount of space
300 % I have in the label box. If there's enough, I can just unbox the box;
301 % otherwise I build a vbox containing the label text and an empty hbox --
302 % |\baselineskip| glue inserted between the two boxes makes sure we get
303 % the correct spacing between the two lines, and the vboxness of the vbox
304 % ensures that the baseline of my strange thing is the baseline of the
305 % \emph{bottom} box. I then bash the vbox on the nose, so as to make its
306 % width zero, and leave that as the result. Either way, I then add glue
307 % to left align whatever it is I've created.
310 \def\nextlinelabel#1{%
312 \ifdim\wd\z@>\labelwidth%
313 \setbox\z@\vbox{\box\z@\hbox{}}%
325 % \begin{macro}{\multilinelabel}
327 % A different idea -- make the label text wrap around onto the next line if
328 % it's too long. This is really easy, actually. I use a parbox to contain
329 % the label text, set to be ragged right, because there won't be enough
330 % space to do proper justification. There's also a funny hskip there --
331 % this is because \TeX\ only hyphenates things it finds sitting \emph{after}
332 % glue items. The parbox is top-aligned, so the label text and the item
333 % run downwards together. I put the result in box~0, and remove the depth,
334 % so as not to make the top line of the item text look really strange.
336 % All this leaves a little problem, though: if the item text isn't very long,
337 % the label might go further down the page than the main item, and possibly
338 % collide with the label below. I must confess that I'm not actually sure
339 % how to deal with this possibility, so I just hope it doesn't happen.
341 % By the way, I don't have moral objections to |\parbox|.
344 \def\multilinelabel#1{%
346 \parbox[t]\labelwidth{\raggedright\hskip\z@skip#1}%
356 % \begin{macro}{\pushlabel}
358 % Now we implement the old style behaviour -- if the label is too wide, we
359 % just push the first line of the item further over to the right. This
360 % is really very easy indeed -- we just stick some |\hfil| space on the
361 % right hand side (to left align if the label comes up too short). The
362 % `push' behaviour is handled automatically by \LaTeX's item handling.
365 \def\pushlabel#1{{#1}\hfil}
370 % \subsubsection{The main environment}
372 % \begin{macro}{\desclabelstyle}
374 % This is a declaration intended to be used only in the argument to the
375 % \env{basedescript} environment. It sets the label style for the list.
376 % All we do is take the argument and assign it to a magic control sequence
377 % which \env{basedescript} will understand later.
380 \def\desclabelstyle#1{\def\desc@labelstyle{#1}}
385 % \begin{macro}{\desclabelwidth}
387 % We set the label width and various other bits of information which will
388 % make all the bits of the description line up beautifully. We set
389 % |\labelwidth| to the value we're given (using |\setlength|, so that
390 % people can use the \package{calc} package if they so wish), and make
391 % the |\leftmargin| equal $|\labelwidth|+|\labelsep|$.
394 \def\desclabelwidth#1{%
395 \setlength\labelwidth{#1}%
396 \leftmargin\labelwidth%
397 \advance\leftmargin\labelsep%
403 % \begin{environment}{basedescript}
405 % This is the new description environment. It does almost everything you
406 % could want from a description environment, I think. The argument is a
407 % collection of declarations to be performed while setting up the list.
409 % This environment isn't really intended to be used by users -- it's here
410 % so that you can define other description environments in terms of it,
412 % The environment is defined in two bits -- the `start' bit here simply
413 % starts the list and inserts the user declarations in an appropriate
414 % point, although sensible details will be inerted if the argument was
418 \def\basedescript#1{%
421 % We must start the list. If the |\item| command's optional argument is
422 % missing, we should just leave a blank space, I think.
428 % So far, so good. Now put in some default declarations. I'll use a
429 % separate macro for this, so that the global appearance of lists can be
436 % Now we do the user's declarations.
442 % Now set up the other parts of the list. We set |\itemindent| so that the
443 % label is up against the current left margin. (The standard version
444 % actually leaves the label hanging to the left of the margin by a
445 % distance of |\labelsep| for a reason I can't quite comprehend -- there's
446 % an |\hspace{\labelsep}| in the standard |\makelabel| to compensate for
447 % this. Strange\dots)
449 % To make the label start in the right place, the text of the item must
450 % start a distance of $|\labelwidth|+|\labelsep|$ from the (pre-list) left
451 % hand margin; this means that we must set |\itemindent| to be
452 % $|\labelwidth|+|\labelsep|-|\leftmargin|$. Time for some \TeX\ arithmetic.
455 \itemindent\labelwidth%
456 \advance\itemindent\labelsep%
457 \advance\itemindent-\leftmargin%
460 % Now we must set up the label typesetting. We'll take the |\makelabel|
461 % provided by the user, remember it, and then redefine |\makelabel| in
462 % terms of the |\desclabelstyle| and the saved |\makelabel|.
465 \let\desc@makelabel\makelabel%
466 \def\makelabel##1{\desc@labelstyle{\desc@makelabel{##1}}}%
469 % I can't think of anything else which needs doing, so I'll call it a day
477 % Now we define the `end-bit' of the environment. Since all we need to do
478 % is to close the list, we can be ever-so slightly clever and use |\let|.
481 \let\endbasedescript\endlist
484 % Note that with these definitions, the standard \env{description}
485 % environment can be emulated by saying simply:
487 %\renewenvironment{description}{%
488 % \begin{basedescript}{}%
496 % \begin{macro}{\defaultdesc}
498 % Now to set up the standard description appearance. In the absence
499 % of any other declarations, the label will `push' the text out the way if
500 % the text is too long. The standard |\labelsep| and |\leftmargin| are not
501 % our problem. We typeset the label text in bold by default. Also,
502 % |\labelwidth| is cleared to 0\,pt, because this is what \LaTeX's usual
503 % \env{description} does.
506 \providecommand\defaultdesc{%
507 \desclabelstyle\pushlabel%
508 \def\makelabel##1{\bfseries##1}%
515 % \subsubsection{An example}
517 % \begin{environment}{note}
519 % The \env{note} environment is a simple application of the general
520 % description list shown above. It typesets the label (by default, the
521 % text `\textbf{note}') at the left margin, and the note text indented by
522 % the width of the label.
524 % The code is simple -- we take the environment's argument (which may have
525 % been omitted), store it in a box (using |\sbox| again, to handle colour
526 % changes correctly), set the label width from the width of the box, and
527 % then create a single item containing the label text. The text of the
528 % environment then appears in exactly the desired place.
530 % I've not used |\newcommand| here, for the following reasons:
533 % \item I don't like it much, to be honest.
535 % \item Until very recently, |\newcommand| only allowed you to define
536 % `long' commands, where new paragraphs were allowed to be started
537 % in command arguments; this removes a useful check which traps
538 % common errors like missing out `|}|' characters. I'd prefer to
539 % be compatible with older \LaTeX s than to use the new |\newcommand|
540 % which provides a $*$-form to work around this restriction.
545 \ifx\note\@@undefined
546 \def\note{\@ifnextchar[\note@i{\note@i[Note]}}
549 \sbox\z@{\makelabel{#1}}%
550 \desclabelwidth{\wd\z@}%
554 \let\endnote\endbasedescript
561 % \subsection{Compacted environments}
563 % Normal lists tend to have rather too much space between items if all or
564 % most of the item texts are one line or less each. We therefore define
565 % a macro |\makecompactlist| whuch creates `compacted' versions of existing
568 % \begin{macro}{\makecompactlist}
570 % We're given two arguments: the name of the new environment to create, and
571 % the name of the existing list environment to create.
573 % The first thing to do is to ensure that the environment we're creating is
574 % actually valid (i.e., it doesn't exist already, and it has a sensible
575 % name). We can do this with the internal \LaTeX\ macro |\@ifdefinable|.
578 \def\makecompactlist#1#2{%
579 \expandafter\@ifdefinable\csname#1\endcsname%
580 {\makecompactlist@i{#1}{#2}}%
584 % We also ought to ensure that the other environment already exists. This
585 % isn't too tricky. We'll steal \LaTeX's error and message for this.
588 \def\makecompactlist@i#1#2{%
589 \@ifundefined{#2}{\me@err{Environment `#2' not defined}\@ehc}{}%
592 % The main work for starting a compact list is done elsewhere.
595 \@namedef{#1}{\@compact@list{#2}}%
598 % Now to define the end of the environment; this isn't terribly difficult.
601 \expandafter\let\csname end#1\expandafter\endcsname%
602 \csname end#2\endcsname%
605 % That's a compacted environment created. Easy, no?
611 % The general case macro has to try slurping some arguments, calling the
612 % underlying environment, and removing vertical space.
615 \def\@compact@list#1{\@testopt{\@compact@list@i{#1}}{}}
616 \def\@compact@list@i#1[#2]{%
625 % \begin{environment}{itemize*}
626 % \begin{environment}{enumerate*}
627 % \begin{environment}{description*}
629 % Let's build some compacted environments now. These are easy now that
630 % we've done all the work above.
633 \makecompactlist{itemize*}{itemize}
634 \makecompactlist{enumerate*}{enumerate}
635 \makecompactlist{description*}{description}
643 % \subsection{Suspending and resuming lists}
645 % This is nowhere near perfect; it relies a lot on the goodwill of the user,
646 % although it seems to work fairly well.
648 % \begin{macro}{\suspend}
650 % The only thing that needs saving here is the list counter, whose name
651 % is stored in |\@listctr|. When I get a request to save the counter, I'll
652 % build a macro which will restore it when the environment is restored later.
654 % The first thing to do is to handle the optional argument. |\@dblarg| will
655 % sort this out, giving me a copy of the mandatory argument if there's no
656 % optional one provided.
659 \def\suspend{\@dblarg\suspend@i}
662 % That's all we need to do here.
665 \def\suspend@i[#1]#2{%
668 % Now I have a little problem; when I |\end| the environment, it will close
669 % off the grouping level, and the counter value will be forgotten. This is
670 % bad. I'll store all my definitions into a macro, and build the |\end|
671 % command into it; that way, everything will be expanded correctly. This
672 % requires the use of |\edef|, which means I must be a little careful.
678 % The first thing to do is to end the environment. I don't want |\end|
679 % expanded yet, so I'll use |\noexpand|.
685 % Now I must define the `resume' macro. I'll use |\csname| to build the
686 % named identifier into the name, so it won't go wrong (maybe). There's
687 % a little fun here to make the control sequence name but not expand it
691 \def\expandafter\noexpand\csname resume.#1\endcsname{%
694 % The counter name is hidden inside |\@listctr|, so the actual counter is
695 % called `|\csname c@\@listctr\endcsname|'. I'll use |\the| to read its
696 % current value, and assign it to the counter when the macro is used later.
699 \csname c@\@listctr\endcsname\the\csname c@\@listctr\endcsname%
702 % That's all we need to do there. Now close the macros and run them.
713 % \begin{macro}{\resume}
715 % Resuming environments is much easier. Since I use |\csname| to build the
716 % name, nothing happens if you try to resume environments which weren't
717 % suspended. I'll trap this and raise an error. Provide an optional
718 % argument for collecting arguments to the target list.
721 \def\resume{\@dblarg\resume@i}
722 \def\resume@i[#1]#2{\@testopt{\resume@ii{#1}{#2}}{}}
723 \def\resume@ii#1#2[#3]{%
725 \@ifundefined{resume.#1}{\ml@err@resume}{\@nameuse{resume.#1}}%
731 % That's all there is.
737 % \hfill Mark Wooding, \today