chiark / gitweb /
Fix all the copyright dates.
[mdwtools] / mdwtab.dtx
1 % \begin{meta-comment} <general public licence>
2 %%
3 %% mdwtab package -- another rewrite of the tabular environment, etc.
4 %% Copyright (c) 1996, 2002, 2003, 2007, 2016, 2020 Mark Wooding
5 %%
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.
10 %%
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.
15 %%
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.
19 %%
20 % \end{meta-comment}
21 %
22 % \begin{meta-comment} <Package preambles>
23 %<+mdwtab>\NeedsTeXFormat{LaTeX2e}
24 %<+mdwtab>\ProvidesPackage{mdwtab}
25 %<+mdwtab>                [2003/08/24 1.10 Table typesetting with style]
26 %<+mathenv>\NeedsTeXFormat{LaTeX2e}
27 %<+mathenv>\ProvidesPackage{mathenv}
28 %<+mathenv>                [2003/08/24 1.10 Various maths environments]
29 %<+colour>\NeedsTeXFormat{LaTeX2e}
30 %<+colour>\ProvidesPackage{mtcolour}
31 %<+colour>                [2003/08/24 1.10 Colour support for mdwtab]
32 %<+color>\NeedsTeXFormat{LaTeX2e}
33 %<+color>\ProvidesPackage{mtcolor}
34 %<+color>                [2003/08/24 1.10 Fix for people who can't spell]
35 % \end{meta-comment}
36 %
37 % \CheckSum{3424}
38 %% \CharacterTable
39 %%  {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
40 %%   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
41 %%   Digits        \0\1\2\3\4\5\6\7\8\9
42 %%   Exclamation   \!     Double quote  \"     Hash (number) \#
43 %%   Dollar        \$     Percent       \%     Ampersand     \&
44 %%   Acute accent  \'     Left paren    \(     Right paren   \)
45 %%   Asterisk      \*     Plus          \+     Comma         \,
46 %%   Minus         \-     Point         \.     Solidus       \/
47 %%   Colon         \:     Semicolon     \;     Less than     \<
48 %%   Equals        \=     Greater than  \>     Question mark \?
49 %%   Commercial at \@     Left bracket  \[     Backslash     \\
50 %%   Right bracket \]     Circumflex    \^     Underscore    \_
51 %%   Grave accent  \`     Left brace    \{     Vertical bar  \|
52 %%   Right brace   \}     Tilde         \~}
53 %%
54 %
55 % \begin{meta-comment}
56 %
57 %<*driver>
58 \input{mdwtools}
59 \describespackage{mdwtab}
60 \describespackage{mtcolour}
61 \describespackage{mathenv}
62 \addcontents{lot}{\listoftables}
63 \mdwdoc
64 %</driver>
65 %
66 % \end{meta-comment}
67 %
68 %^^A-------------------------------------------------------------------------
69 % \renewcommand{\tabstyle}{\small}
70 %
71 % \section{User guide}
72 %
73 %
74 % The \package{mdwtab} package contains a reimplementation of the standard
75 % \LaTeX\ \env{tabular} and \env{array} environments.  This is not just an
76 % upgraded version: it's a complete rewrite.  It has several advantages over
77 % the official \package{array} package (not raw \LaTeX's, which is even less
78 % nice), and it's more-or-less compatible.  Most of these are rather
79 % technical, I'll admit.
80 %
81 % \begin{itemize}
82 %
83 % \item The newcolumn system is properly and perfectly integrated into the
84 %       system.  There are now \emph{no} `primitive' column types -- all the
85 %       standard types are created as user-defined columns.
86 %
87 % \item You can define entirely different table-like environments using the
88 %       equipment here.  It's still hard work, although less so than before.
89 %       I'll do an example of this some time.
90 %
91 % \item Construction of the preamble is generally much tidier.  I've used
92 %       token registers rather than |\edef|, and it's all done very nicely.
93 %
94 % \item Fine spacing before and after rules (described by DEK as `a mark of
95 %       quality') is now utterly trivial, since the preamble-generator will
96 %       store the appropriate information.
97 %
98 % \item You can use \env{array} in LR and paragraph modes without having
99 %       to surround it with `|$|' signs.
100 %
101 % \item Usually you don't want tables in the middle of paragraphs.  For these
102 %       cases, I've provided a simpler way to position the table
103 %       horizontally.
104 %
105 % \item Footnotes work properly inside \env{tabular} environments (hoorah!).
106 %       You can `catch' footnotes using the \env{minipage} environment if
107 %       you like.  (It uses an internal version of the \package{footnote}
108 %       package to handle footnotes, which doesn't provide extra goodies like
109 %       the \env{footnote} environment; you'll need to load the full package
110 %       explicitly to get them.)
111 %
112 % \item Standard \LaTeX\ tabular environments have a problem with lining up
113 %       ruled tables.  The |\firsthline| command given in the \textit{\LaTeX\
114 %       Companion} helps a bit, but it's not really good enough, and besides,
115 %       it doesn't \emph{actually} line the text up right after all.  The
116 %       \package{mdwtab} package does the job properly to begin with, so you
117 %       don't need to worry.
118 %
119 % \end{itemize}
120 %
121 % I've tested the following packages with \package{mdwtab}, and they all
122 % work.  Some of the contortions required to make them work weren't pleasant,
123 % but you don't need to know about them.  By a strange coincidence, all the
124 % packages were written by David Carlisle.  Anyway, here's the list:
125 % \begin{itemize}
126 % \item The quite nice \package{dcolumn} package.
127 % \item The more useful \package{delarray} package.
128 % \item The rather spiffy \package{hhline} package.
129 % \item The truly wonderful \package{tabularx} package.
130 % \item The utterly magnificent \package{longtable} package.
131 % \end{itemize}
132 %
133 % Note that I've looked at \package{supertabular} as well: it won't work, so
134 % use \package{longtable} instead, 'cos it's much better.
135 %
136 %
137 % \subsection{The downside}
138 %
139 % There's no such thing as a free lunch.  The \package{mdwtab} environment
140 % is not 100\% compatible with the \env{tabular} environment found in
141 % \LaTeXe\ or the \package{array} package.
142 %
143 % The differences between \package{mdwtab} and \LaTeXe's \env{tabular}
144 % environment are as follows:
145 %
146 % \begin{itemize} \synshorts \let\`=\lq
147 %
148 % \item The vertical spacing in \env{array} environments is different to
149 %       that in \env{tabular} environments.  This produces more attractive
150 %       results in most mathematical uses of \env{array}s, in the author's
151 %       opinion.  The spacing can be modified by playing with length
152 %       parameters.
153 %
154 % \item The presence of horizontal and vertical rules will alter the spacing
155 %       of the table (so a pair of columns separated by a `|' is wider than
156 %       a pair with no separation by "\\arrayrulewidth".  This does mean that
157 %       horizontal and vertical rules match up properly -- the usual \LaTeX\
158 %       environment makes the horizontal rules stop just short of the edge
159 %       of the table, making an ugly mess (check out the \textit{\LaTeX\
160 %       book} if you don't believe me -- page~62 provides a good example).
161 %       The \package{array} package handles rules in the same way as
162 %       \package{mdwtab}.
163 %
164 % \setbox0=\hbox{\footnotesize`\\def\\xcs{\\tabskip=\\fill}'}
165 % \setbox2=\hbox{\footnotesize`...@{\\span\\xcs}...'}
166 % \item In common with the \package{array} package, there are some
167 %       restrictions on the use of the "\\extracolsep" command in preambles:
168 %       you may use at most one "\\extracolsep" command in each `@' or `!'
169 %       expression.  Also, you can't say
170 %       \begin{listing}
171 %\newcommand{\xcs}{\extracolsep{\fill}}
172 %       \end{listing}
173 %       and then expect something like `...@{\\xcs}...' to actually work --
174 %       the "\\extracolsep" mustn't be hidden inside any other
175 %       commands.  Because things like `@' expressions aren't expanded at
176 %       the time, "\\extracolsep" has to be searched and processed
177 %       \`by hand'.\footnote{^^A
178 %         All \cs{extracolsep} does is modify the \cs{tabskip} glue, so
179 %         if you were an evil \TeX\ hacker like me, you could just say
180 %         \unhbox0\ and put \unhbox2\ in your preamble.  That'd work nicely.
181 %         It also works with the \package{array} package.}
182 %
183 % \item Control sequences (commands) in a table's preamble aren't expanded
184 %       before the preamble is read.  In fact, commands in the preamble are
185 %       considered to be column types, and their names are entirely
186 %       independent of normal \LaTeX\ commands.  No column types of this
187 %       nature have yet been defined\footnote{^^A
188 %         There used to be an internal \cs{@magic} type used by
189 %         \env{eqnarray}, but you're not supposed to know about that.
190 %         Besides, it's not there any more.}
191 %       but the possibility's always there.  Use the "\\newcolumntype" or
192 %       "\\coldef" commands to define new column types.
193 %
194 % \item The preamble parsing works in a completely different way.  There is
195 %       a certain amount of compatibility provided, although it's heavily
196 %       geared towards keeping \package{longtable} happy and probably won't
197 %       work with other packages.
198 %
199 % \item Obscure constructs which were allowed by the old preamble parser but
200 %       violate the syntax shown in the next section (e.g., `|@{}|' to
201 %       suppress the "\\doublerulesep" space between two vertical rules,
202 %       described in \textit{The \LaTeX\ Companion} as \`a misuse of the
203 %       `@{...}' qualifier') are now properly outlawed.  You will be given
204 %       an error message if you attempt to use such a construction.
205 %
206 % \item The `*' forms (which repeat column types) are now expanded at a
207 %       different time.  Previously, preambles like `c@*{4}{{:}@}{--}c'
208 %       were considered valid (the example would expand to
209 %       `c@{:}@{:}@{:}@{:}@{--}c'), because `*'s were expanded before the
210 %       preamble was actually parsed.  In the new system, `*' is treated
211 %       just like any other preamble character (it just has a rather odd
212 %       action), and preambles like this will result in an error (and
213 %       probably a rather confusing one).
214 %
215 % \item David Carlisle's \package{colortbl} package entirely fails to work
216 %       with \package{mdwtab}.  However, we now have colour support of our
217 %       own which is at times similar in style.
218 %
219 % \end{itemize}
220 %
221 % There are also several incompatibilities between \package{mdwtab} and
222 % \package{array}:
223 %
224 % \begin{itemize} \synshorts \let\`=\lq
225 %
226 % \item Because of the way "\\newcolumntype" works in the \package{array}
227 %       package, a horrid construction like
228 %       \begin{listing}
229 %\newcolumntype{x}{{:}}
230 %\begin{tabular}{|c!xc|}
231 %       \end{listing}
232 %       is considered to be valid, and is interpreted as `|c!{:}c|'.  My
233 %       reading of pages~54 and~55 of the \textit{\LaTeX\ book} tells me
234 %       that this sort of thing is forbidden in normal \LaTeX\ commands.
235 %       The \package{mdwtab} preamble parser now treats column type letters
236 %       much more like commands with the result that the hacking above won't
237 %       work any more.  The construction above would actually be interpreted
238 %       as `|c!{x}c|' (i.e., the `x' column type wouldn't be expanded to
239 %       `{:}' because the parser noticed that it was the argument to the
240 %       `!' modifier\footnote{^^A
241 %         This is a direct result of the way \TeX\ treats undelimited
242 %         arguments.  See chapters~5 and~20 of \textit{The \TeX book} for
243 %         more information about how grouping affects argument reading.}).
244 %
245 % \item Most of the points above, particularly those relating to the
246 %       handling of the preamble, also apply to the \package{array} package.
247 %       it's not such an advance over the \LaTeXe\ version as everyone said
248 %       it was.
249 %
250 % \end{itemize}
251 %
252 %
253 % \subsection{Syntax}
254 %
255 % \DescribeEnv{tabular}
256 % \DescribeEnv{tabular*}
257 % \DescribeEnv{array}
258 % So that everyone knows where I stand, here's a complete syntax for my
259 % version of the \env{tabular} environment, and friends
260 %
261 % \begin{grammar}
262 %
263 % <tabular-env> ::= \[[
264 %   "\\begin"
265 %   \begin{stack}
266 %     "{tabular}" \\ "{tabular*}" "{" <length> "}" \\
267 %     "{array}" \\ "{smarray}"
268 %   \end{stack}
269 %   \[ "[" <position-arg> "]" \]
270 %   "{" <preamble> "}" <text>
271 %   "\\end"
272 %   \( "{tabular}" \\ "{tabular*}" \\ "{array}" \\ "{smarray}" \)
273 % \]]
274 %
275 % <position-arg> ::= (see below)
276 %
277 % <preamble>    ::= \[[
278 %   <first-column>
279 %   \[ \< <column> \> \]
280 % \]]
281 %
282 % <first-column> ::= \[[ \[ <rule> \] <column> \]]
283 %
284 % <column>      ::= \[[
285 %   \[ <spacing> \] \[ \< <user-pre-text> \> \] <column-type>
286 %   \[ \< <user-post-text> \> \] \[ <spacing> \] \[ <rule> \]
287 % \]]
288 %
289 % <spacing>     ::= \[[ "@" "{" <text> "}" \]]
290 %
291 % <user-pre-text> ::= \[[ \[ "?" \] ">" "{" <text> "}" \]]
292 %
293 % <column-type> ::= \[[
294 %   \begin{stack}
295 %     \[ "T" \\ "M" \] \( "l" \\ "c" \\ "r" \) \\
296 %     \( "p" \\ "m" \\ "b" \) "{" <length> "}" \\
297 %     "#" "{" <raw-pre-text> "}" "{" <raw-post-text> "}"
298 %   \end{stack}
299 % \]]
300 %
301 % <user-post-text> ::= \[[ \[ "?" \] "<" "{" <text> "}" \]]
302 %
303 % <rule>        ::= \[[ \( "|" \\ "!" "{" <text> "}" \) \]]
304 %
305 % \end{grammar}
306 %
307 % If you examine the above very carefully, you'll notice a slight deviation
308 % from the original -- an |@|-expression \emph{following} a rule is
309 % considered to be part of the \emph{next} column, not the current one.  This
310 % is, I think, an almost insignificant change, and essential for some of the
311 % new features.  You'll also notice the new |#| column type form, which
312 % allows you to define new real column types instead of just modifying
313 % existing ones.  It's not intended for direct use in preambles -- it's
314 % there mainly for the benefit of people who know what they're doing and
315 % insist on using |\newcolumntype| anyway.
316 %%
317 % The actual column types are shown in table~\ref{tbl:columns}.
318 %
319 % \begin{table}
320 % \begin{tabular}[C]{| >{\synshorts} c | m{3in} |}         \hlx{hv[1]}
321 %
322 % \multicolumn{2}{|c|}{\bf Column types}                \\ \hlx{v[1]hv}
323 % \bf Name      & \bf Meaning                           \\ \hlx{vhv.}
324 % "l"           & Left aligned text (\env{tabular}) or
325 %                 equation (\env{array}).               \\ \hlx{.}
326 % "c"           & Centred text (\env{tabular}) or
327 %                 equation (\env{array}).               \\ \hlx{.}
328 % "r"           & Right aligned text (\env{tabular}) or
329 %                 equation (\env{array}).               \\ \hlx{vhv.}
330 % "Ml", "Mc" and "Mr" & Left, centre and right aligned
331 %                 equations.*                           \\ \hlx{.}
332 % "Tl", "Tc" and "Tr" & Left, centre and right aligned
333 %                 text.*                                \\ \hlx{vhv.}
334 % "p{"<width>"}" & Top aligned paragraph with the given
335 %                 width.                                \\ \hlx{.}
336 % "m{"<width>"}" & Vertically centred paragraph with
337 %                 the given width.                      \\ \hlx{.}
338 % "b{"<width>"}" & Bottom aligned paragraph with the
339 %                 given width.                          \\ \hlx{vhv.}
340 % "#{"<pre>"}{"<post>"}" & User defined column type:
341 %                 \<pre> is inserted before the
342 %                 cell entry, \<post> is inserted
343 %                 afterwards.*                          \\ \hlx{vhhv[1]}
344 %
345 % \multicolumn{2}{|c|}{\bf Other modifier characters}   \\ \hlx{v[1]hv}
346 % \bf Name      & \bf Meaning                           \\ \hlx{vhv.}
347 % "|"           & Inserts a vertical rule between
348 %                 columns.                              \\ \hlx{.}
349 % "$*["<params>"]" & Inserts a vertical rule of given
350 %                 width between columns; "*" selects
351 %                 "\arraythickrulewidth".*              \\ \hlx{.}
352 % "!{"<text>"}" & Inserts \<text> between columns,
353 %                 treating it as a vertical rule.       \\ \hlx{vhv.}
354 % "@{"<text>"}" & Inserts \<text> instead of the
355 %                 usual intercolumn space.              \\ \hlx{vhv.}
356 % ">{"<text>"}" & Inserts \<text> just before the
357 %                 actual column entry.                  \\ \hlx{.}
358 % "<{"<text>"}" & Inserts \<text> just after the
359 %                 actual column entry.                  \\ \hlx{.}
360 % "?>{"<text>"}" & Inserts \<text> before the column
361 %                 entry \emph{and} the rules list.*     \\ \hlx{.}
362 % "?<{"<text>"}" & Inserts \<text> after the column
363 %                 entry \emph{and} the rules list.*     \\ \hlx{.}
364 % "'{"<text>"}" & Inserts \<text> into the rules list
365 %                 only.*                                \\ \hlx{vhv.}
366 % "*{"<count>"}{"<chars>"}" & Inserts \<count>
367 %                 copies of the \<chars> into the
368 %                 preamble.                             \\ \hlx{vhs}
369 %
370 % \multicolumn{2}{@{}l}{* This column type is a new feature}
371 % \end{tabular}
372 %
373 % \caption{\package{array} and \package{tabular} column types and modifiers}
374 % \label{tbl:columns}
375 % \end{table}
376 %
377 % Now that's sorted everything out, there shouldn't be any arguments at all
378 % about what a column means.
379 %
380 % The lowercase \<position-arg>s \lit{t}, \lit{c} and \lit{b} do exactly
381 % what they did before: control the vertical positioning of the table.  The
382 % uppercase ones control the \emph{horizontal} positioning -- this is how you
383 % create \emph{unboxed} tables.  You can only create unboxed tables in
384 % paragraph mode.
385 %
386 % Note that unboxed tables still can't be broken across pages.  Use
387 % the \package{longtable} package for this, because it already does an
388 % excellent job.
389 %
390 % \DescribeMacro{\tabpause}
391 % One thing you can to with unboxed tables, however, is to `interrupt' them,
392 % do some normal typesetting, and then continue.  This is achieved by the
393 % |\tabpause| command: its argument is written out in paragraph mode, and
394 % the table is continued after the argument finishes.
395 % Note that it isn't a real argument as far as commands like |\verb| are
396 % concerned -- they'll work inside |\tabpause| without any problems.
397 %
398 % \DescribeMacro{\vline}
399 % The |\vline| command draws a vertical rule the height of the current table
400 % cell (unless the current cell is being typeset in paragraph mode -- it
401 % only works in the simple LR-mode table cells, or in \lit{@} or \lit{!}
402 % modifiers).  It's now been given an optional argument which describes
403 % parameters for the line.  See section~\ref{sec:ruleparams}.
404 %
405 % { \let\tabstyle=\relax
406 % \begin{demo}{An example of \cmd\vline}
407 %\large
408 %\begin{tabular}
409 %  {| c !{\vline[5pt]} c | c |}
410 %  \hlx{hv}
411 %  \bf A & \it B & \sf C \\
412 %  \hlx[2pt]{vhv}
413 %  \bf D & \it E & \sf F \\
414 %  \hlx{vh}
415 %\end{tabular}
416 % \end{demo}
417 % }
418 %
419 % \DescribeMacro{smarray}
420 % You've probably noticed that there's an unfamiliar environment mentioned
421 % in the syntax shown above.  The \env{smarray} environment produces a
422 % `small' array, with script size cells rather than the normal full text
423 % size cells.  I've seen examples of this sort of construction\footnote{^^A
424 %   There's a nasty use of \env{smallmatrix} in the |testmath.tex| file which
425 %   comes with the \package{amslatex} distribution.  It's actually there to
426 %   simulate a `smallcases' environment, which the \package{mathenv} package
427 %   includes, based around \env{smarray}.} ^^A
428 % being implemented by totally unsuitable commands.  Someone may find it
429 % handy.
430 %
431 %
432 % \subsection{An updated \cs{cline} command}
433 %
434 % \DescribeMacro{\cline}
435 % \DescribeMacro{\hline}
436 % The standard \LaTeX\ |\cline| command has been updated.  As well as just
437 % passing a range of columns to draw lines through, you can now pass a comma
438 % separated list of column numbers and ranges:
439 %
440 % \begin{grammar}
441 % <cline-cmd>   ::= \[[
442 %   "\\cline"
443 %   \[ "*" \] \\ \[ "[" <rule-params> "]" \]
444 %   "{" \< <number> \[ "-" <number> \] \\ "," \> "}"
445 % \]]
446 % \end{grammar}
447 %
448 % The rules printed by |\cline| and |\hline| can be modified by rule
449 % parameters: see section~\ref{sec:ruleparams}.
450 %
451 % Note that |\cline| rules are rather bodgy.  Other rules now have
452 % \emph{thickness}, but |\cline|s don't.  Instead, they backspace over
453 % previous stuff.  If you don't like that, insert an appropriate gap, using
454 % |\vgap|.  The \lit{z} rune in |\hlx| is perfect for this kind of thing --
455 % precede your \lit{c} lines by \lit{z} lines for best results.
456 %
457 % {\let\tabstyle\relax
458 % \begin{demo}[w]{A \cs{cline} example}
459 %\newcommand{\mc}{\multicolumn{1}}
460 %\begin{tabular}[C]{|c|c|c|c|}           \cline{2,4}
461 %  \mc{c|}{one} & two & three & four   \\ \hline
462 %  five & six & seven & \mc{c}{eight}  \\ \cline{1,3}
463 %\end{tabular}
464 % \end{demo}
465 % }
466 %
467 % \subsection{Other stuff}
468 %
469 % \DescribeMacro\nextrow
470 % The \env{tabular} and \env{array} environments maintain a counter
471 % \textsf{tabrow}.  The counter is reset to zero at the start of each table.
472 % It is stepped by one by default; you can provide an optional argument which
473 % is the amount to add.
474 %
475 %
476 % \subsection{Spacing control}
477 %
478 % One of the most irritating things about \LaTeX's tables is that there isn't
479 % enough space around horizontal rules.  Donald Knuth, in \textit{The
480 % \TeX book}, describes addition of some extra vertical space here as `a mark
481 % of quality', and since \TeX\ was designed to produce `beautiful documents'
482 % it seems a shame that \LaTeX\ doesn't allow this to be done nicely.  Well,
483 % it does now.
484 %
485 % \DescribeMacro{\vgap}
486 % The extra vertical space is added using a command |\vgap|, with the
487 % following syntax:
488 %
489 % \begin{grammar}
490 %
491 % <vgap-cmd>    ::= \[[
492 %   "\\vgap" \[ "[" <which-cols> "]" \] "{" <length> "}"
493 % \]]
494 %
495 % <which-cols>  ::= \[[ \< <number> \[ "-" <number> \] \\ "," \> \]]
496 %
497 % \end{grammar}
498 %
499 % This command must appear either immediately after the beginning of the
500 % table or immediately after the |\\| which ends a row.  (Actually, there are
501 % other commands which also have this requirement -- you can specify a
502 % collection of them wherever you're allowed to give any one.)  It adds some
503 % vertical space (the amount is given by the \<length>) to the table,
504 % making sure that the vertical rules of the table are extended correctly.
505 %
506 % The |\vgap| command relies on information stored while your table preamble
507 % is being examined.  However, it's possible that you might not want some
508 % of the rules drawn (e.g., if you've used |\multicolumn|).  The optional
509 % \<which-cols> argument allows you to specify which rules are \emph{not}
510 % to be drawn.  You can specify either single column numbers or ranges.  The
511 % rule at the very left hand side is given the number~0; the rules at the
512 % end of column~$n$ are numbered~$n$.  It's easy really.
513 %
514 % \DescribeMacro{\hlx}
515 % Using |\vgap| is all very well, but it's a bit cumbersome, and takes up a
516 % lot of typing, especially when combined with |\hline| commands.  The |\hlx|
517 % command tries to tidy things.
518 %
519 % The syntax is simple:
520 % \begin{grammar}
521 %
522 % <hlx-cmd>     ::= \[[
523 %   "\\hlx"
524 %   \[ "*" \] \[ "[" <rule-params> "]" \]
525 %   "{"
526 %   \begin{rep}
527 %     \begin{stack}
528 %       "h" \\
529 %       \tok{"v["<which-cols>"]["<length>"]"} \\
530 %       \tok{"z["<which-cols>"]["<length>"]"} \\
531 %       \tok{"s["<length>"]"} \\
532 %       \tok{"c{"<which-cols>"}"} \\
533 %       "b" \\
534 %       \tok{"/["<number>"]"} \\
535 %       \tok{"!{"<rule-params>"}"} \\
536 %       \tok{"?{"<stuff>"}"} \\
537 %       \tok{"+["<step>"]"} \\
538 %       "."
539 %     \end{stack}
540 %   \end{rep}
541 %   "}"
542 % \]]
543 %
544 % \end{grammar}
545 % The |*| or optional \<rule-params> give rule-drawing parameters for the |h|
546 % and |c| subcommands.  (Note that you can't pass a |*| or an optional
547 % parameters argument to the |h| or |c| subcommands directly.)  See
548 % section~\ref{sec:ruleparams}.
549 %
550 % The argument works a bit like a table preamble, really.  Each letter is a
551 % command.  The following are supported:
552 %
553 % \begin{description}
554 %
555 % \item [\lit*{h}] Works just like |\hline|.  If you put two adjacent to each
556 %       other, a gap will be put between them.
557 %
558 % \item [\lit*{v[}\<which-cols>\lit*{][}\<length>\lit*{]}]  Works
559 %       like \syntax{"\\vgap["<which-cols>"]{"<length>"}"}.  If the
560 %       \<length> is omitted, the value of |\doublerulesep| is used.
561 %       This usually looks right.
562 %
563 % \item [\lit*{z[}\<which-cols>\lit*{][}\<length>\lit*{]}]  Like \lit{v},
564 %       except that the default gap is the current rule width (set by the
565 %       \<rule-params>) rather than |\doublerulesep|.  This is a good thing
566 %       to insert before a |\cline| row.
567 %
568 % \item [\lit*{s[}\<length>\lit*{]}]  Leaves a vertical gap with the
569 %       given size.  If you omit the \<length> then |\doublerulesep| is
570 %       used.  This is usually right.
571 %
572 % \item [\lit*{c\char`\{}\<which-cols>\lit*{\char`\}}]  Works just like
573 %       |\cline|.
574 %
575 % \item [\lit*{b}] Inserts a backspace the width of a rule.  This is useful
576 %       when doing \package{longtable}s.
577 %
578 % \item [\lit*{/[}\<number>\lit*{]}]  Allows a page break in a table.  Don't
579 %       use this except in a \env{longtable} environment.  The \<number>
580 %       works exactly the same as it does in the |\pagebreak| command,
581 %       except that the default is 0, which just permits a break without
582 %       forcing it.
583 %
584 % \item [\lit*{!\char`\{}\<rule-params>\lit*{\char`\}}]  Change the rule
585 %       parameters to be used for subsequent subcommands.
586 %
587 % \item [\lit*{?\char`\{}\<stuff>\lit*{\char`\}}]  Do \<stuff>, which can be
588 %       any commands which \emph{don't} typeset anything.
589 %
590 % \item [\lit*{+[\<step>]}]  Add \<step> (default is 1) to the value of the
591 %       \textsf{tabrow} counter.
592 %
593 % \item [\lit*{.}]  (That's a dot)  Starts the next row of the table.  No
594 %       more characters may follow the dot, and no |\hline|, |\hlx|, |\vgap|
595 %       or |\multicolumn| commands may be used after it.  You don't have to
596 %       include it, and most of the time it's totally useless.  It can be
597 %       handy for some macros, though.  I used it in (and in fact added it
598 %       especially for) the table of column types.
599 %
600 % \end{description}
601 %
602 % An example of the use of |\hlx| is given, so you can see what's going on.
603 %
604 % \begin{figure}
605 % \let\tabstyle\relax
606 % \begin{demo}[w]{Beautiful table example}
607 %\newcommand{\zerowidth}[1]{\hbox to 0pt{\hss#1\hss}}
608 %\setlength{\tabcolsep}{1.5em}
609 %\begin{tabular}[C]{| r | c | r |}                  \hlx{hv[1,2]}
610 %  \multicolumn{3}{|c|}{\bf AT\&T Common Stock}  \\ \hlx{v[1,2]hv}
611 %  \multicolumn{1}{|c|}{\zerowidth{\bf Year}} &
612 %  \multicolumn{1}{c|}{\zerowidth{\bf Price}} &
613 %  \multicolumn{1}{c|}{\zerowidth{\bf Dividend}} \\ \hlx{vhv}
614 %  1971 & 41--54 & \$2.60                        \\
615 %     2 & 41--54 &   2.70                        \\
616 %     3 & 46--55 &   2.87                        \\
617 %     4 & 40--53 &   3.24                        \\
618 %     5 & 45--52 &   3.40                        \\
619 %     6 & 51--59 &    .95\rlap{*}                \\ \hlx{vhs}
620 %  \multicolumn{3}{@{}l}{* (first quarter only)}
621 %\end{tabular}
622 % \end{demo}
623 % \end{figure}
624 %
625 %
626 % \subsection{Creating beautiful long tables}
627 %
628 % You can use the |\vgap| and |\hlx| commands with David Carlisle's
629 % stunning \package{longtable} package.  However, there are some things you
630 % should be away of to ensure that your tables always come out looking
631 % lovely.
632 %
633 % The \package{longtable} package will break a table at an |\hline| command,
634 % leaving a rule at the bottom of the page and another at the top of the
635 % next page.  This means that a constructions like |\hlx{vhv}| will be
636 % broken into something like |\hlx{vh}| at the bottom of the page and
637 % |\hlx{hv}| at the top of the next.  You need to design the table headers
638 % and footers with this in mind.
639 %
640 % However, there appears to be a slight problem:\footnote
641 %   {You might very well call it a bug.  I couldn't possibly comment.}
642 % if the footer starts with an |\hline|, and a page is broken at an |\hline|,
643 % then you get an extra thick rule at the bottom of the page.  This is a bit
644 % of a problem, because if the rule isn't there in the footer and you get
645 % a break between two rows \emph{without} a rule between them, then the page
646 % looks very odd.
647 %
648 % If you want to do ruled longtables, I'd recommend that you proceed as
649 % follows:
650 % \begin{itemize}
651 % \item End header sections with an |\hlx{vh}|.
652 % \item Begin footer sections with an |\hlx{bh}|.
653 % \item Begin the main table with |\hlx{v}|.
654 % \item Insert |\hlx{vhv}| commands in the main table body as usual.
655 % \end{itemize}
656 % If \package{longtable} gets modified appropriately, the use of the \lit{b}
657 % command won't be necessary.
658 %
659 % Here's an example of the sort of thing you'd type.
660 %
661 % \begin{listinglist} \listingsize
662 % \verb"\begin{longtable}[c]{|c|l|}           \hlx{hv}" \\
663 % \verb"\bf Heading & \bf Also heading     \\ \hlx{vh}" \\
664 % \verb"\endhead" \\
665 % \verb"\hlx{bh}" \\
666 % \verb"\endfoot" \\
667 % \verb"\hlx{v}" \\
668 % \verb"First main & table line            \\ \hlx{vhv}" \\
669 % \verb"Lots of text & like this           \\ \hlx{vhv}" \\
670 % \null\quad\vdots \\
671 % \verb"Lots of text & like this           \\ \hlx{vhv}" \\
672 % \verb"Last main & table line             \\ \hlx{vh}" \\
673 % \verb"\end{longtable}"
674 % \end{listinglist}
675 %
676 %
677 % \subsection{Rules and vertical positioning}
678 %
679 % In the \LaTeXe\ and \package{array.sty} versions of \env{tabular}, you run
680 % into problems if you try to use ruled tables together with the \lit{[t]} or
681 % \lit{[b]} position specifiers -- the top or bottom rule ends up being
682 % nicely lined up with the text baseline, giving you an effect which is
683 % nothing like the one you expected.  The \textit{\LaTeX\ Companion} gives
684 % two commands |\firsthline| and |\lasthline| which are supposed to help with
685 % this problem.  (These commands have since migrated into the \package{array}
686 % package.)  Unfortunately, |\firsthline| doesn't do its job properly --
687 % it gets the text position wrong by exactly the width of the table rules.
688 %
689 % The \package{mdwtab} package makes all of this automatic.  It gets the
690 % baseline positions exactly right, whether or not you use rules.  Earlier
691 % versions of this package required that you play with a length parameter
692 % called |\rulefudge|; this is no longer necessary (or even possible -- the
693 % length parameter no longer exists).  The package now correctly compensates
694 % for all sorts of rules and |\vgap|s at the top and bottom of a table and
695 % it gets the positioning right all by itself.  You've never had it so good.
696 %
697 %
698 % \subsection{Rule parameters}
699 % \label{sec:ruleparams}
700 %
701 % The rule-drawing commands |\hline|, |\vline|, |\cline| and |\hlx|, and the
702 % |$| column type (which is otherwise a synonym for "|") accept \emph{rule
703 % parameters}.  If the command is followed by a |*|, then the rules are a bit
704 % thicker than usual -- they use |\arraythickrulewidth| rather than
705 % |\arrayrulewidth|.  However, there's an optional argument which can contain
706 % one of:
707 %
708 % \begin{description} \setdescriptionlabel{\ttfamily#1}
709 % \item[thin] Use |\arrayrulewidth| as the line width.  This is the default.
710 % \item[thick] Use |\arraythickrulewidth| as the line width.  This is the
711 %   same as giving a |*| after the command.
712 % \item[width=\<length>] Make the rules exactly \<length> wide.
713 % \item[\<length>] The same as \texttt{width=\<length>}, for compatibility.
714 % \end{description}
715 %
716 % More of these keywords will be added later if past experience is anything
717 % to go by.  Note that the individual |\hlx| subcommands \emph{don't} take
718 % rule parameters, but see the |!| subcommand for updating the current
719 % parameters.
720 %
721 % \DescribeMacro\tabsetruleparams
722 % If you say \syntax{"\\tabsetruleparams{"<rule-params>"}"} then the
723 % \<rule-params> will be prepended to any parameters provided to specific
724 % rule-drawing commands (including the \lit{\char`\|} preamble command).  For
725 % example, |\tabsetruleparams{thick}| makes all rules thick.  This is a local
726 % declaration.
727 %
728 %
729 % \subsection{User serviceable parts}
730 %
731 % There are a lot of parameters which you can modify in order to make arrays
732 % and tables look nicer.  They are all listed in table~\ref{tbl:config}.
733 %
734 % \begin{table}
735 % \begin{tabular}[C]{| l | m{3in} |}                               \hlx{hv}
736 % \bf Parameter         & \bf Meaning                           \\ \hlx{vhv}
737 % |\tabstyle|           & A command executed at the beginning of
738 %                         a \env{tabular} or \env{tabular$*$}
739 %                         environment.  By default does nothing.
740 %                         Change using |\renewcommand|.         \\ \hlx{vhv}
741 % |\extrarowheight|     & A length added to the height of every
742 %                         row, used to stop table rules
743 %                         overprinting ascenders.  Default 0\,pt.
744 %                         Usage is deprecated now: use |\hlx|
745 %                         instead.                              \\ \hlx{vhv}
746 % |\tabextrasep|        & Extra space added between rows in a
747 %                         \env{tabular} or \env{tabular$*$}
748 %                         environment (added \emph{before} any
749 %                         following |\hline|).  Default 0\,pt.  \\
750 % |\arrayextrasep|      & Analogous to |\tabextrasep|, but for
751 %                         \env{array} environments.  Default
752 %                         1\,jot (3\,pt).                       \\
753 % |\smarrayextrasep|    & Analogous to |\tabextrasep|, but for
754 %                         \env{smarray} environments.  Default
755 %                         1\,pt.                                \\ \hlx{vhv}
756 % |\tabcolsep|          & Space added by default on each side of
757 %                         a table cell (unless suppressed by an
758 %                         \lit{@}-expression) in \env{tabular}
759 %                         environments.  Default is defined by
760 %                         your document class.                  \\
761 % |\arraycolsep|        & Analogous to |\tabcolsep|, but for
762 %                         \env{array} environments.  Default is
763 %                         defined by your document class.       \\
764 % |\smarraycolsep|      & Analogous to |\tabcolsep|, but for
765 %                         \env{smarray} environments.  Default
766 %                         is 3\,pt.                             \\ \hlx{vhv}
767 % |\arrayrulewidth|     & The width of horizontal and vertical
768 %                         rules in tables.                      \\
769 % |\arraythickrulewidth|& The width of starred rules in tables. \\
770 % |\doublerulesep|      & Space added between two adjacent
771 %                         vertical or horizontal rules.  Also
772 %                         used by |\hlx{v}|.                    \\ \hlx{vhv}
773 % |\arraystretch|       & Command containing a factor to
774 %                         multiply the default row height.
775 %                         Default is defined by your document
776 %                         class (usually 1).                    \\ \hlx{vh}
777 % \end{tabular}
778 %
779 % \caption{Parameters for configuring table environments}
780 % \label{tbl:config}
781 %
782 % \end{table}
783 %
784 %
785 % \subsection{Defining column types}
786 %
787 % \DescribeMacro{\newcolumntype}
788 % The easy way to define new column types is using |\newcolumntype|.  It
789 % works in more or less the same way as |\newcommand|:
790 %
791 % \begin{grammar}
792 %
793 % <new-col-type-cmd> ::= \[[
794 %   "\\newcolumntype"
795 %   "{" <column-name> "}"
796 %   \[ "[" <num-args> "]" \]
797 %   \[ "[" <default-arg> "]" \]
798 %   "{" <first-column> \[ \< <column> \> \] "}"
799 % \]]
800 %
801 % \end{grammar}
802 %
803 % (The \env{array.sty} implementation doesn't accept the \<default-arg>
804 % argument.  I've no idea why not, 'cos it was very easy to implement.)
805 %
806 % \DescribeMacro{\colset}
807 % This implementation allows you to define lots of different sets of columns.
808 % You can change the current set using the |\colset| declaration:
809 % \begin{grammar}
810 % <colset-cmd>  ::= \[[ "\\colset" "{" <set-name> "}" \]]
811 % \end{grammar}
812 % This leaves a problem, though: at any particular moment, the current
813 % column set could be anything, since other macros and packages can change
814 % it.
815 %
816 % \DescribeMacro{\colpush}
817 % \DescribeMacro{\colpop}
818 % What actually happens is that a stack of column sets is maintained.  The
819 % |\colset| command just replaces the item at the top of the stack.  The
820 % command |\colpush| pushes its argument onto the top of the stack, making
821 % it the new current set.  The corresponding |\colpop| macro (which doesn't
822 % take any arguments) removes the top item from the stack, reinstating the
823 % previous current column set.
824 %
825 % \begin{grammar}
826 % <colpush-cmd> ::= \[[ "\\colpush" "{" <set-name> "}" \]]
827 % <colpop-cmd>  ::= \[[ "\\colpop" \]]
828 % \end{grammar}
829 %
830 % The macros which manipulate the column set stack work \emph{locally}.
831 % The contents of the stack are saved when you open a new group.
832 %
833 % To make sure everyone behaves themselves properly, these are the rules for
834 % using the column set stack:
835 %
836 % \begin{itemize}
837 %
838 % \item Packages defining column types must ensure that they preserve the
839 %       current column set.  Either they must push their own column type
840 %       and pop it off when they're finished defining columns, or they must
841 %       avoid changing the stack at all, and use the optional arguments to
842 %       |\coldef| and |\collet|.
843 %
844 % \item Packages must not assume that any particular column set is current
845 %       unless they have made sure of it themselves.
846 %
847 % \item Packages must ensure that they pop exactly as much as they push.
848 %       There isn't much policing of this (perhaps there should be more),
849 %       so authors are encouraged to behave responsibly.
850 %
851 % \item Packages must change the current column set (using |\colset|) when
852 %       they start up their table environment.  This will be restored when
853 %       the environment closes.
854 %
855 % \end{itemize}
856 %
857 % \DescribeMacro{\coldef}
858 % |\newcolumntype| is probably enough for most purposes.  However, Real
859 % \TeX nicians, and people writing new table-generating environments, require
860 % something lower-level.
861 %
862 % \begin{grammar}
863 % <coldef-cmd>  ::= \[[
864 %   "\\coldef"
865 %     \[ "[" <set-name> "]" \]
866 %     <col-name> <arg-template> "{" <replacement-text> "}"
867 % \]]
868 % \end{grammar}
869 %
870 % Note that this defines a column type in the current colset.  It works
871 % almost exactly the same way as \TeX's primitive |\def|.  There is a
872 % potential gotcha here: a |\tab@mkpream| token is inserted at the end of
873 % your replacement text.  If you need to read an optional argument or
874 % something, you'll need to gobble this token before you carry on.  The
875 % |\@firstoftwo| macro could be handy here:
876 % \begin{listing}
877 %\coldef x{\@firstoftwo{\@ifnextchar[\@xcolumn@i\@xcolumn@ii}}}
878 % \end{listing}
879 % This isn't a terribly pretty state of affairs, and I ought to do something
880 % about it.  I've not seen any use for an optional argument yet, though.
881 % Note that if you do gobble the |\tab@mkpream|, it's your responsibility to
882 % insert another one at the very end of your macro's expansion (so that
883 % further preamble characters can be read).
884 %
885 % The replacement text is inserted directly.  It's normal to insert preamble
886 % elements here.  There are several to choose from:
887 %
888 % \begin{description}
889 %
890 % \item [Column items] provide the main `meat' of a column.  You insert a
891 %       column element by saying
892 %       \syntax{"\\tabcoltype{"<pre-text>"}{"<post-text>"}"}.
893 %       The user's text gets inserted between these two.  (So do user pre-
894 %       and post-texts.  Bear this in mind.)
895 %
896 % \item [User pre-text items] work like the \lit{>} preamble command.  You
897 %       use the \syntax{"\\tabuserpretype{"<text>"}"} command to insert it.
898 %       User pre-texts are written in \emph{reverse} order between the
899 %       pre-text of the column item and the text from the table cell.
900 %
901 % \item [User post-text items] work like the \lit{<} preamble command.  You
902 %       use the \syntax{"\\tabuserposttype{"<text>"}"} command to insert it.
903 %       Like user pre-texts, user post-texts are written in reverse order,
904 %       between the table cell text and the column item post-text.
905 %
906 % \item [Space items] work like the \lit{@} preamble command.  They're
907 %       inserted with the \syntax{"\\tabspctype{"<text>"}"} command.
908 %
909 % \item [Rule items] work like the `\verb"|"' and \lit{!} commands.  You
910 %       insert them with the \syntax{"\\tabruletype{"<text>"}"} command.
911 %       Note that the text is inserted by |\vgap| too, so it should contain
912 %       things which adjust their vertical size nicely.  If you really need
913 %       to, you can test |\iftab@vgap| to see if you're in a |\vgap|.
914 %
915 % \end{description}
916 %
917 % \DescribeMacro{\collet}
918 % As well as defining columns, you can copy definitions (rather like |\let|
919 % allows you to copy macros).  The syntax is like this:
920 %
921 % \begin{grammar}
922 %
923 % <collet-cmd>  ::= \[[
924 %   \[ "[" <set-name> "]" \] <col-name> \[ "=" \] \[ "[" <set-name> "]" \]
925 %   <col-name>
926 % \]]
927 %
928 % \end{grammar}
929 %
930 % (In other words, you can copy definitions from other column sets.)
931 %
932 %
933 % \subsection{Defining new table-generating environments}
934 %
935 % Quite a few routines are provided specifically to help you to define new
936 % environments which do alignment in a nice way.
937 %
938 % \subsubsection{Reading preambles}
939 %
940 % The main tricky bit in doing table-like environments is parsing preambles.
941 % No longer.
942 %
943 % \DescribeMacro{\tab@readpreamble}
944 % \DescribeMacro{\tab@doreadpream}
945 % The main parser routine is called |\tab@doreadpream|.  Given a user
946 % preamble string as an argument, it will build an |\halign| preamble to
947 % return to you.  However, the preamble produced won't be complete.  This is
948 % because you can actually make multiple calls to |\tab@doreadpream| with
949 % bits of user preambles.  The |\newcolumntype| system uses this mechanism,
950 % as does the \lit{*} (repeating) modifier.  When there really is no more
951 % preamble to read, you need to \emph{commit} the heldover tokens to the
952 % output.  The |\tab@readpreamble| routine will do this for you -- given a
953 % user preamble, it builds a complete output from it.
954 %
955 % A token register |\tab@preamble| is used to store the generated preamble.
956 % Before starting, you must initialise this token list to whatever you want.
957 % There's another token register, |\tab@shortline|, which is used to store
958 % tokens used by |\vgap|.  For each column in the table, the list contains
959 % an |\omit| (to override the standard preamble) and an |\hfil| space taking
960 % up most of the column.  Finally, for each rule item in the user preamble,
961 % the shortline list contains an entry of the form:
962 % \begin{quote} \synshorts
963 %   "\\tab@ckr{"<column-number>"}{"<rule-text>"}"
964 % \end{quote}
965 % This is used to decide whether to print the rule or an empty thing of the
966 % same width.  You probably ought to know that the very first column does
967 % \emph{not} have a leading |\omit| -- this is supplied by |\vgap| so that
968 % it can then look for optional arguments.
969 %
970 % \DescribeMacro{\tab@initread}
971 % As well as initialising |\tab@preamble| and emptying |\tab@shortline|,
972 % there are several other operations required to initialise a preamble read.
973 % These are all performed by the |\tab@initread| macro, although you may want
974 % to change some of the values for your specific application.  For reference,
975 % the actions performed are:
976 % \begin{itemize}
977 % \item initialising the parser state by setting $|\tab@state| =
978 %       |\tab@startstate|$;
979 % \item clearing the token lists |\tab@preamble| and |\tab@shortlist|;
980 % \item initialising the macros |\tab@tabtext|, |\tab@midtext|, and
981 %       |\tab@multicol| to their default values of `|&|',
982 %       `|\ignorespaces#\unskip|' and the empty token list respectively.^^A
983 %       \footnote{^^A
984 %         These are macros rather than token lists to avoid hogging all
985 %         the token list registers.  Actually, the package only allocates
986 %         two, although it does use almost all of the temporary registers as
987 %         well.  Also, there's a lie: \cs{unskip} is too hamfisted to remove
988 %         trailing spaces properly;  I really use a macro called
989 %         \cs{@maybe@unskip}}
990 % \item clearing the internal token list registers |\tab@pretext|,
991 %       |tab@userpretext| and |\tab@posttext|;
992 % \item clearing the column counter |\tab@columns| to zero;
993 % \item clearing the action performed when a new column is started (by making
994 %       the |\tab@looped| macro equal to |\relax|; this is used to make
995 %       |\multicolumn| macro raise an error if you try to do more than one
996 %       column); and
997 % \item setting up some other switches used by the parser (|\iftab@rule|,
998 %       |\iftab@initrule| and |\iftab@firstcol|, all of which are set to be
999 %       |true|).
1000 % \end{itemize}
1001 %
1002 % The macro |\tab@multicol| is used by the |\multicolumn| command to insert
1003 % any necessary items (e.g., struts) before the actual column text.  If you
1004 % set this to something non-empty, you should probably consider adding a
1005 % call to the macro to the beginning of |\tab@preamble|.
1006 %
1007 % When parsing is finally done, the count register |\tab@columns| contains
1008 % the number of columns in the alignment.  Don't corrupt this value, because
1009 % it's used for handling |\hline| commands.
1010 %
1011 % \subsubsection{Starting new lines}
1012 %
1013 % The other messy bit required by table environments is the newline command
1014 % |\\|.  There are nasty complications involved with starting new lines, some
1015 % of which can be handled by this package, and some on which I can only give
1016 % advice.
1017 %
1018 % \DescribeMacro{\tab@cr}
1019 % The optional arguments and star-forms etc. can be read fairly painlessly
1020 % using the |\tab@cr| command:
1021 %
1022 % \begin{grammar}
1023 % <tabcr-cmd>   ::= \[[
1024 %   "\\tab@cr" <command> "{" <non-star-text> "}" "{" <star-text> "}"
1025 % \]]
1026 % \end{grammar}
1027 %
1028 % This will call your \<command> with two arguments.  The first is the
1029 % contents of the optional argument, or `|\z@|' if there wasn't one.  The
1030 % second is either \<star-text> or \<non-star-text> depending on
1031 % whether the user wrote the $*$-form or not.
1032 %
1033 % Somewhere in your \<command>, you'll have to use the |\cr| primitive to
1034 % end the table row.  After you've done this, you \emph{must} ensure that you
1035 % don't do anything that gets past \TeX's mouth without protecting it --
1036 % otherwise |\hline| and co.\ won't work.  I usually wrap things up in a
1037 % |\noalign| to protect them, although there are other methods.  Maybe.
1038 %
1039 % You might like to have a look at the \env{eqnarray} implementation provided
1040 % to see how all this gets put into practice.
1041 %
1042 %
1043 % \subsection{Colour support}
1044 %
1045 % I've now added colour support to \package{mdwtab}.  That is, you can play
1046 % with the colours of table cell backgrounds, rules and text.  The support
1047 % isn't there by default: you have to either give the \textsf{colour} option
1048 % when you load \package{mdwtab}, or include the \package{mtcolour} package
1049 % yourself.  It's very new, and might break.  It's probably not as good as
1050 % \package{colortbl}.  I prefer English spellings for the commands and
1051 % declarations: to reduce confusion, I've provided synonyms with fewer `u's.
1052 % If only American package authors were so thoughtful.  The examples in this
1053 % part of the documentation may not display correctly in some DVI viewers:
1054 % for best results, run |dvips| and view the PostScript using (say)
1055 % GhostScript.
1056 %
1057 % \subsubsection{New commands and features}
1058 %
1059 % \DescribeMacro\cellcolour
1060 % The |\cellcolour| command changes the background colour for the current
1061 % cell.  You can use it directly in a table cell, or in the table preamble.
1062 % It doesn't matter whereabouts in the table cell it goes.  Note that
1063 % unlike the \package{colortbl}, the |\cellcolour| command works on the
1064 % \emph{entire} contents of the cell, including the |\tabcolsep| space and
1065 % the rules, if any.  That means that it's robust even if there are |@{...}|
1066 % preamble commands present.
1067 %
1068 % The actual syntax is like this:
1069 %
1070 % \begin{grammar}
1071 % <cell-colour-cmd> ::= \[[
1072 %   \( "\\cellcolour" \\ "\\cellcolor" \)
1073 %   \[ "*" \]
1074 %   \[ "[" <colour-model> "]" \]
1075 %   "{" <colour> "}"
1076 %   \[ "[" <left-overhang> "]"
1077 %      \[ "[" <right-overhang> "]" \] \]
1078 % \]]
1079 % \end{grammar}
1080 %
1081 % The \lit{*} makes |\cellcolour| override an extant |\rowcolour| command
1082 % (see below).  The \<colour-model> and \<colour> are as for the |\color|
1083 % command.  The \<left-overhang> is how much the colour band should stick out
1084 % to the left of the cell; and similarly for the \<right-overhang>.  If you
1085 % don't give a \<right-overhang> then the same value is used for both; if you
1086 % give neither then there's no overhang.  The reason you might want overhang
1087 % is to deal with |\extracolsep| glue.  I shouldn't worry about it if I were
1088 % you.
1089 %
1090 % It's very useful to use |\cellcolour| in a preamble, in particular, in the
1091 % |?>| preamble command (which was added specifically).  (If you use only |>|
1092 % then |\vgap| leaves very odd-looking gaps in the table.)
1093 %
1094 % { \let\tabstyle=\relax
1095 % \begin{demo}{A coloured table}
1096 %\newcolumntype{\c}[2]{%
1097 %  >{\color{#1}}%
1098 %  ?>{\cellcolour{#2}}%
1099 %}
1100 %\begin{tabular}
1101 %   {|\c{cyan}{red}c|
1102 %     \c{magenta}{green}c|
1103 %     \c{yellow}{blue}c|}
1104 %                       \hlx{hv}
1105 %  One  &Two  &Three \\ \hlx{vhv}
1106 %  Four &Five &Six   \\ \hlx{vhv}
1107 %  Seven&Eight&Nine  \\ \hlx{vh}
1108 %\end{tabular}
1109 % \end{demo}
1110 % }
1111 %
1112 % Obviously, judicious use of |\newcolumntype| would abbreviate the above
1113 % considerably.
1114 %
1115 % \DescribeMacro\rowcolour
1116 % \DescribeMacro\rowcolouroff
1117 % The |\rowcolour| command changes the background colour in the same way as
1118 % |\cellcolour|; however, its effect takes precedence over |\cellcolour| (but
1119 % not |\cellcolour*|) if both are active, and isn't automatically turned off
1120 % at the start of the next cell.  To actually turn it off again, say
1121 % |\rowcolouroff|.
1122 %
1123 % \begin{grammar}
1124 % <row-colour-cmd> ::= \[[
1125 %   \( "\\rowcolour" \\ "\\rowcolor" \)
1126 %   \[ "[" <colour-model> "]" \]
1127 %   "{" <colour> "}"
1128 % \]]
1129 % \end{grammar}
1130 %
1131 % Note that you don't get to specify overhang parameters here.  The ones from
1132 % the |\cellcolour| declaration are used, unless there isn't one in which
1133 % case there aren't any.
1134 %
1135 % \DescribeMacro\ifmod
1136 % A common thing to do is colour alternate rows of the table differently.
1137 % This is a bit tricker for \package{mdwtab} than it would be for, say,
1138 % \package{array}, since it's hard to spot where the `rows' actually change.
1139 % The solution is to use the \textsf{tabrow} counter, and |\ifmod|.  Saying
1140 % say \syntax{"\\ifmod{"$x$"}{"$m$"}{"$y$"}{"<yes>"}{"<no>"}"} is the same as
1141 % saying \<yes> if $x \bmod m = y$, and \<no> otherwise.  This is typically
1142 % used as follows.
1143 %
1144 % % { \let\tabstyle=\relax
1145 % \begin{demo}{Alternating row colours}
1146 %\begin{tabular}
1147 %  {|?>{\ifmod
1148 %    {\value{tabrow}}{2}{1}
1149 %    {\rowcolour{white}}
1150 %    {\rowcolour[gray]{0.9}}}
1151 %     c|c|}
1152 %  \hlx{h+v}
1153 %  One   & Two   \\ \hlx{vh+v}
1154 %  Three & Four  \\ \hlx{vh+v}
1155 %  Five  & Six   \\ \hlx{vh+v}
1156 %  Seven & Eight \\ \hlx{vh+v}
1157 %  Nine  & Ten   \\ \hlx{vh+}
1158 %\end{tabular}
1159 % \end{demo}
1160 % }
1161 %
1162 % There are new rule parameters for colours.  You get a colourful rule if you
1163 % say \syntax{"colour" "=" <colour>}.  You can also say \syntax{"colourmodel"
1164 % "=" <colour-model>} to choose unnamed colours.
1165 %
1166 % When I've thought of what other things need doing, I'll do some of them.
1167 % The kit I've provided \emph{can} do most interesting things, but it might
1168 % require a certain level of hacking.  Ask me if you want something and it's
1169 % not obvious how to do it.
1170 %
1171 % \subsubsection{Dirty tricks}
1172 %
1173 % The colour support interacts with |\vgap| very badly.  The preamble rune
1174 % |?>{\cellcolour{...}}| works well if you want to colour a column, and
1175 % |\rowcolour| works either in the preamble or as
1176 % |\hlx{?{\rowcolour{...}}}|.  But what if you want to just colour one table
1177 % cell?  You can, as suggested above, just say |\cellcolour{...}| in the
1178 % table text, but that leaves really nasty-looking gaps above and below if
1179 % there are adjacent |\vgap| rows.
1180 %
1181 % This is what |\hlx{?{...}}| was invented for.  Here's a demo.
1182 %
1183 % \begin{demo}[w]{Colouring just one cell}
1184 %\let\hack=\relax
1185 %\begin{tabular}[C]{|c|?>{\hack}c|}          \hlx{hv}
1186 %Uncoloured & cells here                  \\ \hlx{vhv}
1187 %And some & more                          \\
1188 %        \hlx{vh?{\gdef\hack{\cellcolour{red}}}v}
1189 %Yet more & This one's red!               \\
1190 %        \hlx{vh?{\global\let\hack=\relax}v}
1191 %And more & uncoloured cells              \\ \hlx{vh}
1192 %\end{tabular}
1193 % \end{demo}
1194 %
1195 %
1196 % \subsection{The \package{mathenv} package alignment environments}
1197 %
1198 % The \package{mathenv} package provides several environments for aligning
1199 % equations in various ways.  They're mainly provided as a demonstration of
1200 % the table handling macros in \package{mdwtab}, so don't expect great
1201 % things.  If you want truly beautiful mathematics, use
1202 % \package{amsmath}.\footnote{^^A
1203 %   Particularly since nice commands like \cmd\over\ are being reactivated
1204 %   in a later release of \package{amsmath}.}
1205 % However, the various environments do nest in an approximately useful way.
1206 % I also think that the \env{matrix} and \env{script} environments provided
1207 % here give better results than their \package{amsmath} equivalents, and
1208 % they are certainly more versatile.
1209 %
1210 % \subsubsection{The new \env{eqnarray} environment}
1211 %
1212 % \DescribeEnv{eqnarray}
1213 % \DescribeEnv{eqnarray*}
1214 % As an example of the new column defining features, and because the original
1215 % isn't terribly good, I've included a rewritten version of the
1216 % \env{eqnarray} environment.  The new implementation closes the gap between
1217 % \env{eqnarray} and \AmSTeX\ alignment features.  It's in a separate,
1218 % package called \package{mathenv}, to avoid wasting your memory.
1219 %
1220 % \begin{grammar}
1221 %
1222 % <eqnarray-env> ::= \[[
1223 %   <begin-eqnarray> \< <row> \\ "\\\\" \> <end-eqnarray>
1224 % \]]
1225 %
1226 % <begin-eqnarray> ::= \[[
1227 %   "\\begin" \( "{eqnarray}" \\ "{eqnarray*}" \)
1228 %   \[ "[" \< <eqa-column> \> "]" \]
1229 % \]]
1230 %
1231 % <eqa-column>  ::= \[[
1232 %   \[ "q" \\ ":" \]
1233 %   \[ \< ">" "{" <pre-text> "}" \> \]
1234 %   \begin{stack}
1235 %     \[ "T" \] \( "r" \\ "c" \\ "l" \) \\
1236 %     "L" \\
1237 %     "x"
1238 %   \end{stack}
1239 %   \[ \< "<" "{" <post-text> "}" \> \]
1240 % \]]
1241 %
1242 % <end-eqnarray> ::= \[[
1243 %   "\\end" \begin{stack} "{eqnarray}" \\ "{eqnarray*}" \end{stack}
1244 % \]]
1245 %
1246 % \end{grammar}
1247 %
1248 % Descriptions of the various column types are given in
1249 % table~\ref{tbl:eqnarray}.
1250 %
1251 % \begin{table}
1252 % \begin{tabular}[C]{| >{\synshorts} c | m{3in} |}         \hlx{hv[1]}
1253 %
1254 % \multicolumn{2}{|c|}{\bf Column types}                \\ \hlx{v[1]hv}
1255 % \bf Name      & \bf Meaning                           \\ \hlx{vhv.}
1256 % "l"           & Left aligned piece of equation.       \\ \hlx{.}
1257 % "c"           & Centred piece of equation.            \\ \hlx{.}
1258 % "x"           & Centred or flush-left whole equation
1259 %                 (depending on \textsf{fleqn} option). \\ \hlx{.}
1260 % "r"           & Right aligned piece of equation.      \\ \hlx{vhv.}
1261 % "L"           & Left aligned piece of equation whose
1262 %                 width is considered to be 2\,em.      \\ \hlx{vhv.}
1263 % "Tl", "Tc" and "Tr" & Left, centre and right aligned
1264 %                 text.                                 \\ \hlx{vhhv[1]}
1265 %
1266 % \multicolumn{2}{|c|}{\bf Other modifier characters}   \\ \hlx{v[1]hv}
1267 % \bf Name      & \bf Meaning                           \\ \hlx{vhv.}
1268 % ":"           & Leaves a big gap between equations.
1269 %                 By default, the `chunks' separated by
1270 %                 \lit{:}s are equally spaced on the
1271 %                 line.                                 \\ \hlx{.}
1272 % "q"           & Inserts 1\,em of space                \\ \hlx{vhv.}
1273 % ">{"<text>"}" & Inserts \<text> just before the
1274 %                 actual column entry.                  \\ \hlx{.}
1275 % "<{"<text>"}" & Inserts \<text> just after the
1276 %                 actual column entry.                  \\ \hlx{vhv.}
1277 % "*{"<count>"}{"<chars>"}" & Inserts \<count>
1278 %                 copies of the \<chars> into the
1279 %                 preamble.                             \\ \hlx{vh}
1280 % \end{tabular}
1281 %
1282 % \caption{\package{eqnarray} column types and modifiers}
1283 % \label{tbl:eqnarray}
1284 % \end{table}
1285 %
1286 % The default preamble, if you don't supply one of your own, is \lit{rcl}.
1287 % Most of the time, \lit{rl} is sufficient, although compatibility is more
1288 % important to me.
1289 %
1290 % By default, there is no space between columns, which makes formul\ae\ in an
1291 % \env{eqnarray} environment look just like formul\ae\ typeset on their own,
1292 % except that things get aligned in columns.  This is where the default
1293 % \env{eqnarray} falls down: it leaves |\arraycolsep| space between each
1294 % column making the thing look horrible.
1295 %
1296 % An example would be good here, I think.  This one's from exercise 22.9 of
1297 % the \textit{\TeX book}.
1298 %
1299 % \begin{demo}[w]{Simultaneous equations}
1300 %\begin{eqnarray}[*3{rc}rl]
1301 %  10w & + &  3x & + & 3y & + & 18z & = 1 \\
1302 %   6w & - & 17x &   &    & - &  5z & = 2
1303 %\end{eqnarray}
1304 % \end{demo}
1305 %
1306 % Choosing a more up-to-date example, here's some examples from  the
1307 % \textit{\LaTeX\ Companion}.
1308 %
1309 % \begin{demo}[w]{Lots of equations}
1310 %\begin{eqnarray}[rl:rl:lq]
1311 % V_i &= v_i - q_i v_j, & X_i &= x_i - q_i x_j, &
1312 %       U_i = u_i, \qquad \mbox{for $i \ne j$}  \\
1313 % V_j &= v_j,           & X_j &= x_j            &
1314 %       U_j u_j + \sum_{i \ne j} q_i u_i. \label{eq:A}
1315 %\end{eqnarray}
1316 % \end{demo}
1317 %
1318 % \begin{figure}
1319 % \begin{demo}[w]{Plain text column and \cs{tabpause}}
1320 %\begin{eqnarray}[rlqqTl]
1321 %     x  &= y           & by (\ref{eq:A}) \\
1322 %     x' &= y'          & by definition \\
1323 %\tabpause{and}
1324 % x + x' &= y + y'      & by Axiom~1
1325 %\end{eqnarray}
1326 % \end{demo}
1327 % \end{figure}
1328 %
1329 % The new features also mean that you don't need to mess about with
1330 % |\lefteqn| any more.  This is handled by the \lit{L} column type:
1331 %
1332 % \begin{demo}{Splitting example}
1333 %\begin{eqnarray*}[Ll]
1334 %   w+x+y+z = \\
1335 %    & a+b+c+d+e+{} \\
1336 %    & f+g+h+i+j
1337 %\end{eqnarray*}
1338 % \end{demo}
1339 %
1340 % Finally, just to prove that the spacing's right at last, here's another one
1341 % from the \textit{Companion}.
1342 %
1343 % \begin{demo}{Spacing demonstration}
1344 %\begin{equation}
1345 %  x^2 + y^2 = z^2
1346 %\end{equation}
1347 %\begin{eqnarray}[rl]
1348 %  x^2 + y^2 &= z^2 \\
1349 %        y^2 &< z^2
1350 %\end{eqnarray}
1351 % \end{demo}
1352 %
1353 % Well, that was easy enough.  Now on to numbering.  As you've noticed, the
1354 % equations above are numbered.  You can use the \env{eqnarray$*$}
1355 % environment to turn off the numbering in the whole environment, or say
1356 % |\nonumber| on a line to suppress numbering of that one in particular.
1357 %
1358 % \DescribeMacro{\eqnumber}
1359 % More excitingly, you can say |\eqnumber| to enable numbering for a
1360 % particular equation, or \syntax{"\\eqnumber["<text>"]"} to choose what to
1361 % show instead of the line number.  This works for both starred and unstarred
1362 % versions of the environment.  Now |\nonumber| becomes merely a synonym for
1363 % `|\eqnumber[]|'.
1364 %
1365 % A note for cheats: you can use the sparkly new \env{eqnarray} for simple
1366 % equations by specifying \lit{x} as the column description.  Who needs
1367 % \AmSTeX?\ |;-)|
1368 %
1369 % \DescribeEnv{eqlines}
1370 % \DescribeEnv{eqlines*}
1371 % In fact, there's a separate environment \env{eqlines}, which is equivalent
1372 % to \env{eqnarray} with a single \lit{x} column; the result is that you can
1373 % insert a collection of displayed equations separated by |\\| commands.  If
1374 % you don't like numbering, use \env{eqlines$*$} instead.
1375 %
1376 % \subsubsection{The \env{eqnalign} environment}
1377 %
1378 % \DescribeEnv{eqnalign}
1379 % There's a new environment, \env{eqnalign}, which does almost the same
1380 % thing as \env{eqnarray} but not quite.  It doesn't do equation numbers,
1381 % and it wraps its contents up in a box.  The result of this is that:
1382 %
1383 % \begin{itemize}
1384 %
1385 % \item You can use \env{eqnalign} for just a part of a formula.
1386 %       The \env{eqnarray} environment must take up the whole display.
1387 %
1388 % \item You can use \env{eqnalign} within \env{eqnarray} for extra fine
1389 %       alignment of subsidiary bits.
1390 %
1391 % \item You can break off from doing an \env{eqnarray} using the |\tabpause|
1392 %       command.  You can't use |\tabpause| inside
1393 %       \env{eqnalign}.\footnote{^^A
1394 %         Well, technically speaking there's nothing to stop you.  However,
1395 %         the results won't be pretty.}
1396 %
1397 % \end{itemize}
1398 %
1399 % The \env{eqnalign} environment works like this:
1400 %
1401 % \begin{grammar}
1402 %
1403 % <eqnalign-env> ::= \[[
1404 %   <begin-eqnalign> <contents> <end-eqnalign>
1405 % \]]
1406 %
1407 % <begin-eqnalign> ::= \[[
1408 %   "\\begin" "{eqnalign}"
1409 %   \[ "[" \< <eqa-column> \> "]" \]
1410 %   \[ "[" \( "t" \\ "c" \\ "b" \) "]" \]
1411 % \]]
1412 %
1413 % <end-eqnalign> ::= \[[ "\\end" "{eqnalign}" \]]
1414 %
1415 % \end{grammar}
1416 %
1417 % As the syntax suggests, the preamble for the \env{eqnalign} environment
1418 % works exactly the same way as for \env{eqnarray}.  Example time: another
1419 % one from the \textit{\TeX book}.
1420 %
1421 % \begin{figure}
1422 % \begin{demo}[w]{Example of \env{eqnalign}}
1423 %\[
1424 %  \left\{ \begin{eqnalign}[rl]
1425 %    \alpha &= f(z) \\ \beta  &= f(z^2) \\
1426 %    \gamma &= f(z^3)
1427 %  \end{eqnalign} \right\}
1428 %  \qquad
1429 %  \left\{ \begin{eqnalign}[rl]
1430 %    x &= \alpha^2 - \beta \\ y &= 2\gamma
1431 %  \end{eqnalign} \right\}.
1432 %\]
1433 % \end{demo}
1434 % \end{figure}
1435 %
1436 % \DescribeMacro{\multicolumn}
1437 % The |\multicolumn| command works correctly in both the \env{eqnarray} and
1438 % \env{eqnalign} environments, although you should bear in mind that you
1439 % should give \env{eqnarray} column types, not \env{array} ones.
1440 %
1441 % \subsubsection{A note on spacing in alignment environments}
1442 %
1443 % Most of the time, equations in \env{eqnarray} and \env{eqnalign}
1444 % environments will be beautiful.  However, there are some things you should
1445 % bear in mind when you produce beautiful equations.
1446 %
1447 % The main problem with spacing is making sure that binary relations and
1448 % binary operators have the correct amount of space on each side of them.
1449 % The alignment environments insert `hidden' objects at the ends of table
1450 % cells to assist with the spacing: \lit{l} column types have a hidden object
1451 % on the left, \lit{r} types have a hidden object on the right, and \lit{c}
1452 % types have a hidden object on \emph{both} ends.  These hidden objects add
1453 % the correct space when there's a binary operator or relation next to them.
1454 % If some other sort of object is lurking there, no space is added.  So far,
1455 % so good.
1456 %
1457 % The only problem comes when you have something like this:
1458 %
1459 % \begin{demo}{How not to do an \env{eqnarray}}
1460 %\begin{eqnarray*}[rcl]
1461 %  x +  y & = & 12 \\
1462 % 2x - 5y & = & -6
1463 %\end{eqnarray*}
1464 % \end{demo}
1465 %
1466 % The `$-$' sign in the second equation has been treated as a binary operator
1467 % when really it should be a unary prefix operator, but \TeX\ isn't clever
1468 % enough to know the difference.  (Can you see the difference in the spacing
1469 % between $-6$~and~${}-6$?)  There are two possible solutions to the
1470 % problem.  You could wrap the `|-6|' up in a group (`|{-6}|'), or just the
1471 % $-$ sign (`|{-}6|').  A better plan, though, is to get rid of the middle
1472 % column altogether:
1473 %
1474 % \begin{demo}{How to do an \env{eqnarray}}
1475 %\begin{eqnarray*}[rl]
1476 %  x +  y & = 12 \\
1477 % 2x - 5y & = -6
1478 %\end{eqnarray*}
1479 % \end{demo}
1480 %
1481 % Since the things in the middle column were the same width, it's not
1482 % actually doing any good.  Also, now that \TeX\ can see that the thing on
1483 % the left of the `$-$' sign is a relation (the `$=$' sign), it will space
1484 % the formula correctly.
1485 %
1486 % In this case, it might be even better to add some extra columns, and line
1487 % up the $x$ and $y$ terms in the left hand side:
1488 %
1489 % \begin{demo}{Extra beautiful \env{eqnarray}}
1490 %\begin{eqnarray*}[rrl]
1491 %  x + &  y & = 12 \\
1492 % 2x - & 5y & = -6
1493 %\end{eqnarray*}
1494 % \end{demo}
1495 %
1496 % ^^A Some hacking now to display box sizes.
1497 %
1498 % {
1499 % \catcode`p=12 \catcode`t=12
1500 % \gdef\magni#1pt{#1}
1501 % }
1502 %
1503 % \newcommand{\widthof}[1]{^^A
1504 %   \settowidth{\dimen0 }{#1}^^A
1505 %   \expandafter\magni\the\dimen0\,pt^^A
1506 % }
1507 %
1508 % ^^A The text below makes an assumption which looks correct to me (I asked
1509 % ^^A TeX, and it agreed with me), although in case anything changes, I want
1510 % ^^A to be informed.
1511 %
1512 % \sbox0{$+$} \sbox2{$-$} \ifdim\wd0=\wd2\else%
1513 %   \errmessage{Assertion failed: `+' and `-' are different widths!}
1514 % \fi
1515 %
1516 % There's no need to put the `$+$' and `$-$' operators in their own column
1517 % here, because they're both \widthof{$+$} wide, even though they don't
1518 % look it.
1519 %
1520 % \subsubsection{Configuring the alignment environments}
1521 %
1522 % There are a collection of parameters you can use to make the equation
1523 % alignment environments (\env{eqnarray} and \env{eqnalign}) look the way
1524 % you like them.  These are all shown in table~\ref{tbl:eqnparms}.
1525 %
1526 % \begin{table}
1527 % \begin{tabular}[C]{| l | p{3in} |}                               \hlx{hv}
1528 % \bf Parameter         & \bf Use                               \\ \hlx{vhv}
1529 % |\eqaopenskip|        & Length put on the left of an
1530 %                         \env{eqnarray} environment.  By
1531 %                         default, this is |\@centering| (to
1532 %                         centre the alignment) or |\mathindent|
1533 %                         (to left align) depending on whether
1534 %                         you're using the \textsf{fleqn}
1535 %                         document class option.                \\
1536 % |\eqacloseskip|       & Length put on the right of an
1537 %                         \env{eqnarray} environment.  By
1538 %                         default, this is |\@centering|, to
1539 %                         align the environment correctly.      \\ \hlx{vhv}
1540 % |\eqacolskip|         & Space added by the \lit{:} column
1541 %                         modifier.  This should be a rubber
1542 %                         length, although it only stretches in
1543 %                         \env{eqnarray}, not in \env{eqnalign}.
1544 %                         The default value is 1\smallf1/2\,em
1545 %                         with 1000\,pt of stretch.             \\
1546 % |\eqainskip|          & Space added at each side of a normal
1547 %                         column.  By default this is 0\,pt.    \\ \hlx{vhv}
1548 % |\eqastyle|           & The maths style used in the alignment.
1549 %                         By default, this is |\textstyle|,
1550 %                         and you probably won't want to change
1551 %                         it.                                   \\ \hlx{vh}
1552 % \end{tabular}
1553 %
1554 % \caption{Parameters for the \env{eqnarray} and \env{eqnalign} environments}
1555 % \label{tbl:eqnparms}
1556 % \end{table}
1557 %
1558 %
1559 % \subsection{Other multiline equations}
1560 %
1561 % Sometimes there's no sensible alignment point for splitting equations.  The
1562 % normal thing to do under these circumstances is to put the first line way
1563 % over to the left of the page, and the last line over to the right.  (If
1564 % there are more lines, I imagine we put them in the middle.)
1565 %
1566 % \DescribeEnv{spliteqn}
1567 % \DescribeEnv{spliteqn*}
1568 % The \env{spliteqn} environment allows you to do such splitting of
1569 % equations.  Rather than tediously describe it, I'll just give an example,
1570 % because it's really easy.  The $*$-version works the same, except it
1571 % doesn't put an equation number in.
1572 %
1573 % \begin{figure}
1574 % \begin{demo}[w]{A split equation}
1575 %\begin{spliteqn}
1576 %  \sum_{1\le j\le n}
1577 %  \frac {1} { (x_j - x_1) \ldots (x_j - x_{j-1})
1578 %              (x - x_j) (x_j - x_{j+1}) \ldots (x_j - x_n) }
1579 %  \\
1580 %  = \frac {1} { (x - x_1) \ldots (x - x_n) }.
1581 %\end{spliteqn}
1582 % \end{demo}
1583 % \end{figure}
1584 %
1585 % \DescribeEnv{subsplit}
1586 % If you have a very badly behaved equation, you might want to split a part
1587 % of it (say, a bit of a fraction), particularly if you're doing things in
1588 % narrow columns.
1589 %
1590 % \begin{figure}
1591 % \begin{demo}[w]{A \env{subsplit} environment}
1592 %\begin{equation}
1593 %  \frac{
1594 %    \begin{subsplit}
1595 %      q^{\frac{1}{2} n(n+1)}(ea; q^2)_\infty (eq/a; q^2)_\infty \\
1596 %                 (caq/e; q^2)_\infty (cq^2/ae; q^2)_\infty
1597 %    \end{subsplit}
1598 %  }{
1599 %    (e; q)_\infty (cq/e; q)_\infty
1600 %  }
1601 %\end{equation}
1602 % \end{demo}
1603 % \end{figure}
1604 %
1605 % \subsection{Matrices}
1606 %
1607 % Also included in the \package{mathenv} package is a collection of things
1608 % for typesetting matrices.  The standard \env{array} doesn't (in my opinion)
1609 % provide the right sort of spacing for matrices.  \PlainTeX\ provides some
1610 % quite nice matrix handling macros, but they don't work in the appropriate
1611 % \LaTeX\ way.
1612 %
1613 % \textbf{Warning:} These definitions will make old versions of
1614 % \package{plain.sty} unhappy; newer versions correctly restore the
1615 % Plain~\TeX\ macros |\matrix| and |\pmatrix|.
1616 %
1617 % \DescribeEnv{matrix}
1618 % The simple way to do matrices is with the \env{matrix} environment.
1619 %
1620 % \begin{grammar}
1621 %
1622 % <matrix-env>  ::= \[[ <begin-matrix> <contents> <end-matrix> \]]
1623 %
1624 % <begin-matrix> ::= \[[ "\\begin{matrix}" \[ "[" <matrix-cols> "]" \] \]]
1625 %
1626 % <matrix-cols> ::= \[[
1627 %   \< \[ "[" \] \[ "T" \] \( "l" \\ "c" \\ "r" \) \>
1628 % \]]
1629 %
1630 % <end-matrix>  ::= \[[ "\\end{stack}" \]]
1631 %
1632 % \end{grammar}
1633 %
1634 % The \lit{l}, \lit{c} and \lit{r} columns are fairly obvious -- they align
1635 % their contents in the appropriate way.  The \lit{[} character is more
1636 % complicated.  It means `repeat the remaining column types forever', so a
1637 % preamble of \lit{cc[lr} means `two centred columns, then alternating left-
1638 % and right-aligned columns for as often as needed'.  The default preamble,
1639 % if you don't specify one, is \lit{[c} -- `any number of centred columns'.
1640 %
1641 % \DescribeMacro{\multicolumn}
1642 % The |\multicolumn| command works correctly in matrices, although you should
1643 % bear in mind that you should give \env{matrix} column types, not
1644 % \env{array} ones.
1645 %
1646 % \DescribeEnv{pmatrix}
1647 % The standard \env{matrix} environment doesn't put any delimiters around the
1648 % matrix.  You can use the standard |\left| and |\right| commands, although
1649 % this is a bit nasty.  The \env{pmatrix} environment will put parentheses
1650 % around the matrix it creates; it's otherwise exactly the same as
1651 % \env{matrix}.
1652 %
1653 % \DescribeEnv{dmatrix}
1654 % A \env{dmatrix} environment is also provided.  It takes two extra
1655 % arguments: the left and right delimiter characters (without |\left| or
1656 % |\right|).
1657 %
1658 % \begin{figure}
1659 % \begin{demo}[w]{Various \env{matrix} environments}
1660 %\[ \begin{matrix} 1 & 0 \\ 0 & -1 \end{matrix} \quad
1661 %   \begin{pmatrix}
1662 %     \cos\theta & \sin\theta \\
1663 %    -\sin\theta & \cos\theta
1664 %   \end{pmatrix} \quad
1665 %   \begin{dmatrix}[] 0 & -i \\ i & 0 \end{dmatrix}
1666 %\]
1667 % \end{demo}
1668 % \end{figure}
1669 %
1670 % \DescribeEnv{smatrix}
1671 % Normal matrices always come out the same size; they don't change size
1672 % according to the surrounding context (unfortunately).  However, it can be
1673 % occasionally useful to put matrices in running text, so you can talk about
1674 % $A$ being $\bigl( \begin{smatrix} a & b \\ b & c \end{smatrix} \bigr)$
1675 % being its own transpose (i.e., $A = A^T$).  This is accomplished using the
1676 % \env{smatrix} (the `s' stands for `small' -- I thought that `smallmatrix'
1677 % was too big to type inline).  As well as inline text, the \env{smatrix}
1678 % can be useful in displays, if the matrix is deep in a subformula.  I can't
1679 % think of any examples offhand, though.
1680 %
1681 % \DescribeEnv{spmatrix}
1682 % \DescribeEnv{sdmatrix}
1683 % The \env{smatrix} environment doesn't supply any delimiters, like
1684 % \env{matrix}.  There are \env{spmatrix} and \env{sdmatrix} environments
1685 % which do, though.  Note that delimiters have a tendency to get too big and
1686 % mess up the line spacing -- I had to use explicitly |\big| delimiters
1687 % in the above example.
1688 %
1689 % \DescribeEnv{pmatrix*}
1690 % \DescribeEnv{spmatrix*}
1691 % \DescribeEnv{sdmatrix*}
1692 % All the small matrix environments have starred versions, which are more
1693 % suitable for use in displays, since they have more space between the rows.
1694 % They're intended for typesetting really big matrices in displays.
1695 %
1696 % \DescribeMacro{\ddots}
1697 % \DescribeMacro{\vdots}
1698 % The standard |\vdots| and |\ddots| commands don't produce anything at all
1699 % nice in small matrices, so this package redefines them so that they scale
1700 % properly to smaller sizes.
1701 %
1702 % \DescribeEnv{genmatrix}
1703 % Actually, all these environments are special cases of one: \env{genmatrix}.
1704 % This takes oodles of arguments:
1705 % \begin{quote} \synshorts
1706 % "\\begin{genmatrix}{"<matrix-style>"}{"<outer-style>"}" \\
1707 % \null \qquad "{"<spacing>"}{"<left-delim>"}{"<right-delim>"}" \\
1708 % \null \quad\vdots \\
1709 % "\\end{genmatrix}"
1710 % \end{quote}
1711 % The two `style' arguments should be things like |\textstyle| or
1712 % |\scriptstyle|; the first, \<matrix-style>, is the style to use for the
1713 % matrix elements, and the second, \<outer-style>, is the style to assume
1714 % for the surrounding text (this affects the spacing within the matrix; it
1715 % should usually be the same as \<matrix-style>).  The \<spacing> is inserted
1716 % between the matrix and the delimiters, on each side of the matrix.  It's
1717 % usually `|\,|' in full-size matrices, and blank for small ones.  The
1718 % delimiters are inserted around the matrices, and sized appropriately.
1719 %
1720 % \DescribeEnv{newmatrix}
1721 % You can create your own matrix environments if you like, using the
1722 % |\newmatrix| command.  It takes two arguments, although they're a bit
1723 % odd.  The first is the name of the environment, and the second contains
1724 % the arguments to pass to \env{genmatrix}.  For example, the \env{pmatrix}
1725 % environment was defined by saying
1726 %
1727 % \begin{listing}
1728 %\newmatrix{pmatrix}{{\textstyle}{\textstyle}{\,}{(}{)}}
1729 % \end{listing}
1730 %
1731 % If you don't pass all three arguments, then you end up requiring the
1732 % user to specify the remaining ones.  This is how \env{dmatrix} works.
1733 %
1734 % \DescribeEnv{script}
1735 % Finally, although it's not really a matrix, stacked super- and subscripts
1736 % follow much the same sorts of spacing rules.  The \env{script} environment
1737 % allows you to do this sort of thing very easily.  It essentially provides
1738 % a `matrix' with the right sort of spacing.  The default preamble string is
1739 % \lit{c}, giving you centred scripts, although you can say
1740 % |\begin{script}[l]| for left-aligned scripts, which is better if the
1741 % script is being placed to the right of its operator.  If you're really
1742 % odd, you can have more than one column.
1743 %
1744 % \begin{demo}{Example of \env{script}}
1745 %\[ \mathop{{\sum}'}_{x \in A}
1746 %   f(x)
1747 %   \stackrel{\mathrm{def}}{=}
1748 %   \sum_{\begin{script}
1749 %     x \in A \\ x \ne 0
1750 %   \end{script}} f(x)
1751 %\]
1752 % \end{demo}
1753 %
1754 %
1755 % \subsection{Other \package{mathenv} environments}
1756 %
1757 % The \package{mathenv} package contains some other environments which may
1758 % be useful, based on the enhanced \env{tabular} and \env{array}
1759 % environments.
1760 %
1761 % \DescribeEnv{cases}
1762 % The \env{cases} environment lets you say things like the following:
1763 %
1764 % \begin{demo}[w]{Example of \env{cases}}
1765 %\[ P_{r-j} = \begin{cases}
1766 %               0 & if $r-j$ is odd \\
1767 %               r!\,(-1)^{(r-j)/2} & if $r-j$ is even
1768 %             \end{cases}
1769 %\]
1770 % \end{demo}
1771 %
1772 % The spacing required for this is a bit messy, so providing an environment
1773 % for it is quite handy.
1774 %
1775 % \DescribeEnv{smcases}
1776 % The \env{smcases} environment works the same way as \env{cases}, but with
1777 % scriptsize lettering.
1778 %
1779 % \implementation
1780 %
1781 %
1782 %^^A-------------------------------------------------------------------------
1783 % \section{Implementation of table handling}
1784 %
1785 %
1786 % Here we go.  It starts horrid and gets worse.  However, it does stay nicer
1787 % than the original, IMHO.
1788 %
1789 %    \begin{macrocode}
1790 %<*mdwtab>
1791 %    \end{macrocode}
1792 %
1793 %
1794 % \subsection{Registers, switches and things}
1795 %
1796 % We need lots of these.  It's great fun.
1797 %
1798 % The two count registers are simple enough:
1799 %
1800 % \begin{description}
1801 % \item [\cs{tab@state}] contains the current parser state.  Since we
1802 %       probably won't be parsing preambles recursively, this is a global
1803 %       variable.
1804 % \item [\cs{tab@columns}] contains the number of the current column.
1805 % \item [\cs{tab@hlstate}] contains the state required for hline management.
1806 % \item [\textsf{tabrow}] contains the row number in the table.  It's a
1807 %       proper \LaTeX\ counter.
1808 % \end{description}
1809 %
1810 %    \begin{macrocode}
1811 \newcount\tab@state
1812 \newcount\tab@columns
1813 \newcounter{tabrow}
1814 %    \end{macrocode}
1815 %
1816 % We need \emph{lots} of token registers.  Fortunately, most of them are only
1817 % used during parsing.  We'll use \PlainTeX's scratch tokens for this.  Note
1818 % that |\toks\tw@| isn't used here.  It, and |\toks@|, are free for use by
1819 % column commands.
1820 %
1821 %    \begin{macrocode}
1822 \newtoks\tab@preamble
1823 \newtoks\tab@shortline
1824 \toksdef\tab@pretext 4
1825 \toksdef\tab@posttext 6
1826 \toksdef\tab@userpretext 8
1827 %    \end{macrocode}
1828 %
1829 % The dimens are fairly straightforward.  The inclusion of |\col@sep| is a
1830 % sacrifice to compatibility -- judicious use of |\let| in \package{array}
1831 % would have saved a register.
1832 %
1833 %    \begin{macrocode}
1834 \newdimen\extrarowheight
1835 \newdimen\tabextrasep
1836 \newdimen\arrayextrasep
1837 \newdimen\smarraycolsep
1838 \newdimen\smarrayextrasep
1839 \newdimen\tab@width
1840 \newdimen\col@sep
1841 \newdimen\tab@endheight
1842 \newdimen\arraythickrulewidth
1843 \newdimen\tab@rulewidth
1844 %    \end{macrocode}
1845 %
1846 % Some skip registers too.  Phew.
1847 %
1848 %    \begin{macrocode}
1849 \newskip\tab@leftskip
1850 \newskip\tab@rightskip
1851 %    \end{macrocode}
1852 %
1853 % And some switches.  The first three are for the parser.
1854 %
1855 %    \begin{macrocode}
1856 \newif\iftab@firstcol
1857 \newif\iftab@initrule
1858 \newif\iftab@rule
1859 \newif\iftab@vgap
1860 \newif\iftab@colour
1861 %    \end{macrocode}
1862 %
1863 % Now assign some default values to new dimen parameters.  These definitions
1864 % are essentially the equivalent of an |\openup 1\jot| in \env{array}, but
1865 % not in \env{tabular}.  This looks nice, I think.
1866 %
1867 %    \begin{macrocode}
1868 \tabextrasep\z@
1869 \arrayextrasep\jot
1870 \smarraycolsep\thr@@\p@
1871 \smarrayextrasep\z@
1872 \arraythickrulewidth\p@
1873 %    \end{macrocode}
1874 %
1875 % Set some things up for alien table environments.
1876 %
1877 %    \begin{macrocode}
1878 \let\tab@extrasep\tabextrasep
1879 \let\tab@penalty\relax
1880 %    \end{macrocode}
1881 %
1882 %
1883 % \subsection{Options processing}
1884 %
1885 % Notice options, load package.
1886 %
1887 %    \begin{macrocode}
1888 \DeclareOption{colour}{\tab@colourtrue}
1889 \DeclareOption{color}{\tab@colourtrue}
1890 \ProcessOptions
1891 \RequirePackage{mdwkey}
1892 %    \end{macrocode}
1893 %
1894 % \subsection{Some little details}
1895 %
1896 % \begin{macro}{\@maybe@unskip}
1897 %
1898 % This macro solves a little problem.  In an alignment (and in other places)
1899 % it's desirable to suppress trailing space.  The usual method, to say
1900 % |\unskip|, is a little hamfisted, because it removes perfectly reasonable
1901 % aligning spaces like |\hfil|s.  While as a package writer I can deal with
1902 % this sort of thing by saying |\kern\z@| in appropriate places, it can
1903 % annoy users who are trying to use |\hfill| to override alignment in funny
1904 % places.
1905 %
1906 % My current solution seems to be acceptable.  I'll remove the natural width
1907 % of the last glue item, so that it can still stretch and shrink if
1908 % necessary.  The implementation makes use of the fact that multiplying
1909 % a \<skip> by a \<number> kills off the stretch.  (Bug fix: don't do this
1910 % when we're in vertical mode.)
1911 %
1912 %    \begin{macrocode}
1913 \def\@maybe@unskip{\ifhmode\hskip\m@ne\lastskip\relax\fi}
1914 %    \end{macrocode}
1915 %
1916 % \end{macro}
1917 %
1918 % \begin{macro}{\q@delim}
1919 %
1920 % Finally, for the sake of niceness, here's a delimiter token I can use
1921 % for various things.  It's a `quark', for what it's worth (i.e., it expands
1922 % to itself) although I'm not really sure why this is a good thing.  As far
1923 % as I'm concerned, it's important that it has a unique meaning (i.e., that
1924 % it won't be |\ifx|-equal to other things, or something undefined) and that
1925 % it won't be used where I don't expect it to be used.  \TeX\ will loop
1926 % horridly if it tries to expand this, so I don't think that quarks are
1927 % wonderfully clever thing to use.  (Maybe it should really expand to
1928 % something like `\syntax{<quark>"."}', which will rapidly fill \TeX's memory
1929 % if it gets accidentally expanded.  Still, I'll leave it as it is until
1930 % such time as I understand the idea more.)
1931 %
1932 %    \begin{macrocode}
1933 \def\q@delim{\q@delim}
1934 %    \end{macrocode}
1935 %
1936 % \end{macro}
1937 %
1938 %
1939 % \subsection{Parser states}
1940 %
1941 % Now we start on the parser.  It's really simple, deep down.  We progress
1942 % from state to state, extracting tokens from the preamble and building
1943 % command names from them.  Each command calls one of the element-building
1944 % routines, which works out which state it should be in.  We go through each
1945 % of the states in between (see later) doing default things for the ones we
1946 % missed out.
1947 %
1948 % Anyway, here's some symbolic names for the states.  It makes my life
1949 % easier.
1950 %
1951 %    \begin{macrocode}
1952 \chardef\tab@startstate 0
1953 \chardef\tab@loopstate 1
1954 \chardef\tab@rulestate 1
1955 \chardef\tab@prespcstate 2
1956 \chardef\tab@prestate 3
1957 \chardef\tab@colstate 4
1958 \chardef\tab@poststate 5
1959 \chardef\tab@postspcstate 6
1960 \chardef\tab@limitstate 7
1961 %    \end{macrocode}
1962 %
1963 %
1964 % \subsection{Adding things to token lists}
1965 %
1966 % Define some macros for adding stuff to the beginning and end of token
1967 % lists.  This is really easy, actually.  Here we go.
1968 %
1969 %    \begin{macrocode}
1970 \def\tab@append#1#2{#1\expandafter{\the#1#2}}
1971 \def\tab@prepend#1#2{%
1972   \toks@{#2}#1\expandafter{\the\expandafter\toks@\the#1}%
1973 }
1974 %    \end{macrocode}%
1975 %
1976 %
1977 % \subsection{Committing a column to the preamble}
1978 %
1979 % Each time we pass the `rule' state, we `commit' the tokens we've gathered
1980 % so far to the main preamble token list.  This is how we do it.  Note the
1981 % icky use of |\expandafter|.
1982 %
1983 %    \begin{macrocode}
1984 \def\tab@commit{%
1985 %    \end{macrocode}
1986 %
1987 % If this isn't the first column, then we need to put in a column separator.
1988 %
1989 %    \begin{macrocode}
1990   \iftab@firstcol\else%
1991     \expandafter\tab@append\expandafter\tab@preamble%
1992       \expandafter{\tab@tabtext}%
1993   \fi%
1994 %    \end{macrocode}
1995 %
1996 % Now dump in the |\tab@lefttext| material.
1997 %
1998 %    \begin{macrocode}
1999   \expandafter\tab@append\expandafter\tab@preamble%
2000     \expandafter{\tab@lefttext}%
2001 %    \end{macrocode}
2002 %
2003 % Now we spill the token registers into the main list in a funny order (which
2004 % is why we're doing it in this strange way in the first place.
2005 %
2006 %    \begin{macrocode}
2007   \toks@\expandafter{\tab@midtext}%
2008   \tab@preamble\expandafter{%
2009     \the\expandafter\tab@preamble%
2010     \the\expandafter\tab@pretext%
2011     \the\expandafter\tab@userpretext%
2012     \the\expandafter\toks@%
2013     \the\expandafter\tab@posttext%
2014     \tab@righttext%
2015   }%
2016 %    \end{macrocode}
2017 %
2018 % Now reset token lists and things for the next go round.
2019 %
2020 %    \begin{macrocode}
2021   \tab@firstcolfalse%
2022   \tab@pretext{}%
2023   \tab@userpretext{}%
2024   \tab@posttext{}%
2025 }
2026 %    \end{macrocode}
2027 %
2028 %
2029 % \subsection{Playing with parser states}
2030 %
2031 % \begin{macro}{\tab@setstate}
2032 %
2033 % This is how we set new states.  The algorithm is fairly simple, really.
2034 %
2035 % ^^A Let's see how good my TeX really is... ;-)
2036 % ^^A Actually, it doesn't seem to have worked out too badly.  Maybe I should
2037 % ^^A write a package to do this automatically.  It's rather tricky, though.
2038 %
2039 % \def\qq{\mbox{\quad}}
2040 % \sbox{0}{\itshape\textunderscore}\def\_{\usebox{0}}
2041 %
2042 % \begin{quote}
2043 % {\bf while} $\it tab\_state \ne s$ {\bf do} \\
2044 % \qq $\mathit{tab\_state = tab\_state}+1$; \\
2045 % \qq {\bf if} $\it tab\_state = tab\_limitState$ {\bf then}
2046 %                               $\it tab\_state=tab\_loopState$; \\
2047 % \qq {\bf if} $\it tab\_state = tab\_preSpcState$ {\bf then} \\
2048 % \qq \qq {\bf if} $\it tab\_initRule$ {\bf then} \\
2049 % \qq \qq \qq $\it tab\_initRule = {\bf false}$; \\
2050 % \qq \qq {\bf else} \\
2051 % \qq \qq \qq {\bf if} $\it tab\_inMultiCol$ {\bf then moan}; \\
2052 % \qq \qq \qq $\it commit$; \\
2053 % \qq \qq \qq $\it append(tab\_shortLine,\hbox{`|&\omit|')}$; \\
2054 % \qq \qq {\bf end\,if}; \\
2055 % \qq {\bf end\,if}; \\
2056 % \qq {\bf if} $\it tab\_state \ne s$ {\bf then}
2057 %                               $\it do\_default(tab\_state)$; \\
2058 % {\bf end\,while};
2059 % \end{quote}
2060 %
2061 % First we decide if there's anything to do.  If so, we call another macro to
2062 % do it for us.
2063 %
2064 %    \begin{macrocode}
2065 \def\tab@setstate#1{%
2066   \ifnum#1=\tab@state\else%
2067     \def\@tempa{\tab@setstate@i{#1}}%
2068     \@tempa%
2069   \fi%
2070 }
2071 %    \end{macrocode}
2072 %
2073 % This is where the fun is.  First we bump the state by one, and loop back
2074 % if we fall off the end.
2075 %
2076 %    \begin{macrocode}
2077 \def\tab@setstate@i#1{%
2078   \global\advance\tab@state\@ne%
2079   \ifnum\tab@state>\tab@limitstate%
2080     \global\tab@state\tab@loopstate%
2081   \fi%
2082 %    \end{macrocode}
2083 %
2084 % Now, if we've just passed the ruleoff state, we commit the current text
2085 % \emph{unless} this was the strange initial rule at the very beginning.  We
2086 % provide a little hook here so that |\multicolumn| can moan if you try and
2087 % give more than one column there.  We also add another tab/omit pair to the
2088 % list we use for |\vgap|.
2089 %
2090 %    \begin{macrocode}
2091   \ifnum\tab@state=\tab@prespcstate%
2092     \iftab@initrule%
2093       \tab@initrulefalse%
2094     \else%
2095       \tab@looped%
2096       \tab@commit%
2097       \expandafter\tab@append\expandafter\tab@shortline%
2098         \expandafter{\tab@rightruletext}%
2099       \tab@append\tab@shortline{&\omit}%
2100       \expandafter\tab@append\expandafter\tab@shortline%
2101         \expandafter{\tab@leftruletext}%
2102     \fi%
2103   \fi%
2104 %    \end{macrocode}
2105 %
2106 % Now we decide whether to go round again.  If not, we do the default thing
2107 % for this state.  This is mainly here so that we can put the |\tabcolsep| or
2108 % whatever in if the user didn't give an \lit{@} expression.
2109 %
2110 %    \begin{macrocode}
2111   \ifnum#1=\tab@state%
2112     \let\@tempa\relax%
2113   \else%
2114     \csname tab@default@\number\tab@state\endcsname%
2115   \fi%
2116   \@tempa%
2117 }
2118 %    \end{macrocode}
2119 %
2120 % \end{macro}
2121 %
2122 % Now we set up the default actions for the various states.
2123 %
2124 % In state~2 (pre-space) we add in the default gap if either we didn't have
2125 % an \lit{@} expression in the post-space state or there was an explicit
2126 % intervening rule.
2127 %
2128 %    \begin{macrocode}
2129 \@namedef{tab@default@2}{%
2130   \iftab@rule%
2131     \tab@append\tab@pretext{\hskip\col@sep}%
2132   \fi%
2133 }
2134 %    \end{macrocode}
2135 %
2136 % If the user omits the column type, we insert an `l'-type column and moan
2137 % a lot.
2138 %
2139 %    \begin{macrocode}
2140 \@namedef{tab@default@4}{%
2141   \tab@err@misscol%
2142   \tab@append\tab@pretext{\tab@bgroup\relax}%
2143   \tab@append\tab@posttext{\relax\tab@egroup\hfil}%
2144   \tab@append\tab@shortline{\hfil}%
2145   \advance\tab@columns\@ne%
2146 }
2147 %    \end{macrocode}
2148 %
2149 % Finally we deal with the post-space state.  We set a marker so that we
2150 % put in the default space in the pre-space state later too.
2151 %
2152 %    \begin{macrocode}
2153 \@namedef{tab@default@6}{%
2154   \tab@append\tab@posttext{\hskip\col@sep}%
2155   \tab@ruletrue%
2156 }
2157 %    \end{macrocode}
2158 %
2159 %
2160 % \subsection{Declaring token types}
2161 %
2162 % \begin{macro}{\tab@extracol}
2163 %
2164 % Before we start, we need to handle |\extracolsep|.  This is a right pain,
2165 % because the original version of \env{tabular} worked on total expansion,
2166 % which is a Bad Thing.  On the other hand, turning |\extracolsep| into a
2167 % |\tabskip| is also a major pain.
2168 %
2169 %    \begin{macrocode}
2170 \def\tab@extracol#1#2{\tab@extracol@i#1#2\extracolsep{}\extracolsep\end}
2171 \def\tab@extracol@i#1#2\extracolsep#3#4\extracolsep#5\end{%
2172   \ifx @#3@%
2173     \def\@tempa{#1{#2}}%
2174   \else%
2175     \def\@tempa{#1{#2\tabskip#3\relax#4}}%
2176   \fi%
2177   \@tempa%
2178 }
2179 %    \end{macrocode}
2180 %
2181 % \end{macro}
2182 %
2183 % This is where we do the work for inserting preamble elements.
2184 %
2185 % \begin{macro}{\tabruletype}
2186 %
2187 % Inserting rules is interesting, because we have to decide where to put
2188 % them.  If this is the funny initial rule, it goes in the pre-text list,
2189 % otherwise it goes in the post-text list.  We work out what to do first
2190 % thing:
2191 %
2192 %    \begin{macrocode}
2193 \def\tabruletype#1{\tab@extracol\tabruletype@i{#1}}%
2194 \def\tabruletype@i#1{%
2195   \iftab@initrule%
2196     \let\tab@tok\tab@pretext%
2197   \else%
2198     \let\tab@tok\tab@posttext%
2199   \fi%
2200 %    \end{macrocode}
2201 %
2202 % Now if we're already in the rule state, we must have just done a rule.
2203 % This means we must put in the |\doublerulesep| space, both here and in the
2204 % shortline list.  Otherwise we just stick the rule in.
2205 %
2206 % This is complicated, because |\vgap| needs to be able to remove some bits
2207 % of rule.  We pass each one to a macro |\tab@ckr|, together with the column
2208 % number, which is carefully bumped at the right times, and this macro will
2209 % vet the rules and output the appropriate ones.  There's lots of extreme
2210 % |\expandafter| nastiness as a result.  Amazingly, this actually works.
2211 %
2212 %    \begin{macrocode}
2213   \ifnum\tab@state=\tab@rulestate%
2214     \tab@append\tab@tok{\hskip\doublerulesep\begingroup#1\endgroup}%
2215     \expandafter\tab@append\expandafter\tab@shortline\expandafter{%
2216       \expandafter\hskip\expandafter\doublerulesep%
2217       \expandafter\tab@ckr\expandafter{\the\tab@columns}%
2218         {\begingroup#1\endgroup}%
2219     }%
2220   \else%
2221     \tab@setstate\tab@rulestate%
2222     \tab@append\tab@tok{\begingroup#1\endgroup}%
2223     \expandafter\tab@append\expandafter\tab@shortline\expandafter{%
2224       \expandafter\tab@ckr\expandafter{\the\tab@columns}%
2225         {\begingroup#1\endgroup}%
2226     }%
2227   \fi%
2228 %    \end{macrocode}
2229 %
2230 % Finally, we say there was a rule here, so that default space gets put in
2231 % after this.  Otherwise we lose lots of generality.
2232 %
2233 %    \begin{macrocode}
2234   \tab@ruletrue%
2235 }
2236 %    \end{macrocode}
2237 %
2238 % \end{macro}
2239 %
2240 % \begin{macro}{\tabspctype}
2241 %
2242 % We need to work out which space-state we should be in.  Then we just put
2243 % the text in.  Easy, really.
2244 %
2245 %    \begin{macrocode}
2246 \def\tabspctype#1{\tab@extracol\tabspctype@i{#1}}%
2247 \def\tabspctype@i#1{%
2248   \tab@rulefalse%
2249   \ifnum\tab@state>\tab@prespcstate%
2250     \tab@setstate\tab@postspcstate%
2251     \let\tab@tok\tab@posttext%
2252   \else%
2253     \tab@setstate\tab@prespcstate%
2254     \let\tab@tok\tab@pretext%
2255   \fi%
2256   \tab@append\tab@tok{\begingroup#1\endgroup}%
2257 }
2258 %    \end{macrocode}
2259 %
2260 % \end{macro}
2261 %
2262 % \begin{macro}{\tabcoltype}
2263 %
2264 % If we're already in the column state, we bump the state and loop round
2265 % again, to get all the appropriate default behaviour.  We bump the column
2266 % counter, and add the bits of text we were given to appropriate token lists.
2267 % We also add the |\hfil| glue to the shortline list, to space out the rules
2268 % properly.
2269 %
2270 %    \begin{macrocode}
2271 \def\tabcoltype#1#2{%
2272   \ifnum\tab@state=\tab@colstate%
2273     \global\advance\tab@state\@ne%
2274   \fi%
2275   \advance\tab@columns\@ne%
2276   \tab@setstate\tab@colstate%
2277   \tab@append\tab@pretext{#1}%
2278   \tab@append\tab@posttext{#2}%
2279   \tab@append\tab@shortline{\hfil}%
2280 }
2281 %    \end{macrocode}
2282 %
2283 % \end{macro}
2284 %
2285 % \begin{macro}{\tabuserpretype}
2286 % \begin{macro}{\tabuserposttype}
2287 %
2288 % These are both utterly trivial.
2289 %
2290 %    \begin{macrocode}
2291 \def\tabuserpretype#1{%
2292   \tab@setstate\tab@prestate%
2293   \tab@prepend\tab@userpretext{#1}%
2294 }
2295 %    \end{macrocode}
2296 %
2297 %    \begin{macrocode}
2298 \def\tabuserposttype#1{%
2299   \tab@setstate\tab@poststate%
2300   \tab@prepend\tab@posttext{#1}%
2301 }
2302 %    \end{macrocode}
2303 %
2304 % \end{macro}
2305 % \end{macro}
2306 %
2307 %
2308 % \subsection{The colset stack}
2309 %
2310 % Let's start with something fairly easy.  We'll keep a stack of column sets
2311 % so that users don't get confused by package authors changing the current
2312 % column set.  This is fairly easy, really.
2313 %
2314 % \begin{macro}{\tab@push}
2315 % \begin{macro}{\tab@pop}
2316 % \begin{macro}{\tab@head}
2317 %
2318 % These are the stack management routines.  The only important thing to note
2319 % is that |\tab@head| must take place \emph{only} in \TeX's mouth, so we can
2320 % use it in |\csname|\dots|\endcsname| constructions.
2321 %
2322 %    \begin{macrocode}
2323 \def\tab@push#1#2{%
2324   \toks@{{#2}}%
2325   \expandafter\def\expandafter#1\expandafter{\the\expandafter\toks@#1}%
2326 }
2327 \def\tab@pop#1{\edef#1{\expandafter\@gobble#1}}
2328 \def\tab@head#1{\expandafter\tab@head@i#1\relax}
2329 \def\tab@head@i#1#2\relax{#1}
2330 %    \end{macrocode}
2331 %
2332 % \end{macro}
2333 % \end{macro}
2334 % \end{macro}
2335 %
2336 % \begin{macro}{\colset}
2337 % \begin{macro}{\colpush}
2338 % \begin{macro}{\colpop}
2339 %
2340 % Now we can define the user macros.
2341 %
2342 %    \begin{macrocode}
2343 \def\tab@colstack{{tabular}}
2344 \def\colset{\colpop\colpush}
2345 \def\colpush{\tab@push\tab@colstack}
2346 \def\colpop{\tab@pop\tab@colstack}
2347 %    \end{macrocode}
2348 %
2349 % \end{macro}
2350 % \end{macro}
2351 % \end{macro}
2352 %
2353 % \begin{macro}{\tab@colset}
2354 %
2355 % Now we define a shortcut for reading the top item off the stack.
2356 %
2357 %    \begin{macrocode}
2358 \def\tab@colset{\tab@head\tab@colstack}
2359 %    \end{macrocode}
2360 %
2361 % \end{macro}
2362 %
2363 %
2364 % \subsection{The main parser routine}
2365 %
2366 % \begin{macro}{\tab@initread}
2367 %
2368 % This macro sets up lots of variables to their normal states prior to
2369 % parsing a preamble.  Some things may need changing, but not many.  This
2370 % version just sets the major hooks, and then does a subread.  The midtext
2371 % macro contains what to put in the very middle of each template --
2372 % |\multicolumn| will insert its argument here.
2373 %
2374 %    \begin{macrocode}
2375 \def\tab@initread{%
2376   \def\tab@lefttext{}%
2377   \def\tab@leftruletext{}%
2378   \def\tab@righttext{}%
2379   \def\tab@rightruletext{}%
2380   \def\tab@tabtext{&}%
2381   \def\tab@midtext{\ignorespaces####\@maybe@unskip}%
2382   \tab@initsubread%
2383 }
2384 %    \end{macrocode}
2385 %
2386 % \end{macro}
2387 %
2388 % \begin{macro}{\tab@initsubread}
2389 %
2390 % This is where most of the activity is.  We don't replace the left and right
2391 % texts, so that we effectively inherit them rfrom the enclosing
2392 % environment.
2393 %
2394 %    \begin{macrocode}
2395 \def\tab@initsubread{%
2396 %    \end{macrocode}
2397 %
2398 % First, reset the parser state to the start state.
2399 %
2400 %    \begin{macrocode}
2401   \global\tab@state\tab@startstate%
2402 %    \end{macrocode}
2403 %
2404 % We clear the token lists to sensible values, mostly.
2405 %
2406 %    \begin{macrocode}
2407   \tab@preamble{}%
2408   \tab@shortline{}%
2409   \tab@pretext{}%
2410   \tab@userpretext{}%
2411   \tab@posttext{}%
2412   \let\tab@multicol\@empty%
2413   \def\tab@startpause{\penalty\postdisplaypenalty\medskip}%
2414   \def\tab@endpause{\penalty\predisplaypenalty\medskip}%
2415 %    \end{macrocode}
2416 %
2417 % Finally, reset the column counter, don't raise errors when we loop, and set
2418 % some parser flags to their appropriate values.
2419 %
2420 %    \begin{macrocode}
2421   \tab@columns\z@%
2422   \let\tab@looped\relax%
2423   \tab@ruletrue%
2424   \tab@initruletrue%
2425   \tab@firstcoltrue%
2426 }
2427
2428 %    \end{macrocode}
2429 %
2430 % \end{macro}
2431 %
2432 % \begin{macro}{\tab@readpreamble}
2433 %
2434 % This is the main macro for preamble handling.  Actually, all it does is
2435 % gobble its argument's leading brace and call another macro, but it does it
2436 % with style.
2437 %
2438 %    \begin{macrocode}
2439 \def\tab@readpreamble#1{%
2440   \expandafter\tab@append\expandafter\tab@shortline%
2441     \expandafter{\tab@leftruletext}%
2442   \tab@doreadpream{#1}%
2443   \iftab@initrule\global\tab@state\tab@prespcstate\fi%
2444   \tab@setstate\tab@rulestate%
2445   \expandafter\tab@append\expandafter\tab@shortline%
2446     \expandafter{\tab@rightruletext}%
2447   \tab@commit%
2448 }
2449 %    \end{macrocode}
2450 %
2451 % \end{macro}
2452 %
2453 % \begin{macro}{\tab@doreadpream}
2454 %
2455 % The preamble is in an argument.  Previous versions used a nasty trick using
2456 % |\let| and |\afterassignment|.  Now we use an explicit end token, to allow
2457 % dodgy column type handlers to scoop up the remaining preamble tokens
2458 % and process them.  Not that anyone would want to do that, oh no (see
2459 % the \lit{[} type in the \env{eqnarray} environment |;-)|).
2460 %
2461 %    \begin{macrocode}
2462 \def\tab@doreadpream#1{\tab@mkpreamble#1\q@delim}
2463 %    \end{macrocode}
2464 %
2465 % \end{macro}
2466 %
2467 % \begin{macro}{\tab@mkpreamble}
2468 %
2469 % This is the main parser routine.  It takes each token in turn, scrutinises
2470 % it carefully, and does the appropriate thing with it.
2471 %
2472 % The preamble was given as an argument to |\tab@doreadpream|, and that has
2473 % helpfully stripped off the initial |{| character.  We need to pick off the
2474 % next token (whatever it is) so we can examine it.  We'll use |\futurelet|
2475 % so we can detect groups and things in funny places.
2476 %
2477 %    \begin{macrocode}
2478 \def\tab@mkpreamble{\futurelet\@let@token\tab@mkpreamble@i}
2479 %    \end{macrocode}
2480 %
2481 % If we find a space token, we'll go off and do something a bit special,
2482 % since spaces are sort of hard to handle.  Otherwise we'll do it in the old
2483 % fashioned way.
2484 %
2485 %    \begin{macrocode}
2486 \def\tab@mkpreamble@i{%
2487   \ifx\@let@token\@sptoken%
2488     \expandafter\tab@mkpreamble@spc%
2489   \else%
2490     \expandafter\tab@mkpreamble@ii%
2491   \fi%
2492 }
2493 %    \end{macrocode}
2494 %
2495 % If we find a |\@@endpreamble| token, that's it and we're finished.  We just
2496 % gobble it and return.  Otherwise, if it's an open group character, we'll
2497 % complain because someone's probably tried to put an argument in the wrong
2498 % place.  Finally, if none of the other things apply, we'll deal with the
2499 % character below.
2500 %
2501 %    \begin{macrocode}
2502 \def\tab@mkpreamble@ii{%
2503   \ifx\@let@token\q@delim%
2504     \def\@tempa{\let\@let@token}%
2505   \else%
2506     \ifcat\bgroup\noexpand\@let@token%
2507       \tab@err@oddgroup%
2508       \def\@tempa##1{\tab@mkpreamble}%
2509     \else%
2510       \let\@tempa\tab@mkpreamble@iii%
2511     \fi%
2512   \fi%
2513   \@tempa%
2514 }
2515 %    \end{macrocode}
2516 %
2517 % Handle a character.  This involves checking to see if it's actually
2518 % defined, and then doing it.  Doing things this way means we won't get
2519 % stranded in mid-preamble unless a package author has blown it.
2520 %
2521 %    \begin{macrocode}
2522 \def\tab@mkpreamble@iii#1{%
2523   \@ifundefined{\tab@colset!col.\string#1}{%
2524     \tab@err@undef{#1}\tab@mkpreamble%
2525   }{%
2526     \@nameuse{\tab@colset!col.\string#1}%
2527   }%
2528 }
2529 %    \end{macrocode}
2530 %
2531 % If we get given a space character, we'll look up the command name as
2532 % before.  If no-one's defined the column type we'll just skip it silently,
2533 % which lets users do pretty formatting if they like.
2534 %
2535 %    \begin{macrocode}
2536 \@namedef{tab@mkpreamble@spc} {%
2537   \@ifundefined{\tab@colset!col. }{%
2538     \tab@mkpreamble%
2539   }{%
2540     \@nameuse{\tab@colset!col. }%
2541   }%
2542 }
2543 %    \end{macrocode}
2544 %
2545 % \end{macro}
2546 %
2547 % \begin{macro}{\coldef}
2548 %
2549 % Here's how to define column types the nice way.  Some dexterity is required
2550 % to make everything work right, but it's simple really.
2551 %
2552 %    \begin{macrocode}
2553 \def\coldef{\@testopt\coldef@i\tab@colset}
2554 \def\coldef@i[#1]#2#3#{\coldef@ii[#1]{#2}{#3}}
2555 \def\coldef@ii[#1]#2#3#4{%
2556   \expandafter\def\csname#1!col.\string#2\endcsname#3{%
2557     #4\tab@mkpreamble%
2558   }%
2559 }
2560 %    \end{macrocode}
2561 %
2562 % \end{macro}
2563 %
2564 % \begin{macro}{\collet}
2565 %
2566 % We'd like to let people copy column types from other places.  This is how
2567 % to do it.
2568 %
2569 %    \begin{macrocode}
2570 \def\collet{\@testopt\collet@i\tab@colset}
2571 \def\collet@i[#1]#2{%
2572   \@ifnextchar=%
2573     {\collet@ii[#1]{#2}}%
2574     {\collet@ii[#1]{#2}=}%
2575 }
2576 \def\collet@ii[#1]#2={\@testopt{\collet@iii[#1]{#2}}\tab@colset}
2577 \def\collet@iii[#1]#2[#3]#4{%
2578   \expandafter\let\csname#1!col.\string#2\expandafter\endcsname%
2579                   \csname#3!col.\string#4\endcsname%
2580 }
2581 %    \end{macrocode}
2582 %
2583 % \end{macro}
2584 %
2585 % \begin{macro}{\newcolumntype}
2586 %
2587 % We just bundle the text off to |\newcommand| and expect it to cope.  It
2588 % ought to.  The column type code inserts the user's tokens directly, rather
2589 % than calling |\tab@doreadpream| recursively.  The magic control sequence
2590 % is the one looked up by the parser.
2591 %
2592 % There's some additional magic here for compatibility with the obscure way
2593 % that \package{array} works.
2594 %
2595 %    \begin{macrocode}
2596 \def\newcolumntype#1{\@testopt{\nct@i{#1}}0}
2597 \def\nct@i#1[#2]{\@ifnextchar[{\nct@ii{#1}[#2]}{\nct@iii{#1}{[#2]}}}
2598 \def\nct@ii#1[#2][#3]{\nct@iii{#1}{[#2][#3]}}
2599 \def\nct@iii#1#2#3{%
2600   \expandafter\let\csname\tab@colset!col.\string#1\endcsname\relax%
2601   \expandafter\newcommand\csname\tab@colset!col.\string#1\endcsname#2{%
2602     \tab@deepmagic{#1}%
2603     \tab@mkpreamble%
2604     #3%
2605   }%
2606 }
2607 %    \end{macrocode}
2608 %
2609 % Now for some hacking for compatibility with \package{tabularx}.
2610 %
2611 %    \begin{macrocode}
2612 \def\newcol@#1[#2]{\nct@iii{#1}{[#2]}}
2613 %    \end{macrocode}
2614 %
2615 % And now some more.  This is seriously deep magic.  Hence the name.
2616 %
2617 %    \begin{macrocode}
2618 \def\tab@deepmagic#1{%
2619   \csname NC@rewrite@\string#1\endcsname\NC@find\tab@@magic@@%
2620 }
2621 \def\NC@find#1\tab@@magic@@{}
2622 %    \end{macrocode}
2623 %
2624 % \end{macro}
2625 %
2626 %
2627 % \subsection{Standard column types}
2628 %
2629 % First, make sure we're setting up the right columns.  This also sets the
2630 % default for the user.  Other packages must not use the |\colset| command
2631 % for defining columns -- they should use the stack operations defined above.
2632 % For colour support, we ensure that the total stretch in a table cell is
2633 % 1\,fil.
2634 %
2635 %    \begin{macrocode}
2636 \def\tab@halfhfil{\hskip\z@\@plus.5fil\relax}
2637 %    \end{macrocode}
2638 %
2639 % And now on with the show.
2640 %
2641 %    \begin{macrocode}
2642 \colset{tabular}
2643 %    \end{macrocode}
2644 %
2645 % Now do the simple alignment types.  These are fairly simple.  The
2646 % mysterious kern in the \lit{l} type is to stop the |\col@sep| glue from
2647 % vanishing due to the |\unskip| inserted by the standard |\tab@midtext| if
2648 % the column contains no text.  (Thanks for spotting this bug go to that
2649 % nice Mr~Carlisle.)
2650 %
2651 %    \begin{macrocode}
2652 \coldef l{\tabcoltype{\kern\z@\tab@bgroup}{\tab@egroup\hfil}}
2653 \coldef c{\tabcoltype{\tab@halfhfil\tab@bgroup}{\tab@egroup\tab@halfhfil}}
2654 \coldef r{\tabcoltype{\hfil\tab@bgroup}{\tab@egroup}}
2655 %    \end{macrocode}
2656 %
2657 % Some extensions now.  These are explicitly textual or mathematical
2658 % columns.  Can be useful if you're providing column types for other people.
2659 % I've inserted a kern here for exactly the same reason as for the \lit{l}
2660 % column type above.
2661 %
2662 %    \begin{macrocode}
2663 \coldef T#1{\tab@aligncol{#1}{\tab@btext}{\tab@etext}}
2664 \coldef M#1{\tab@aligncol{#1}{\tab@bmaths}{\tab@emaths}}
2665 \def\tab@aligncol#1#2#3{%
2666   \if#1l\tabcoltype{\kern\z@#2}{#3\hfil}\fi%
2667   \if#1c\tabcoltype{\tab@halfhfil#2}{#3\tab@halfhfil}\fi%
2668   \if#1r\tabcoltype{\hfil#2}{#3}\fi%
2669 }
2670 %    \end{macrocode}
2671 %
2672 % Now for the default rules.
2673 %
2674 %    \begin{macrocode}
2675 \coldef ${\@firstoftwo{\tab@withrp\tab@vrule}}
2676 \coldef |{\@firstoftwo{\tab@withrp\tab@vrule[]}}
2677 \def\tab@vrule#1{\tabruletype{#1\vrule\@width\dimen@}\tab@mkpreamble}
2678 \coldef !#1{\tabruletype{#1}}
2679 %    \end{macrocode}
2680 %
2681 % Deal with \lit{@} expressions.
2682 %
2683 %    \begin{macrocode}
2684 \coldef @#1{\tabspctype{#1}}
2685 %    \end{macrocode}
2686 %
2687 % And the paragraph types.  I've added things to handle footnotes here.
2688 %
2689 %    \begin{macrocode}
2690 \coldef p#1{\tabcoltype%
2691              {\savenotes\vtop\tab@bpar{#1}}%
2692              {\tab@epar\spewnotes\hfil}}
2693 \coldef m#1{\tabcoltype%
2694              {\savenotes$\vcenter\tab@bpar{#1}}%
2695              {\tab@epar$\spewnotes\hfil}}
2696 \coldef b#1{\tabcoltype%
2697              {\savenotes\vbox\tab@bpar{#1}}%
2698              {\tab@epar\spewnotes\hfil}}
2699 %    \end{macrocode}
2700 %
2701 % Phew.  Only a few more left now.  The user text ones.
2702 %
2703 %    \begin{macrocode}
2704 \coldef >#1{\tabuserpretype{#1}}
2705 \coldef <#1{\tabuserposttype{#1}}
2706 \coldef ?#1#2{%
2707   \ifx>#1\expandafter\tabuserpretype%
2708   \else\expandafter\tabuserposttype\fi%
2709   {#2}%
2710   \tab@append\tab@shortline{#2}%
2711 }
2712 \coldef '#1{%
2713   \tab@append\tab@shortline{#1}%
2714 }
2715 %    \end{macrocode}
2716 %
2717 % The strange column type.
2718 %
2719 %    \begin{macrocode}
2720 \coldef ##1#2{\tabcoltype{#1}{#2}}
2721 %    \end{macrocode}
2722 %
2723 % And \lit{*}, which repeats a preamble spec.  The tricky part is ensuring
2724 % that nested \lit{*} specs work correctly: we must save the loop counter, in
2725 % |\count 0|, obviously, but \emph{also} the |\iterate| macro built which
2726 % |\loop| modifies internally.  Usually you'd use grouping to fix this, but
2727 % if we introduce a group level then we won't update the preamble registers
2728 % correctly.  Instead, queue up tokens to restore the values in \TeX's input
2729 % list.
2730 %
2731 %    \begin{macrocode}
2732 \coldef *#1#2{%
2733   \toks\tw@\expandafter{\iterate}%
2734   \toks@{%
2735     \count@#1%
2736     \loop\ifnum\count@>0\relax%
2737       \tab@doreadpream{#2}%
2738       \advance\count@\m@ne%
2739     \repeat%
2740   }%
2741   \edef\@tempa{%
2742     \the\toks@%
2743     \def\noexpand\iterate{\the\toks\tw@}%
2744     \count@\the\count@%
2745   }%
2746   \@tempa%
2747 }
2748 %    \end{macrocode}
2749 %
2750 %
2751 % \subsection{Paragraph handling}
2752 %
2753 % First of all, starting new paragraphs: the vbox token is already there, and
2754 % we have the width as an argument.
2755 %
2756 % \begin{macro}{\tab@bpar}
2757 %
2758 % There are some gymnastics to do here to support lists which form the
2759 % complete text of the parbox.  One of the odd things I'll do here is to
2760 % not insert a strut on the first line: instead, I'll put the text into a
2761 % box register so that I can inspect it later.  So that I have access to
2762 % the height of the first line, I'll use a |\vtop| -- I can get at the
2763 % final depth by using |\prevdepth|, so this seems to be the most general
2764 % solution.
2765 %
2766 %    \begin{macrocode}
2767 \def\tab@bpar#1{%
2768   \bgroup%
2769   \setlength\hsize{#1}%
2770   \@arrayparboxrestore%
2771   \setbox\z@\vtop\bgroup%
2772   \global\@minipagetrue%
2773   \global\@noskipsecfalse%
2774   \everypar\expandafter{\the\everypar%
2775     \global\@minipagefalse%
2776     \everypar{}%
2777   }%
2778 }
2779 %    \end{macrocode}
2780 %
2781 % \end{macro}
2782 %
2783 % \begin{macro}{\tab@epar}
2784 %
2785 % To end the paragraph, close the box.  That sounds easy, doesn't it?
2786 % I need to space out the top and bottom of the box so that it looks as if
2787 % struts have been applied.
2788 %
2789 %    \begin{macrocode}
2790 \def\tab@epar{%
2791 %    \end{macrocode}
2792 %
2793 % Anyway, I should end the current paragraph if I'm still in horizontal
2794 % mode.  A simple |\par| will do this nicely.  I'll also remove any trailing
2795 % vertical glue (which may be left there by a list environment), because
2796 % things will look very strange otherwise.
2797 %
2798 %    \begin{macrocode}
2799   \ifhmode\@maybe@unskip\par\fi%
2800   \unskip%
2801 %    \end{macrocode}
2802 %
2803 % Now I'll look at the depth of the last box: if it's less deep than my
2804 % special strut, I'll cunningly backpedal by a bit, and add a box with the
2805 % appropriate depth.  Since this will lie on the previous baseline, it won't
2806 % alter the effective height of the box.  There's a snag here.  |\prevdepth|
2807 % may be wrong for example if the last thing inserted was a rule, or the
2808 % box is just empty.  Check for this specially.  (Thanks to Rowland McDonnell
2809 % for spotting this.)
2810 %
2811 %    \begin{macrocode}
2812   \ifdim\prevdepth>-\@m\p@\ifdim\prevdepth<\dp\@arstrutbox%
2813     \kern-\prevdepth%
2814     \nointerlineskip%
2815     \vtop to\dp\@arstrutbox{}%
2816   \fi\fi%
2817 %    \end{macrocode}
2818 %
2819 % I've finished the bottom of the box now: I'll close it, and start work on
2820 % the top again.
2821 %
2822 %    \begin{macrocode}
2823   \egroup%
2824 %    \end{macrocode}
2825 %
2826 % For top-alignment to work, the first item in the box must be another box.
2827 % (This is why I couldn't just set |\prevdepth| at the beginning.)  If the
2828 % box isn't high enough, I'll add a box of the right height and then kern
2829 % backwards so that the `real' first box ends up in the right place.
2830 %
2831 %    \begin{macrocode}
2832   \ifdim\ht\z@<\ht\@arstrutbox%
2833     \vbox to\ht\@arstrutbox{}%
2834     \kern-\ht\z@%
2835   \fi%
2836   \unvbox\z@%
2837   \egroup%
2838 }
2839 %    \end{macrocode}
2840 %
2841 % \end{macro}
2842 %
2843 %
2844 % \subsection{Gentle persuasion}
2845 %
2846 % To persuade \package{longtable} to work, we emulate some features of
2847 % the \package{array} way of doing things.  It's a shame, but we have to do
2848 % it, because \package{longtable} came first.
2849 %
2850 % Note the horribleness with the grouping here.  In order to get everything
2851 % expanded at the right time, |\@preamble| just replaces itself with the (not
2852 % expanded!) preamble string, using |\the|.  This means that the preamble
2853 % string must be visible in the group just above us.  Now,
2854 % \package{longtable} (and \package{array} for that matter) does
2855 % |\@mkpreamble| immediately after opening a new group.  So all we need to do
2856 % is close that group, do our stuff, and reopen the group again.  (Evil
2857 % laughter\dots)
2858 %
2859 %    \begin{macrocode}
2860 \def\@mkpream#1{%
2861   \endgroup%
2862   \colset{tabular}%
2863   \tab@initread%
2864   \def\tab@multicol{\@arstrut}%
2865   \tab@preamble{\tab@multicol}%
2866   \let\tab@lefttext\tab@lefttexthook%
2867   \let\tab@righttext\tab@righttexthook%
2868   \let\tab@leftruletext\tab@leftruletexthook%
2869   \let\tab@rightruletext\tab@rightruletexthook%
2870   \def\tab@midtext{\tab@setcr\ignorespaces\@sharp\@sharp\@maybe@unskip}%
2871   \tab@readpreamble{#1}%
2872   \gdef\@preamble{\the\tab@preamble}%
2873   \let\tab@bgroup\begingroup%
2874   \let\tab@egroup\endgroup%
2875   \begingroup%
2876 }
2877 %    \end{macrocode}
2878 %
2879 % \subsection{Debugging}
2880 %
2881 % This macro just parses a preamble and displays it on the terminal.  It
2882 % means I can see whether the thing's working.
2883 %
2884 %    \begin{macrocode}
2885 \def\showpream#1{%
2886   \tab@initread%
2887   \tab@readpreamble{#1}%
2888   \showthe\tab@preamble%
2889   \showthe\tab@shortline%
2890 }
2891 %    \end{macrocode}
2892 %
2893 % A quick macro for showing column types.
2894 %
2895 %    \begin{macrocode}
2896 \def\showcol#1{%
2897   \expandafter\show\csname\tab@colset!col.\string#1\endcsname%
2898 }
2899 %    \end{macrocode}
2900 %
2901 %
2902 % \subsection{The \env{tabular} and \env{array} environments}
2903 %
2904 % This is where we define the actual environments which users play with.
2905 %
2906 % \subsubsection{The environment routines}
2907 %
2908 % The real work is done in the |\@array| macro later.  We just set up lots
2909 % (and I mean \emph{lots}) of parameters first, and then call |\@array|.
2910 %
2911 % \begin{macro}{\tab@array}
2912 %
2913 % The |\tab@array| macro does most of the common array things.
2914 %
2915 %    \begin{macrocode}
2916 \def\tab@array{%
2917   \tab@width\z@%
2918   \let\tab@bgroup\tab@bmaths%
2919   \let\tab@egroup\tab@emaths%
2920   \@tabarray%
2921 }
2922 %    \end{macrocode}
2923 %
2924 % \end{macro}
2925 %
2926 % \begin{macro}{\tab@btext}
2927 % \begin{macro}{\tab@bmaths}
2928 % \begin{macro}{\tab@etext}
2929 % \begin{macro}{\tab@emaths}
2930 %
2931 % These macros contain appropriate things to use when typesetting
2932 % text or maths macros.  They're all trivial.  They're here only for
2933 % later modification by funny things like the \env{smarray} environment.
2934 %
2935 %    \begin{macrocode}
2936 \def\tab@btext{\begingroup}
2937 \def\tab@bmaths{\color@begingroup$}
2938 \def\tab@etext{\endgroup}
2939 \def\tab@emaths{\m@th$\color@endgroup}
2940 %    \end{macrocode}
2941 %
2942 % \end{macro}
2943 % \end{macro}
2944 % \end{macro}
2945 % \end{macro}
2946 %
2947 % \begin{environment}{array}
2948 %
2949 % Now for the \env{array} environment.  The `|$|' signs act as a group, so we
2950 % don't need to do extra grouping this time.  Closing the environment is
2951 % easy.
2952 %
2953 %    \begin{macrocode}
2954 \def\array{%
2955   \col@sep\arraycolsep%
2956   \let\tab@extrasep\arrayextrasep%
2957   \tab@normalstrut%
2958   \tab@array%
2959 }
2960 \def\endarray{%
2961   \crcr%
2962   \egroup%
2963   \tab@right%
2964   \endgroup%
2965   \tab@restorehlstate%
2966   \global\c@tabrow\count@%
2967   \def\@currentlabel{\p@tabrow\thetabrow}%
2968   \tab@endhook%
2969 }
2970 %    \end{macrocode}
2971 %
2972 % \end{environment}
2973 %
2974 % \begin{environment}{smarray}
2975 %
2976 % Now for something a little different.  The \env{smarray} environment
2977 % gives you an array with lots of small text.
2978 %
2979 %    \begin{macrocode}
2980 \def\smarray{%
2981   \extrarowheight\z@%
2982   \col@sep\smarraycolsep%
2983   \let\tab@extrasep\smarrayextrasep%
2984   \def\tab@bmaths{\color@begingroup$\scriptstyle}%
2985   \def\tab@btext{\begingroup\scriptsize}%
2986   \setbox\z@\hbox{\scriptsize\strut}%
2987   \dimen@\ht\z@\dimen@ii\dp\z@\tab@setstrut%
2988   \tab@array%
2989 }
2990 \let\endsmarray\endarray
2991 %    \end{macrocode}
2992 %
2993 % \end{environment}
2994 %
2995 % \begin{macro}{\tabstyle}
2996 %
2997 % This is a little hook that document designers can use to modify the
2998 % appearance of tables throughout a document.  For example, I've set it to
2999 % make the text size |\small| in all tables in this document.  Macro writers
3000 % shouldn't try to use it as a hook for their own evilness, though.  I've
3001 % used |\providecommand| to avoid nobbling an existing definition.
3002 %
3003 %    \begin{macrocode}
3004 \providecommand\tabstyle{}
3005 %    \end{macrocode}
3006 %
3007 % \end{macro}
3008 %
3009 % \begin{macro}{\@tabular}
3010 %
3011 % The two \env{tabular} environments share lots of common code, so we
3012 % separate that out.  (This needs to be done better.)  All we really do here
3013 % is set up the |\tab@bgroup| and |\tab@egroup| to localise things properly,
3014 % and then go.
3015 %
3016 %    \begin{macrocode}
3017 \def\@tabular#1{%
3018   \tabstyle%
3019   \setlength\tab@width{#1}%
3020   \let\tab@bgroup\tab@btext%
3021   \let\tab@egroup\tab@etext%
3022   \col@sep\tabcolsep%
3023   \let\tab@extrasep\tabextrasep%
3024   \tab@normalstrut%
3025   \@tabarray%
3026 }
3027 %    \end{macrocode}
3028 %
3029 % \end{macro}
3030 %
3031 % \begin{environment}{tabular}
3032 % \begin{environment}{tabular*}
3033 %
3034 % These environments just call a macro which does all the common stuff.
3035 %
3036 %    \begin{macrocode}
3037 \def\tabular{\@tabular\z@}
3038 \expandafter\let\csname tabular*\endcsname\@tabular
3039 \let\endtabular\endarray
3040 \expandafter\let\csname endtabular*\endcsname\endarray
3041 %    \end{macrocode}
3042 %
3043 % \end{environment}
3044 % \end{environment}
3045 %
3046 % \subsubsection{Setting the strut height}
3047 %
3048 % \begin{macro}{\tab@setstrut}
3049 %
3050 % We use a magical strut, called |\@arstrut|, which keeps the table from
3051 % collapsing around our heads.  This is where we set it up.
3052 %
3053 % It bases the array strut size on the given values of |\dimen@| and
3054 % |\dimen@ii|, amended by various appropriate fiddle values added in by
3055 % various people.
3056 %
3057 %    \begin{macrocode}
3058 \def\tab@setstrut{%
3059   \setbox\@arstrutbox\hbox{%
3060     \vrule%
3061       \@height\arraystretch\dimen@%
3062       \@depth\arraystretch\dimen@ii%
3063       \@width\z@%
3064   }%
3065 }
3066 %    \end{macrocode}
3067 %
3068 % \end{macro}
3069 %
3070 % \begin{macro}{\tab@normalstrut}
3071 %
3072 % This sets the strut the normal way, from the size of |\strutbox|.
3073 %
3074 %    \begin{macrocode}
3075 \def\tab@normalstrut{%
3076   \dimen@\ht\strutbox\advance\dimen@\extrarowheight%
3077   \dimen\tw@\dp\strutbox%
3078   \tab@setstrut%
3079 }
3080 %    \end{macrocode}
3081 %
3082 % \end{macro}
3083 %
3084 % \subsubsection{Setting up the alignment}
3085 %
3086 % The following bits are mainly for other packages to hook themselves onto.
3087 %
3088 %    \begin{macrocode}
3089 \let\@arrayleft\relax
3090 \let\@arrayright\relax
3091 \let\tab@beginhook\@empty
3092 \let\tab@lefttexthook\@empty
3093 \let\tab@righttexthook\@empty
3094 \let\tab@leftruletexthook\@empty
3095 \let\tab@rightruletexthook\@empty
3096 \let\tab@endhook\@empty
3097 %    \end{macrocode}
3098 %
3099 % For setting these hooks, we provide some handy commands.
3100 %
3101 %    \begin{macrocode}
3102 \def\tab@addhookbefore#1#2{%
3103   \toks@{#2}\toks@\expandafter{\the\expandafter\toks@#1}%
3104   \edef#1{\the\toks@}%
3105 }
3106 \def\tab@addhookafter#1#2{%
3107   \toks@\expandafter{#1#2}%
3108   \edef#1{\the\toks@}%
3109 }
3110 %    \end{macrocode}
3111 %
3112 % And now we get on with the real thing.
3113 %
3114 %    \begin{macrocode}
3115 \def\@tabarray{%
3116   \let\@arrayleft\relax%
3117   \let\@arrayright\relax%
3118   \@testopt\@array c%
3119 }
3120 %    \end{macrocode}
3121 %
3122 % \begin{macro}{\@array}
3123 %
3124 % The |\@array| macro does most of the real work for the environments.  The
3125 % first job is to set up the row strut, which keeps the table rows at the
3126 % right height.  We just take the normal strut box, and extend its height by
3127 % the |\extrarowheight| length parameter.
3128 %
3129 %    \begin{macrocode}
3130 \def\@array[#1]#2{%
3131 %    \end{macrocode}
3132 %
3133 % Sort out the hline state variable.  We'll store the old value in a
3134 % control sequence to avoid wasting any more count registers.
3135 %
3136 %    \begin{macrocode}
3137   \tab@beginhook%
3138   \count@\c@tabrow%
3139   \global\c@tabrow\z@%
3140   \edef\tab@restorehlstate{%
3141     \global\tab@endheight\the\tab@endheight%
3142     \gdef\noexpand\tab@hlstate{\tab@hlstate}%
3143   }%
3144   \begingroup%
3145   \def\tab@hlstate{n}%
3146 %    \end{macrocode}
3147 %
3148 % Now we read the preamble.  All the clever things we've already done are
3149 % terribly useful here.
3150 %
3151 % The |\tab@setcr| sets up |\\| to be a newline even if users have changed it
3152 % using something like |\raggedright|.
3153 %
3154 %    \begin{macrocode}
3155   \colset{tabular}%
3156   \tab@initread%
3157   \let\tab@lefttext\tab@lefttexthook%
3158   \let\tab@righttext\tab@righttexthook%
3159   \let\tab@leftruletext\tab@leftruletexthook%
3160   \let\tab@rightruletext\tab@rightruletexthook%
3161   \def\tab@midtext{\tab@setcr\ignorespaces####\@maybe@unskip}%
3162   \def\tab@multicol{\@arstrut\tab@startrow}%
3163   \tab@preamble{\tab@multicol\tabskip\z@skip}%
3164   \tab@readpreamble{#2}%
3165 %    \end{macrocode}
3166 %
3167 % Set up the default tabskip glue.  This is easy: there isn't any.
3168 %
3169 %    \begin{macrocode}
3170   \tab@leftskip\z@skip%
3171   \tab@rightskip\z@skip%
3172 %    \end{macrocode}
3173 %
3174 % Now set up the positioning of the table.  This is put into a separate macro
3175 % because it's rather complicated.
3176 %
3177 %    \begin{macrocode}
3178   \tab@setposn{#1}%
3179 %    \end{macrocode}
3180 %
3181 % Now work out how to start the alignment.
3182 %
3183 %    \begin{macrocode}
3184   \ifdim\tab@width=\z@%
3185     \def\tab@halign{}%
3186   \else%
3187     \def\tab@halign{to\tab@width}%
3188   \fi%
3189 %    \end{macrocode}
3190 %
3191 % Finally, do all the normal things we need to do before an alignment.  Note
3192 % that we define |\tabularnewline| first, then set |\\| from that (using
3193 % |\tab@setcr|).  Since |\\| is reset in the |\tab@midtext| of every table
3194 % cell, it becomes secondary to |\tabularnewline|.  Doing things this way
3195 % avoids the problems with declarations like |\raggedright| which redefine
3196 % |\\| in their own (usually rather strange) way, so you don't need to mess
3197 % about with things like the |\PreserveBackslash| command given in the
3198 % \textit{\LaTeX\ Companion}.
3199 %
3200 %    \begin{macrocode}
3201   \lineskip\z@\baselineskip\z@%
3202   \m@th%
3203   \def\tabularnewline{\tab@arraycr\tab@penalty}%
3204   \tab@setcr%
3205   \let\par\@empty%
3206   \everycr{}\tabskip\tab@leftskip%
3207   \tab@left\halign\tab@halign\expandafter\bgroup%
3208     \the\tab@preamble\tabskip\tab@rightskip\cr%
3209 }
3210 %    \end{macrocode}
3211 %
3212 % \end{macro}
3213 %
3214 % You've no doubt noticed the |\tab@left| and |\tab@right| macros above.
3215 % These are set up here and elsewhere to allow other things to gain control
3216 % at various points of the table (they include and take the place of the
3217 % |\@arrayleft| and |\@arrayright| hooks in \package{array}, put in for
3218 % \package{delarray}'s use.
3219 %
3220 % \subsubsection{Positioning the table}
3221 %
3222 % \begin{macro}{\tab@setposn}
3223 %
3224 % This macro sets everything up for the table's positioning.  It's rather
3225 % long, but not all that complicated.  Honest.
3226 %
3227 % First, we set up some defaults (for centring).  If anything goes wrong, we
3228 % just do the centring things.
3229 %
3230 %    \begin{macrocode}
3231 \def\tab@setposn#1{%
3232   \def\tab@left{%
3233     \savenotes%
3234     \leavevmode\hbox\bgroup$\@arrayleft\vcenter\bgroup%
3235   }%
3236   \def\tab@right{%
3237     \egroup%
3238     \m@th\@arrayright$\egroup%
3239     \spewnotes%
3240   }%
3241   \global\tab@endheight\z@%
3242 %    \end{macrocode}
3243 %
3244 % For the standard positioning things, we just do appropriate boxing things.
3245 % Note that the dollar signs are important, since \package{delarray} might
3246 % want to put its delimiters in here.
3247 %
3248 % The |\if@tempswa| switch it used to decide if we're doing an unboxed
3249 % tabular.  We'll set it if we find an unbox-type position code, and then
3250 % check that everything's OK for this.
3251 %
3252 %    \begin{macrocode}
3253   \@tempswafalse%
3254   \let\tab@penalty\relax%
3255   \if#1t%
3256     \def\tab@left{%
3257       \savenotes%
3258       \leavevmode\setbox\z@\hbox\bgroup$\@arrayleft\vtop\bgroup%
3259     }%
3260     \def\tab@right{%
3261       \egroup%
3262       \m@th\@arrayright$\egroup%
3263       \tab@raisebase%
3264       \spewnotes%
3265     }%
3266     \gdef\tab@hlstate{t}%
3267     \global\tab@endheight\ht\@arstrutbox%
3268   \else\if#1b%
3269     \def\tab@left{%
3270       \savenotes%
3271       \leavevmode\setbox\z@\hbox\bgroup$\@arrayleft\vbox\bgroup%
3272     }%
3273     \def\tab@right{%
3274       \egroup%
3275       \m@th\@arrayright$\egroup%
3276       \tab@lowerbase%
3277       \spewnotes%
3278     }%
3279     \gdef\tab@hlstate{b}%
3280   \else%
3281     \if#1L\@tempswatrue\fi%
3282     \if#1C\@tempswatrue\fi%
3283     \if#1R\@tempswatrue\fi%
3284   \fi\fi%
3285 %    \end{macrocode}
3286 %
3287 % Now for some tests to make sure we're allowed to do the unboxing.  We text
3288 % for |\@arrayleft| being defined, because people trying to hook us won't
3289 % understand unboxed tabulars.
3290 %
3291 %    \begin{macrocode}
3292   \if@tempswa\ifhmode%
3293     \ifinner\tab@err@unbrh\@tempswafalse\else\par\fi%
3294   \fi\fi%
3295   \if@tempswa\ifmmode\tab@err@unbmm\@tempswafalse\fi\fi%
3296   \if@tempswa\ifx\@arrayleft\relax\else%
3297     \tab@err@unbext\@tempswafalse%
3298   \fi\fi%
3299 %    \end{macrocode}
3300 %
3301 % Finally, if we're still doing an unboxed alignment, we need to sort out the
3302 % spacing.  We know that no-one's tried to hook on to the environment, so we
3303 % clear |\tab@left| and |\tab@right|.
3304 %
3305 %    \begin{macrocode}
3306   \if@tempswa%
3307     \def\tab@left{\vskip\parskip\medskip}%
3308     \def\tab@right{\par\@endpetrue\global\@ignoretrue}%
3309 %    \end{macrocode}
3310 %
3311 % Now we need to sort out the alignment.  The only way we can do this is by
3312 % playing with tabskip glue.  There are two possibilities:
3313 %
3314 % \begin{itemize}
3315 %
3316 % \item If this is a straight \env{tabular} or an \env{array}, we just use
3317 %       infinite glue.  This is reasonable, I think.
3318 %
3319 % \item If we have a width for the table, we calculate the fixed values of
3320 %       glue on either side.  This is fairly easy, and forces the table to
3321 %       the required width.
3322 %
3323 % \end{itemize}
3324 %
3325 % First, set up the left and right glues to represent the prevailing
3326 % margins set up by \env{list} environments.  I think this is the right
3327 % thing to do.
3328 %
3329 %    \begin{macrocode}
3330     \tab@leftskip\@totalleftmargin%
3331     \tab@rightskip\hsize%
3332     \advance\tab@rightskip-\linewidth%
3333     \advance\tab@rightskip-\@totalleftmargin%
3334 %    \end{macrocode}
3335 %
3336 % First of all, deal with the simple case.  I'm using 10000\,fill glue here,
3337 % in an attempt to suppress |\extracolsep| glue from making the table the
3338 % wrong width.  It can always use filll glue if it really needs to, though.
3339 %
3340 %    \begin{macrocode}
3341     \ifdim\tab@width=\z@%
3342       \if#1L\else\advance\tab@leftskip\z@\@plus10000fill\fi%
3343       \if#1R\else\advance\tab@rightskip\z@\@plus10000fill\fi%
3344 %    \end{macrocode}
3345 %
3346 % Now for the fun bit.  This isn't too hard really.  The extra space I must
3347 % add around the table adds up to $|\linewidth| - |\tab@width|$.  I just
3348 % need to add this onto the appropriate sides of the table.
3349 %
3350 %    \begin{macrocode}
3351     \else%
3352       \dimen@\linewidth%
3353       \advance\dimen@-\tab@width%
3354       \if#1L\advance\tab@rightskip\dimen@\fi%
3355       \if#1R\advance\tab@leftskip\dimen@\fi%
3356       \if#1C%
3357         \advance\tab@leftskip.5\dimen@%
3358         \advance\tab@rightskip.5\dimen@%
3359       \fi%
3360     \fi%
3361 %    \end{macrocode}
3362 %
3363 % Don't allow page breaks.  David Carlisle's wonderful \env{longtable}
3364 % package does page breaks far better than I could possibly do here, and
3365 % we're compatible with it (wahey!).
3366 %
3367 %    \begin{macrocode}
3368     \def\tab@penalty{\penalty\@M}%
3369 %    \end{macrocode}
3370 %
3371 % Finally, set the new width of the table, and leave.
3372 %
3373 %    \begin{macrocode}
3374     \tab@width\hsize%
3375   \fi%
3376 }
3377 %    \end{macrocode}
3378 %
3379 % \end{macro}
3380 %
3381 % \subsubsection{Handling tops and bottoms}
3382 %
3383 % This is how the tops and bottoms of tables are made to line up with the
3384 % text on the same line, in the presence of arbitrary rules and space.  The
3385 % old method, based on the way the \package{array} package worked, wasn't
3386 % terribly good.  This new version copes much better with almost anything
3387 % that gets thrown at it.
3388 %
3389 % I'll keep a state in a macro (|\tab@hlstate|), which tells me what I'm
3390 % meant to be doing.  The possible values are \lit{n}, which means I don't
3391 % have to do anything, \lit{t}, which means that I'm meant to be handling
3392 % top-aligned tables, and \lit{b}, which means that I'm meant to be lining
3393 % up the bottom.  There are several other `substates' which have various
3394 % magic meanings.
3395 %
3396 %    \begin{macrocode}
3397 \def\tab@hlstate{n}
3398 %    \end{macrocode}
3399 %
3400 % When all's said and done, I extract the box containing the table, and
3401 % play with the height and depth to try and make it correct.
3402 %
3403 % \begin{macro}{\tab@addruleheight}
3404 %
3405 % This macro is called by `inter-row' things to add their height to our
3406 % dimen register.
3407 %
3408 % Only do this if the state indicates that it's sensible.
3409 %
3410 %    \begin{macrocode}
3411 \def\tab@addruleheight#1{%
3412   \if\tab@hlstate n\else%
3413     \global\advance\tab@endheight#1\relax%
3414   \fi%
3415 }
3416 %    \end{macrocode}
3417 %
3418 % \end{macro}
3419 %
3420 % \begin{macro}{\tab@startrow}
3421 %
3422 % This is called at the start of a row, from within the array preamble.
3423 % Currently, this assumes that the rows aren't bigger than their struts:
3424 % this is reasonable, although slightly limiting, and it could be done better
3425 % if I was willing to rip the alignment apart and put it back together
3426 % again.
3427 %
3428 %    \begin{macrocode}
3429 \def\tab@startrow{%
3430   \if\tab@hlstate t%
3431     \gdef\tab@hlstate{n}%
3432   \else\if\tab@hlstate b%
3433     \global\tab@endheight\dp\@arstrutbox%
3434   \fi\fi%
3435 }
3436 %    \end{macrocode}
3437 %
3438 % \end{macro}
3439 %
3440 % \begin{macro}{\tab@raisebase}
3441 %
3442 % This macro is called at the end of it all, to set the height and depth
3443 % of the box correctly.  It sets the height to |\tab@endheight|, and the
3444 % depth to everything else.  The box is in |\box|~0 currently.
3445 %
3446 %    \begin{macrocode}
3447 \def\tab@raisebase{%
3448   \global\advance\tab@endheight-\ht\z@%
3449   \raise\tab@endheight\box\z@%
3450 }
3451 %    \end{macrocode}
3452 %
3453 % \end{macro}
3454 %
3455 % \begin{macro}{\tab@lowerbase}
3456 %
3457 % And, for symmetry's sake, here's how to set the bottom properly instead.
3458 %
3459 %    \begin{macrocode}
3460 \def\tab@lowerbase{%
3461   \global\advance\tab@endheight-\dp\z@%
3462   \lower\tab@endheight\box\z@%
3463 }
3464 %    \end{macrocode}
3465 %
3466 % \end{macro}
3467 %
3468 %
3469 % \subsection{Breaking tables into bits}
3470 %
3471 % Unboxed tables have a wonderful advantage over boxed ones: you can stop
3472 % halfway through and do something else for a bit.  Here's how:
3473 %
3474 % \begin{macro}{\tabpause}
3475 %
3476 % I'd like to avoid forbidding catcode changes here.  I'll use |\doafter|
3477 % now I've got it, to ensure that colour handling and things occur
3478 % \emph{inside} the |\noalign| (otherwise they'll mess up the alignment
3479 % very seriously).  We selectively include lots of stuff from
3480 % |\arrayparboxrestore|.
3481 %
3482 % We have to be careful here to ensure that everything works correctly within
3483 % lists.  (The \package{amsmath} package had this problem in its
3484 % |\intertext| macro, so I'm not alone here.)
3485 %
3486 %    \begin{macrocode}
3487 \def\tabpause#{%
3488   \noalign{\ifnum0=`}\fi%
3489   \let\if@nobreak\iffalse
3490   \let\if@noskipsec\iffalse
3491   \let\par\@@par
3492   \let\-\@dischyph
3493   \let\'\@acci\let\`\@accii\let\=\@acciii
3494   \everypar{}%
3495   \lineskip\normallineskip%
3496   \let\\\@normalcr%
3497   \color@begingroup%
3498   \tab@startpause%
3499   \vskip-\parskip%
3500   \parshape\@ne\@totalleftmargin\linewidth%
3501   \noindent%
3502   \doafter\tabpause@i%
3503 }
3504 \def\tabpause@i{%
3505   \nobreak%
3506   \tab@endpause%
3507   \color@endgroup%
3508   \ifnum0=`{\fi}%
3509 }
3510 %    \end{macrocode}
3511 %
3512 % \end{macro}
3513 %
3514 %
3515 % \subsection{The wonderful world of \cmd\multicolumn}
3516 %
3517 % \begin{macro}{\multicolumn}
3518 %
3519 % This is actually fantastically easy.  Watch and learn.  Make sure you
3520 % notice the |\long|s here: remember that some table cells can contain
3521 % paragraphs, so it seems sensible to allow |\par| into the argument.
3522 % (As far as I know, most other |\multicolumn| commands don't do this,
3523 % which seems a little silly.  Then again, I forgot to do it the first
3524 % time around.)
3525 %
3526 %    \begin{macrocode}
3527 \long\def\multicolumn#1#2#3{%
3528   \multispan{#1}%
3529   \begingroup%
3530     \tab@multicol%
3531     \tab@initsubread%
3532     \long\def\tab@midtext{#3}%
3533     \let\tab@looped\tab@err@multi%
3534     \tab@readpreamble{#2}%
3535     \the\tab@preamble%
3536   \endgroup%
3537   \ignorespaces%
3538 }
3539 %    \end{macrocode}
3540 %
3541 % \end{macro}
3542 %
3543 %
3544 % \subsection{Interlude: range lists}
3545 %
3546 % For processing arguments to |\vgap| and |\cline|, we need to be able to
3547 % do things with lists of column ranges.  To save space, and to make my
3548 % fingers do less typing, here's some routines which do range handling.
3549 %
3550 % \begin{macro}{\ranges}
3551 %
3552 % Given a macro name and a comma separated list of ranges and simple numbers,
3553 % this macro will call the macro giving it each range in the list in turn.
3554 % Single numbers~$n$ will be turned into ranges $n$--$n$.
3555 %
3556 % The first job is to read the macro to do (which may already have some
3557 % arguments attached to it).  We'll also start a group to make sure that
3558 % our changes to temp registers don't affect anyone else.
3559 %
3560 % There's a space before the delimiting |\q@delim| to stop numbers being
3561 % parsed to far and expanding our quark (which will stop \TeX\ dead in its
3562 % tracks).  Since we use |\@ifnextchar| to look ahead, spaces in range lists
3563 % are perfectly all right.
3564 %
3565 %    \begin{macrocode}
3566 \def\ranges#1#2{%
3567   \gdef\ranges@temp{#1}%
3568   \begingroup%
3569   \ranges@i#2 \q@delim%
3570 }
3571 %    \end{macrocode}
3572 %
3573 %
3574 % We're at the beginning of the list.  We expect either the closing marker
3575 % (if this is an empty list) or a number, which we can scoop up into a
3576 % scratch register.
3577 %
3578 %    \begin{macrocode}
3579 \def\ranges@i{%
3580   \@ifnextchar\q@delim\ranges@done{\afterassignment\ranges@ii\count@}%
3581 }
3582 %    \end{macrocode}
3583 %
3584 % We've read the first number in the range.  If there's another number, we'll
3585 % expect a `|-|' sign to be next.  If there is no `|-|', call the user's code
3586 % with the number duplicated and then do the rest of the list.
3587 %
3588 %    \begin{macrocode}
3589 \def\ranges@ii{%
3590   \@ifnextchar-\ranges@iii{\ranges@do\count@\count@\ranges@v}%
3591 }
3592 %    \end{macrocode}
3593 %
3594 % Now we strip the `|-|' off and read the other number into a temporary
3595 % register.
3596 %
3597 %    \begin{macrocode}
3598 \def\ranges@iii-{\afterassignment\ranges@iv\@tempcnta}
3599 %    \end{macrocode}
3600 %
3601 % We have both ends of the range now, so call the user's code, passing it
3602 % both ends of the range.
3603 %
3604 %    \begin{macrocode}
3605 \def\ranges@iv{\ranges@do\count@\@tempcnta\ranges@v}
3606 %    \end{macrocode}
3607 %
3608 % We've finished doing an item now.  If we have a `|,|' next, then start
3609 % over with the next item.  Otherwise, if we're at the end of the list,
3610 % we can end happily.  Finally, if we're totally confused, raise an
3611 % error.
3612 %
3613 %    \begin{macrocode}
3614 \def\ranges@v{%
3615   \@ifnextchar,%
3616     \ranges@vi%
3617     {%
3618       \@ifnextchar\q@delim%
3619         \ranges@done%
3620         {\tab@err@range\ranges@vi,}%
3621     }%
3622 }
3623 %    \end{macrocode}
3624 %
3625 % We had a comma, so gobble it, read the next number, and go round again.
3626 %
3627 %    \begin{macrocode}
3628 \def\ranges@vi,{\afterassignment\ranges@ii\count@}
3629 %    \end{macrocode}
3630 %
3631 % Here's how we call the user's code, now.  We close the group, so that the
3632 % user's code doesn't have to do global things to remember its results, and
3633 % we expand the two range ends from their count registers.  We also ensure
3634 % that the range is the right way round.
3635 %
3636 %    \begin{macrocode}
3637 \def\ranges@do#1#2{%
3638   \ifnum#1>#2\else%
3639     \expandafter\endgroup%
3640     \expandafter\ranges@temp%
3641     \expandafter{%
3642     \the\expandafter#1%
3643     \expandafter}%
3644     \expandafter{%
3645     \the#2%
3646     }%
3647     \begingroup%
3648   \fi%
3649 }
3650 %    \end{macrocode}
3651 %
3652 % And finishing the scan is really easy.  We close the group after gobbling
3653 % the close token.
3654 %
3655 %    \begin{macrocode}
3656 \def\ranges@done\q@delim{\endgroup}
3657 %    \end{macrocode}
3658 %
3659 % \end{macro}
3660 %
3661 % \begin{macro}{\ifinrange}
3662 %
3663 % Something a little more useful, now.  |\ifinrange| takes four arguments:
3664 % a number, a range list (as above), and two token lists which I'll call
3665 % \emph{then} and \emph{else}.  If the number is in the list, I'll do
3666 % \emph{then}, otherwise I'll do \emph{else}.
3667 %
3668 %    \begin{macrocode}
3669 \def\ifinrange#1#2{%
3670   \@tempswafalse%
3671   \count@#1%
3672   \ranges\ifinrange@i{#2}%
3673   \if@tempswa%
3674     \expandafter\@firstoftwo%
3675   \else%
3676     \expandafter\@secondoftwo%
3677   \fi%
3678 }
3679 \def\ifinrange@i#1#2{%
3680   \ifnum\count@<#1 \else\ifnum\count@>#2 \else\@tempswatrue\fi\fi%
3681 }
3682 %    \end{macrocode}
3683 %
3684 % \end{macro}
3685 %
3686 %
3687 % \subsection{Horizontal rules OK}
3688 %
3689 % This is where all the gubbins for |\vgap| and friends is kept, lest it
3690 % contaminate fairly clean bits of code found elsewhere.
3691 %
3692 % \subsubsection{Common parsing for rule parameters twiddling}
3693 %
3694 % \begin{macro}{\tab@ruleparams}
3695 %
3696 % Given a macro name, make a (global) macro |\tab@ruledecls|, which sets
3697 % |\dimen0| to be the chosen rule thickness, and sets up colours and whatnot,
3698 % and then and calls the macro.  We parse a `|*|' to mean
3699 % |\arraythickrulewidth|, an optional argument which should be something
3700 % |\setlength| can understand, or nothing, which gives the default
3701 % |\arrayrulewidth|.
3702 %
3703 % To make this properly hookable, we need to make a list of properties and
3704 % gather them together.
3705 %
3706 %    \begin{macrocode}
3707 \let\tab@rp@inithook\@empty
3708 \let\tab@rp@sethook\@empty
3709 \let\tab@rp@donehook\@empty
3710 \let\tab@rp@default\@empty
3711 \def\tab@ruleparams#1{%
3712   {\ifnum0=`}\fi%
3713   \tab@rp@inithook%
3714   \def\tab@rp@next{\ifnum0=`{\fi}#1}%
3715   \expandafter\tab@rp@keys\expandafter{\tab@rp@default}%
3716   \@ifstar\tab@rp@star\tab@rp@what%
3717 }
3718 \def\tab@rp@star{\dimen@\arraythickrulewidth\tab@rp@what}
3719 \def\tab@rp@what{\@ifnextchar[\tab@rp@opt\tab@rp@done}
3720 \def\tab@rp@opt[#1]{\tab@rp@keys{#1}\tab@rp@done}
3721 \def\tab@rp@keys{\mkparse{mdwtab:rule}}
3722 \def\tab@rp@done{%
3723   \protected@xdef\tab@rp@{\tab@rp@sethook}%
3724   \tab@rp@donehook%
3725   \tab@rp@next%
3726 }
3727 \def\tab@withrp#1{\tab@ruleparams{\tab@withrp@i{#1}}}
3728 \def\tab@withrp@i#1{%
3729   \toks@{#1}%
3730   \toks@\expandafter{\the\expandafter\toks@\expandafter{\tab@rp@}}%
3731   \the\toks@%
3732 }
3733 %    \end{macrocode}
3734 %
3735 % And now to define the width parameters.
3736 %
3737 %    \begin{macrocode}
3738 \tab@addhookafter\tab@rp@inithook{\dimen@\arrayrulewidth}
3739 \tab@addhookafter\tab@rp@sethook{\dimen@\the\dimen@}
3740 \tab@addhookafter\tab@rp@donehook{\global\tab@rulewidth\dimen@}
3741 \mkdef{mdwtab:rule}{width}{\setlength\dimen@{#1}}
3742 \mkdef{mdwtab:rule}{thin}*{\dimen@\arrayrulewidth}
3743 \mkdef{mdwtab:rule}{thick}*{\dimen@\arraythickrulewidth}
3744 \mkdef*{mdwtab:rule}*{\setlength\dimen@{#1}}
3745 %    \end{macrocode}
3746 %
3747 % \end{macro}
3748 %
3749 % \begin{macro}{\tabsetruleparams}
3750 %
3751 % And the user default-parameter list.
3752 %
3753 %    \begin{macrocode}
3754 \def\tabsetruleparams{\def\tab@rp@default}
3755 %    \end{macrocode}
3756 %
3757 % \end{macro}
3758 %
3759 % \subsubsection{Drawing horizontal rules}
3760 %
3761 % \begin{macro}{\hline}
3762 %
3763 % Note the funny use of |\noalign| to allow \TeX\ stomach ops like
3764 % |\futurelet| without starting a new table row.  This lets us see if there's
3765 % another |\hline| coming up, so we can see if we need to insert extra
3766 % vertical space.
3767 %
3768 %    \begin{macrocode}
3769 \def\hline{\noalign\tab@ruleparams\hline@prep}
3770 \def\hline@prep{%
3771   \tab@dohline%
3772   \noalign{\ifnum0=`}\fi%
3773   \tab@penalty%
3774   \futurelet\@let@token\hline@i%
3775 }
3776 %    \end{macrocode}
3777 %
3778 % We check here for another |\hline| command, and insert glue if there is.
3779 % This looks terrible, though, and |\hlx{hvh}| is much nicer.  Still\dots
3780 %
3781 %    \begin{macrocode}
3782 \def\hline@i{%
3783   \@tempswafalse%
3784   \ifx\@let@token\hline\@tempswatrue\fi%
3785   \ifx\@let@token\hline@prep\@tempswatrue\fi%
3786   \if@tempswa%
3787     \vskip\doublerulesep%
3788     \tab@addruleheight\doublerulesep%
3789   \fi%
3790   \ifnum0=`{\fi}%
3791 }
3792 %    \end{macrocode}
3793 %
3794 % \end{macro}
3795 %
3796 % \begin{macro}{\tab@dohline}
3797 %
3798 % This is where hlines actually get drawn.
3799 % Drawing lines is more awkward than it used to be, particularly in unboxed
3800 % tables.  It used to be a case simply of saying |\noalign{\hrule}|.
3801 % However, since unboxed tables are actually much wider than they look, this
3802 % would make the rules stretch right across the page and look generally
3803 % horrible.
3804 %
3805 % The solution is simple: we basically do a dirty big |\cline|.
3806 %
3807 %    \begin{macrocode}
3808 \def\tab@dohline{%
3809   \multispan\tab@columns%
3810   \color@begingroup%
3811   \tab@rp@\leaders\hrule\@height\dimen@\hfil%
3812   \tab@addruleheight\dimen@%
3813   \color@endgroup%
3814   \cr%
3815 }
3816 %    \end{macrocode}
3817 %
3818 % \end{macro}
3819 %
3820 % \subsubsection{Vertical rules}
3821 %
3822 % I couldn't fit these in anywhere else, so they'll have to go here.  I'll
3823 % provide a new optional argument which specifies the width of the rule; this
3824 % gets rid of the problem described in the \emph{Companion}, where to get
3825 % an unusually wide vertical rule, you have to play with things like
3826 % \syntax{"\\vrule width" <dimen>} which really isn't too nice.
3827 %
3828 % \begin{macro}{\vline}
3829 %
3830 % Now uses the general |\tab@ruleparams| parser.  We save and restore the
3831 % global |\tab@rulewidth| parameter here.
3832 %
3833 %    \begin{macrocode}
3834 \def\vline{%
3835   \begingroup%
3836   \@tempdima\tab@rulewidth\let\safe@\tab@rp@%
3837   \tab@ruleparams\tab@vline%
3838 }
3839 \def\tab@vline{%
3840   \tab@rp@\vrule\@width\dimen@%
3841   \global\tab@rulewidth\@tempdima\global\let\tab@rp@\safe@%
3842   \endgroup%
3843 }
3844 %    \end{macrocode}
3845 %
3846 % \end{macro}
3847 %
3848 % \subsubsection{Drawing bits of lines}
3849 %
3850 % Just for a bit of fun, here's an extended version of |\cline| which takes
3851 % a list of columns to draw lines under, rather than just a single range.
3852 %
3853 % \begin{macro}{\cline}
3854 %
3855 % Not a single line of code written yet, and we already have a dilemma on
3856 % our hands.  Multiple consecutive |\cline| commands are meant to draw
3857 % on the same vertical bit of table.  But horizontal lines are meant to have
3858 % thickness now.  Worse, if the lines have real thickness then we leave gaps
3859 % in the vertical rules which aren't covered by our line.  But if we
3860 % backspace over the line, then we overwrite it with coloured blobs.
3861 %
3862 % We give up on doing the job properly -- that's just doomed.  Backspace over
3863 % the previous row, and provide a hack for doing the spacing right elsewhere.
3864 %
3865 % Now the problem remains how best to do the job.  The way I see it, there
3866 % are three possibilities:
3867 %
3868 % \begin{itemize}
3869 %
3870 % \item We can start a table row, and then for each column of the table
3871 %       (as recorded in |\tab@columns|) we look to see if that column is
3872 %       listed in the range list and if so draw the rule.  This requires
3873 %       lots of scanning of the range list.
3874 %
3875 % \item We can take each range in the list, and draw rules appropriately,
3876 %       just like the old |\cline| used to do, and starting a new table row
3877 %       for each.
3878 %
3879 % \item We can start a table row, and then for each range remember where we
3880 %       stopped drawing the last row, move to the start of the new one, and
3881 %       draw it.  If we start moving backwards, we close the current row
3882 %       and open a new one.
3883 %
3884 % \end{itemize}
3885 %
3886 % The last option looks the most efficient, and the most difficult.  This
3887 % is therefore what I shall do |;-)|.
3888 %
3889 % The first thing to do is to add in a little negative space, and start a
3890 % table row (omitting the first item).  Then scan the range list, and finally
3891 % close the table row and add some negative space again.
3892 %
3893 % We need a global count register to keep track of where we are.  Mixing
3894 % local and global assignments causes all sorts of tragedy, so I shall hijack
3895 % |\tab@state|.
3896 %
3897 %    \begin{macrocode}
3898 \def\cline{\noalign\tab@ruleparams\cline@do}
3899 %    \end{macrocode}
3900 %
3901 % Now for the tricky bit.  When we're given a range, we look to see if the
3902 % first number is less than |\tab@state|.  If so, we quickly close the
3903 % current row, kern backwards and start again with an |\omit| and reset
3904 % |\tab@state| to 1, and try again.  This is hardly perfect, but gets the job
3905 % done in many cases.  Correct |\vgap| insertion fixes the remaining bugs.
3906 %
3907 %    \begin{macrocode}
3908 \def\cline@do#1{%
3909   \noalign{\kern-\tab@rulewidth}%
3910   \omit%
3911   \global\tab@state\@ne%
3912   \ranges\cline@do@i{#1}\cr%
3913 }
3914 \def\cline@do@i#1#2{%
3915   \ifnum#1<\tab@state\relax%
3916     \tab@@cr%
3917     \noalign{\kern-\tab@rulewidth\tab@penalty}%
3918     \omit%
3919     \global\tab@state\@ne%
3920   \fi%
3921 %    \end{macrocode}
3922 %
3923 % We are now either at or in front of the column position required.  If
3924 % we're too far back, we must |\hfil&\omit| our way over to the correct
3925 % column.
3926 %
3927 %    \begin{macrocode}
3928   \@whilenum\tab@state<#1\do{%
3929     \hfil\tab@@tab@omit%
3930     \global\advance\tab@state\@ne%
3931   }%
3932 %    \end{macrocode}
3933 %
3934 % We've found the start correctly.  We must deal with a tiny problem now:
3935 % if this is not the first table cell, the left hand vertical rule is in the
3936 % column to the left, so our horizontal rule won't match up properly.  So
3937 % we skip back by a bit to compensate.  If there isn't actually a vertical
3938 % rule to line up with, no-one will notice, because the rules are so thin.
3939 % This adds a little touch of quality to the whole thing, which is after all
3940 % the point of this whole exercise.
3941 %
3942 %    \begin{macrocode}
3943   \ifnum\tab@state>\@ne%
3944     \kern-\arrayrulewidth%
3945   \fi%
3946 %    \end{macrocode}
3947 %
3948 % Now we must stretch this table cell to the correct width.
3949 %
3950 %    \begin{macrocode}
3951   \@whilenum\tab@state<#2\do{%
3952     \tab@@span@omit%
3953     \global\advance\tab@state\@ne%
3954   }%
3955 %    \end{macrocode}
3956 %
3957 % We're ready.  Draw the rule.  Note that this is |\hfill| glue, just in case
3958 % we start putting in |\hfil| glue when we step onto the next cell.
3959 %
3960 %    \begin{macrocode}
3961   \color@begingroup%
3962   \tab@rp@%
3963   \leaders\hrule\@height\tab@rulewidth\hfill%
3964   \color@endgroup%
3965 }
3966 %    \end{macrocode}
3967 %
3968 % Some alignment primitives are hidden inside macros so they don't get seen
3969 % at the wrong time.  This is what they look like:
3970 %
3971 %    \begin{macrocode}
3972 \def\tab@@cr{\cr}
3973 \def\tab@@tab@omit{&\omit}
3974 \def\tab@@span@omit{\span\omit}
3975 %    \end{macrocode}
3976 %
3977 % \end{macro}
3978 %
3979 % \subsubsection{Drawing short table rows}
3980 %
3981 % Before I start on a description of more code, I think I'll briefly discuss
3982 % my reasons for leaving the |\vgap| command in its current state.  There's a
3983 % reasonable case for introducing an interface between |\vgap| and
3984 % |\multicolumn|, to avoid all the tedious messing about with column
3985 % ranges.  There are good reasons why I'm not going to do this:
3986 %
3987 % \begin{itemize}
3988 %
3989 % \item It's very difficult to do: it requires either postprocessing of
3990 %       the table or delaying processing of each row until I know exactly
3991 %       what's in it; a |\multicolumn| in a row should be able to affect
3992 %       a |\vgap| before the row, which gets very nasty.  This package is
3993 %       probably far too large already, and adding more complexity and
3994 %       running the risk of exhausting \TeX's frustratingly finite capacity
3995 %       for the sake of relieving the user of a fairly trivial job doesn't
3996 %       seem worthwhile.
3997 %
3998 % \item Perhaps more importantly, there are perfectly valid occasions when
3999 %       it's useful to have the current vgap behaviour.  For example, the
4000 %       \texttt{MIX} word layout diagrams found in \emph{The Art of
4001 %       Computer Programming} use the little `stub lines' to show where
4002 %       data items cross byte boundaries:
4003 %
4004 %       ^^A This actually looks terrifyingly similar to the original.
4005 %       ^^A The leading @{} is there to stop the table looking off-centre,
4006 %       ^^A because there's no left hand rule telling you where the table
4007 %       ^^A starts, like there is on the right, just the \tabcolsep glue.
4008 %
4009 %       \begingroup
4010 %       \newcommand{\wide}[2]{\multicolumn{#1}{c|}{\ttfamily #2}}
4011 %       \begin{tabular}[C]{@{} r @{\qquad} | Mc | *{5}{c|}} \hlx{c{2-7} v}
4012 %          empty & - & 1 & 0 & 0 & 0 & 0                \\ \hlx{v c{2-7} v}
4013 %       occupied & + & \wide{2}{LINK} & \wide{3}{KEY}   \\ \hlx{v c{2-7}}
4014 %       \end{tabular}
4015 %       \endgroup
4016 %
4017 % \end{itemize}
4018 %
4019 % That's my excuses out of the way; now I'll press on with the actual
4020 % programming.
4021 %
4022 % \begin{macro}{\tab@checkrule}
4023 %
4024 % We have a range list in |\tab@xcols| and a number as an argument.  If we
4025 % find the number in the list, we just space out the following group,
4026 % otherwise we let it be.
4027 %
4028 %    \begin{macrocode}
4029 \def\tab@checkrule#1{%
4030   \count@#1\relax%
4031   \expandafter\ifinrange%
4032   \expandafter\count@%
4033   \expandafter{\tab@xcols}%
4034     {\tab@checkrule@i}%
4035     {}%
4036 }
4037 \def\tab@checkrule@i#1{\setbox\z@\hbox{#1}\hb@xt@\wd\z@{}}
4038 %    \end{macrocode}
4039 %
4040 % \end{macro}
4041 %
4042 % \begin{macro}{\vgap}
4043 %
4044 % We must tread carefully here.  A single misplaced stomach operation can
4045 % cause error messages.  We therefore start with an |\omit| so we can search
4046 % for optional arguments.
4047 %
4048 % So that |\hlx| can get control after |\vgap| has finished, we provide a
4049 % hook called |\vgap@after| which is expanded after |\vgap| has finished.
4050 % Here we make it work like |\@empty|, which expands to nothing.  (Note that
4051 % |\relax| will start a new table row, so we can't use that.)  There are
4052 % some penalty items here to stick the |\vgap| row to the text row and
4053 % |\hline| that are adjacent to it.  The \package{longtable} package will
4054 % split an |\hline| in half, so this is the correct thing to do.
4055 %
4056 %    \begin{macrocode}
4057 \def\vgap{%
4058   \noalign{\nobreak}%
4059   \omit%
4060   \global\let\vgap@after\@empty%
4061   \iffalse{\fi\ifnum0=`}\fi%
4062   \@ifnextchar[\vgap@i\vgap@simple%
4063 }
4064 %    \end{macrocode}
4065 %
4066 % We set up two different sorts of |\vgap| -- a simple one which allows all
4067 % rules to be passed through, and a specific one which carefully vets each
4068 % one (and is therefore slower).  We decide which to so based on the presence
4069 % of an optional argument.
4070 %
4071 % The optional argument handler just passes its argument to an interface
4072 % routine which is used by |\hlx|.
4073 %
4074 %    \begin{macrocode}
4075 \def\vgap@i[#1]{\vgap@spec{#1}}
4076 %    \end{macrocode}
4077 %
4078 % Now we handle specified columns.  Since we're in an omitted table cell, we
4079 % must set things up globally.  Assign the column spec to a macro, and set up
4080 % vetting by the routine above.  Then just go and do the job.
4081 %
4082 %    \begin{macrocode}
4083 \def\vgap@spec#1#2{%
4084   \gdef\tab@xcols{#1}%
4085   \global\let\tab@ckr\tab@checkrule%
4086   \vgap@do{#2}%
4087 }
4088 %    \end{macrocode}
4089 %
4090 % Handle all columns.  Just gobble the column number for each rule, and let
4091 % the drawing pass unharmed.  Easy.
4092 %
4093 %    \begin{macrocode}
4094 \def\vgap@simple#1{%
4095   \global\let\tab@ckr\@gobble%
4096   \vgap@do{#1}%
4097 }
4098 %    \end{macrocode}
4099 %
4100 % This is where stuff actually gets done.  We set the |\vgap| flag on while
4101 % we do the short row.  Then just expand the token list we built while
4102 % scanning the preamble.
4103 %
4104 % Note that the flag is cleared at the end of the last column, to allow other
4105 % funny things like |\noalign| and |\omit| before a new row is started.
4106 %
4107 %    \begin{macrocode}
4108 \def\vgap@do#1{%
4109   \ifnum0=`{}\fi%
4110   \global\tab@vgaptrue%
4111   \the\tab@shortline%
4112     \vrule\@height#1\@width\z@%
4113     \global\tab@vgapfalse
4114     \tab@addruleheight{#1}%
4115     \cr%
4116   \noalign{\nobreak}%
4117   \vgap@after%
4118 }
4119 %    \end{macrocode}
4120 %
4121 % \end{macro}
4122 %
4123 % \subsubsection{Prettifying syntax}
4124 %
4125 % \begin{macro}{\hlx}
4126 %
4127 % This is like a poor cousin to the preamble parser.  The whole loop is
4128 % carefully written to take place \emph{only} in \TeX's mouth, so the
4129 % alignment handling bits half way down the gullet don't see any of this.
4130 %
4131 % First, pass the string to another routine.
4132 %
4133 %    \begin{macrocode}
4134 \def\hlx{\noalign\tab@ruleparams\hlx@prep}
4135 \def\hlx@prep#1{\hlx@loop#1\q@delim}
4136 %    \end{macrocode}
4137 %
4138 % Now peel off a token, and dispatch using |\csname|.  We handle
4139 % undefinedness of the command in a fairly messy way, although it probably
4140 % works.  Maybe.
4141 %
4142 %    \begin{macrocode}
4143 \def\hlx@loop#1{%
4144   \ifx#1\q@delim\else%
4145     \@ifundefined{hlx@cmd@\string#1}{%
4146       \expandafter\hlx@loop%
4147     }{%
4148       \csname hlx@cmd@\string#1\expandafter\endcsname%
4149     }%
4150   \fi%
4151 }
4152 %    \end{macrocode}
4153 %
4154 % \end{macro}
4155 %
4156 % \begin{macro}{\hlxdef}
4157 %
4158 % New |\hlx| commands can be defined using |\hlxdef|.  This is a simple
4159 % abbreviation.
4160 %
4161 %    \begin{macrocode}
4162 \def\hlxdef#1{\@namedef{hlx@cmd@#1}}
4163 %    \end{macrocode}
4164 %
4165 % \end{macro}
4166 %
4167 % \begin{macro}{\hlx h}
4168 %
4169 % Handle an \lit{h} character.  Just do an |\hline| and return to the loop.
4170 % We look ahead to see if there's another \lit{h} coming up, and if so
4171 % insert two |\hline| commands.  This strange (and inefficient) behaviour
4172 % keeps packages which redefine |\hline| happy.
4173 %
4174 %    \begin{macrocode}
4175 \hlxdef h#1{%
4176   \noalign{%
4177     \ifx#1h\def\@tempa{\hline@prep\hline@prep\hlx@loop}%
4178     \else\def\@tempa{\hline@prep\hlx@loop#1}%
4179     \fi\expandafter
4180   }%
4181   \@tempa%
4182 }
4183 %    \end{macrocode}
4184 %
4185 % \end{macro}
4186 %
4187 % \begin{macro}{\hlx b}
4188 %
4189 % The \lit{b} character does a nifty backspace, for \package{longtable}'s
4190 % benefit.
4191 %
4192 %    \begin{macrocode}
4193 \hlxdef b{\noalign{\kern-\arrayrulewidth}\hlx@loop}
4194 %    \end{macrocode}
4195 %
4196 % \end{macro}
4197 %
4198 % \begin{macro}{\hlx /}
4199 %
4200 % The `"/"' character allows a page break at the current position.
4201 %
4202 %    \begin{macrocode}
4203 \hlxdef /{\noalign{\ifnum0=`}\fi\@testopt\hlx@cmd@break@i0}
4204 \def\hlx@cmd@break@i[#1]{\ifnum0=`{\fi}\pagebreak[#1]\hlx@loop}
4205 %    \end{macrocode}
4206 %
4207 % \end{macro}
4208 %
4209 % \begin{macro}{\hlx v}
4210 % \begin{macro}{\hlx z}
4211 %
4212 % Handle a \lit{v} or \lit{z} character.  This is rather like the |\vgap|
4213 % code above, although there are syntactic differences.
4214 %
4215 %    \begin{macrocode}
4216 \hlxdef v{\hlx@vgap\doublerulesep}
4217 \hlxdef z{\hlx@vgap\tab@rulewidth}
4218 \def\hlx@vgap#1{%
4219   \noalign{\nobreak}%
4220   \omit%
4221   \iffalse{\fi\ifnum0=`}\fi%
4222   \global\let\vgap@after\hlx@loop%
4223   \@ifnextchar[{\hlx@vgap@i{#1}}{\hlx@vgap@ii\vgap@simple{#1}}%
4224 }
4225 \def\hlx@vgap@i#1[#2]{%
4226   \ifx!#2!\def\@tempa{\hlx@vgap@ii\vgap@simple{#1}}%
4227   \else\def\@tempa{\hlx@vgap@ii{\vgap@spec{#2}}{#1}}\fi%
4228   \@tempa%
4229 }
4230 \def\hlx@vgap@ii#1#2{\@testopt{\hlx@vgap@iii{#1}}{#2}}
4231 \def\hlx@vgap@iii#1[#2]{#1{#2}}
4232 %    \end{macrocode}
4233 %
4234 % \end{macro}
4235 % \end{macro}
4236 %
4237 % \begin{macro}{\hlx s}
4238 %
4239 % Allow the user to leave a small gap using the \lit{s} command.
4240 %
4241 %    \begin{macrocode}
4242 \hlxdef s{%
4243   \noalign{\ifnum0=`}\fi%
4244   \nobreak%
4245   \@testopt\hlx@space@i\doublerulesep%
4246 }
4247 \def\hlx@space@i[#1]{%
4248   \vskip#1%
4249   \tab@addruleheight{#1}%
4250   \ifnum0=`{\fi}%
4251   \hlx@loop%
4252 }
4253 %    \end{macrocode}
4254 %
4255 % \end{macro}
4256 %
4257 % \begin{macro}{\hlx c}
4258 %
4259 % We might as well allow a \lit{c} command to do a |\cline|.  The fix to
4260 % |\cline| permeates here.
4261 %
4262 %    \begin{macrocode}
4263 \hlxdef c#1{\cline@do{#1}\hlx@loop}
4264 %    \end{macrocode}
4265 %
4266 % \end{macro}
4267 %
4268 % \begin{macro}{\hlx ?}
4269 %
4270 % Do some arbitrary stuff which won't typeset.  Put the stuff in a box which
4271 % is discarded, just in case.
4272 %
4273 %    \begin{macrocode}
4274 \hlxdef ?#1{%
4275   \noalign{\setbox\z@\hbox{\color@begingroup#1\color@endgroup}}\hlx@loop%
4276 }
4277 %    \end{macrocode}
4278 %
4279 % \end{macro}
4280 %
4281 % \begin{macro}{\hlx !}
4282 %
4283 % Change parameters in mid-flow.
4284 %
4285 %    \begin{macrocode}
4286 \hlxdef !#1{\noalign\tab@ruleparams\hlx@loop[{#1}]}
4287 %    \end{macrocode}
4288 %
4289 % \end{macro}
4290 %
4291 % \begin{macro}{\hlx .}
4292 %
4293 % The \lit{.} character forces a start of the new column.  There's a little
4294 % problem here.  Since the \lit{.} character starts the next column, we need
4295 % to gobble any spaces following the |\hlx| command before the cell contents
4296 % actually starts.  Unfortunately, |\ignorespaces| will start the column for
4297 % us, so we can't put it in always.  We'll handle it here, then.  We'll take
4298 % the rest of the `preamble' string, and warn if it's not empty.  Then we'll
4299 % |\ignorespaces| -- this will start the column for us, so we don't need to
4300 % |\relax| any more.
4301 %
4302 %    \begin{macrocode}
4303 \hlxdef .#1\q@delim{%
4304   \ifx @#1@\else%
4305     \PackageWarning{mdwtab}{%
4306       Ignoring \protect\hlx\space command characters following a
4307       `.'\MessageBreak command%
4308     }%
4309   \fi%
4310   \ignorespaces%
4311 }
4312 %    \end{macrocode}
4313 %
4314 % \end{macro}
4315 %
4316 % \begin{macro}{\hlx +}
4317 % \begin{macro}{\nextrow}
4318 %
4319 % The \lit{+} subcommand just steps the table-row counter.
4320 %
4321 %    \begin{macrocode}
4322 \hlxdef +{\nextrow\hlx@loop}
4323 \def\nextrow{\noalign{\ifnum0=`}\fi\@testopt\nextrow@i\@ne}
4324 \def\nextrow@i[#1]{\global\advance\c@tabrow#1\ifnum0=`{\fi}}
4325 %    \end{macrocode}
4326 %
4327 % \end{macro}
4328 % \end{macro}
4329 %
4330 %
4331 % \subsection{Starting new table rows}
4332 %
4333 % We take a break from careful mouthery at last, and start playing with
4334 % newlines.  The standard one allows pagebreaks in unboxed tables, which
4335 % isn't really too desirable.
4336 %
4337 % Anyway, we'll try to make this macro rather more reusable than the standard
4338 % one.  Here goes.
4339 %
4340 % \begin{macro}{\@arraycr}
4341 %
4342 % We pass lots of information to a main parser macro, and expect it to cope.
4343 %
4344 %    \begin{macrocode}
4345 \def\@arraycr{\tab@arraycr{}}
4346 \def\tab@arraycr#1{\tab@cr{\tab@tabcr{#1}}{}{}}
4347 %    \end{macrocode}
4348 %
4349 % Now to actually do the work.  |\tab@cr| passes us the skip size, and the
4350 % appropriate one of the two arguments given above (both of which are empty)
4351 % depending on the presence of the $*$.
4352 %
4353 %    \begin{macrocode}
4354 \def\tab@tabcr#1#2{%
4355 %    \end{macrocode}
4356 %
4357 % If the total height I need to add between rows (from the optional argument
4358 % and the `extrasep' parameter) is greater than zero, I'll handle this by
4359 % extending the strut slightly.  I'm not actually sure whether this is the
4360 % right thing to do, to be honest, although it's easier than trying to
4361 % to an automatic |\vgap|, because I need to know which columns to skip.
4362 % If the space is less than zero, I'll just insert the vertical space with
4363 % in a |\noalign|.
4364 %
4365 % First, to calculate how much space needs adding.
4366 %
4367 %    \begin{macrocode}
4368   \setlength\dimen@{#2}%
4369   \advance\dimen@\tab@extrasep%
4370 %    \end{macrocode}
4371 %
4372 % If the height is greater than zero, I need to play with the strut.  I must
4373 % bear in mind that the current table cell (which I'm still in, remember)
4374 % may be in vertical mode, and I may or may not be in a paragraph.
4375 %
4376 % If I am in vertical mode, I'll backpedal to the previous box and put the
4377 % strut in an hbox superimposed on the previous baseline.  Otherwise, I can
4378 % just put the strut at the end of the text.  (This works in either LR
4379 % or paragraph mode as long as I'm not between paragraphs.)  Again, Rowland's
4380 % empty cell bug strikes.  (See |\tab@epar| for details.)
4381 %
4382 %    \begin{macrocode}
4383   \ifdim\dimen@>\z@%
4384     \ifvmode%
4385       \unskip\ifdim\prevdepth>-\@m\p@\kern-\prevdepth\fi%
4386       \nointerlineskip\expandafter\hbox%
4387     \else%
4388       \@maybe@unskip\expandafter\@firstofone%
4389     \fi%
4390     {\advance\dimen@\dp\@arstrutbox\vrule\@depth\dimen@\@width\z@}%
4391   \fi%
4392 %    \end{macrocode}
4393 %
4394 % This table cell works as a group (which is annoying here).  I'll copy the
4395 % interrow gap into a global register so that I can use it in the |\noalign|.
4396 %
4397 %    \begin{macrocode}
4398   \global\dimen\@ne\dimen@%
4399   \cr%
4400   \noalign{%
4401     #1%
4402     \ifdim\dimen\@ne<\z@\vskip\dimen\@ne\relax\fi%
4403   }%
4404   \@gobble%
4405 }
4406 %    \end{macrocode}
4407 %
4408 % \end{macro}
4409 %
4410 % \begin{macro}{\tab@setcr}
4411 %
4412 % To set the |\\| command correctly in each table cell, we make it a part of
4413 % the preamble (in |\tab@midtext|) to call this routine.  It's easy -- just
4414 % saves the preamble from being huge.
4415 %
4416 %    \begin{macrocode}
4417 \def\tab@setcr{\let\\\tabularnewline}
4418 %    \end{macrocode}
4419 %
4420 % \end{macro}
4421 %
4422 % \begin{macro}{\tab@cr}
4423 %
4424 % Now we do the parsing work.  This is fun.  Note the revenge of the funny
4425 % braces here.  Nothing to worry about, honest.  The tricky bit is to keep
4426 % track of which arguments are which.  (Thanks to David Carlisle for pointing
4427 % out that I'd missed out the |\relax| here.)
4428 %
4429 %    \begin{macrocode}
4430 \def\tab@cr#1#2#3{%
4431   \relax%
4432   \iffalse{\fi\ifnum0=`}\fi%
4433   \@ifstar{\tab@cr@i{#1}{#3}}{\tab@cr@i{#1}{#2}}%
4434 }
4435 \def\tab@cr@i#1#2{\@testopt{\tab@cr@ii{#1}{#2}}\z@}
4436 \def\tab@cr@ii#1#2[#3]{\ifnum0=`{}\fi#1{#3}{#2}}
4437 %    \end{macrocode}
4438 %
4439 % \end{macro}
4440 %
4441 %
4442 % \subsection{Gratuitous grotesquery}
4443 %
4444 % So far we've had an easy-ish ride (or should that be \emph{queasy}?).  Now
4445 % for something unexplainably evil.  We convince \LaTeX\ that it's loaded the
4446 % \package{array} package, so that packages which need it think they've got
4447 % it.
4448 %
4449 % The bogus date is the same as the date for the \package{array} package I've
4450 % got here -- this will raise a warning if Frank updates his package which
4451 % should filter back to me telling me that there's something I need to
4452 % know about.
4453 %
4454 % The messing with |\xdef| and the funny parsing ought to insert the current
4455 % \package{mdwtab} version and date into the fake \package{array} version
4456 % string, giving a visible clue to the user that this isn't the real
4457 % \package{array} package.
4458 %
4459 %    \begin{macrocode}
4460 \begingroup
4461 \catcode`.=11
4462 \def\@tempa#1 #2 #3\@@{#1 #2}
4463 \xdef\ver@array.sty
4464   {1995/11/19 [mdwtab.sty \expandafter\@tempa\ver@mdwtab.sty\@@]}
4465 \endgroup
4466 %    \end{macrocode}
4467 %
4468 %
4469 % \subsection{Error messages}
4470 %
4471 % I've put all the error messages together, where I can find them, translate
4472 % them or whatever.
4473 %
4474 % First, some token-space saving (which also saves my fingers):
4475 %
4476 %    \begin{macrocode}
4477 \def\tab@error{\PackageError{mdwtab}}
4478 %    \end{macrocode}
4479 %
4480 % Now do the error messages.
4481 %
4482 %    \begin{macrocode}
4483 \def\tab@err@misscol{%
4484   \tab@error{Missing column type}{%
4485     I'm lost.  I was expecting something describing^^J%
4486     the type of the current column, but you seem to^^J%
4487     have missed it out.  I've inserted a type `l'^^J%
4488     column here in the hope that this makes sense.%
4489   }%
4490 }
4491 %    \end{macrocode}
4492 %
4493 %    \begin{macrocode}
4494 \def\tab@err@oddgroup{%
4495   \tab@error{Misplaced group in table preamble}{%
4496     I've found an open brace character in your preamble^^J%
4497     when I was expecting a specifier character.  I'm^^J%
4498     going to gobble the whole group and carry on as if^^J%
4499     I'd never seen it.%
4500   }%
4501 }
4502 %    \end{macrocode}
4503 %
4504 %    \begin{macrocode}
4505 \def\tab@err@undef#1{%
4506   \tab@error{Unknown `\tab@colset' preamble character `\string#1'}{%
4507     I don't understand what you meant by typing this^^J%
4508     character.  Anyway, I'll ignore it this time around.^^J%
4509     Just don't you do it again.%
4510   }%
4511 }
4512 %    \end{macrocode}
4513 %
4514 %    \begin{macrocode}
4515 \def\tab@err@unbrh{%
4516   \tab@error{Can't use unboxed tabular in LR mode}{%
4517     You've asked for a tabular or array environment with^^J%
4518     `L', `C' or `R' as the position specifier, but you're^^J%
4519     in LR (restricted horizontal) mode, so it won't work.^^J%
4520     I'll assume you really meant `c' and soldier on.%
4521   }%
4522 }
4523 %    \end{macrocode}
4524 %
4525 %    \begin{macrocode}
4526 \def\tab@err@unbmm{%
4527   \tab@error{Can't use unboxed tabular in maths mode}{%
4528     You've asked for a tabular or array environment with^^J%
4529     `L', `C' or `R' as the position specifier, but you're^^J%
4530     in maths mode, so it won't work.  I'll pretend that^^J%
4531     you really typed `c', and that this is all a bad dream.%
4532   }%
4533 }
4534 %    \end{macrocode}
4535 %
4536 %    \begin{macrocode}
4537 \def\tab@err@unbext{%
4538   \tab@error{Can't extend unboxed tabulars}{%
4539     You're trying to use kludgy extensions (e.g.,^^J%
4540     `delarray') on an array or tabular with `L', `C'^^J%
4541     or `R' as the position specifier.  I'll assume you^^J%
4542     subconsciously wanted a `c' type all along.%
4543   }%
4544 }
4545 %    \end{macrocode}
4546 %
4547 %    \begin{macrocode}
4548 \def\tab@err@multi{%
4549   \tab@error{More than one column in a \protect\multicolumn}{%
4550     You've put more than one column into a \string\multicolumn^^J%
4551     descriptor.  It won't work.  I have no idea what^^J%
4552     will happen, although it won't be pleasant.  Hold^^J%
4553     on tight now...%
4554   }%
4555 }
4556 %    \end{macrocode}
4557 %
4558 %    \begin{macrocode}
4559 \def\tab@err@range{%
4560   \tab@error{Expected `,' or `<end>' in range list}{%
4561     I was expecting either the end of the range list,^^J%
4562     or a comma, followed by another range.  I've^^J%
4563     inserted a comma to try and get me back on track.^^J%
4564     Good luck.%
4565   }%
4566 }
4567 %    \end{macrocode}
4568 %
4569 %
4570 % \subsection{Loading the colour package}
4571 %
4572 % If requested, we load the \package{mtcolour} package here.  This ensures
4573 % that it can patch this code if it needs to.
4574 %
4575 %    \begin{macrocode}
4576 \iftab@colour
4577   \RequirePackage{mtcolour}
4578 \fi
4579 %    \end{macrocode}
4580 %
4581 % That's it.  No more.  Move along please.
4582 %
4583 %    \begin{macrocode}
4584 %</mdwtab>
4585 %    \end{macrocode}
4586 %
4587 %
4588 %^^A-------------------------------------------------------------------------
4589 % \section{Implementation of \package{mtcolour}}
4590 %
4591 %
4592 % This is in a separate package to avoid dragging in the \package{color}
4593 % package if it's unwanted.
4594 %
4595 % I prefer English spellings.  Here's a trivial redirection for Americans.
4596 %
4597 %    \begin{macrocode}
4598 %<*color>
4599 \DeclareOption*{\PassOptionsToPackage{\CurrentOption}{mtcolour}}
4600 \ProcessOptions
4601 \RequirePackage{mtcolour}
4602 %</color>
4603 %    \end{macrocode}
4604 %
4605 % And now we can start the thing properly.
4606 %
4607 %    \begin{macrocode}
4608 %<*colour>
4609 \RequirePackage{color}
4610 %    \end{macrocode}
4611 %
4612 %
4613 % \subsection{Cell background colours}
4614 %
4615 % First, some simple preliminaries.  The |\iftab@colour| switch is set if the
4616 % current cell is meant to have a colour.
4617 %
4618 %    \begin{macrocode}
4619 \newif\iftab@colour
4620 \tab@colourfalse
4621 %    \end{macrocode}
4622 %
4623 % We shall store the cell colour information in |\tab@cellcolour|, and the
4624 % row colour information as |\tab@rowcolour|.  Because of the structure of
4625 % tables, we need to make global assignments; so we must copy the current
4626 % value away at the start of a table and put the value back at the end.  In
4627 % order to transfer the overhang information reliably, we use a separate
4628 % control sequence |\tab@colouroverhangs| for that -- otherwise |\color| can
4629 % corrupt it.
4630 %
4631 %    \begin{macrocode}
4632 \tab@addhookbefore\tab@beginhook{%
4633   \let\tab@saverowcolour\tab@rowcolour%
4634   \let\tab@savecolouroverhangs\tab@colouroverhangs%
4635   \let\tab@savecellcolour\tab@cellcolour%
4636 }
4637 \tab@addhookafter\tab@endhook{%
4638   \global\let\tab@rowcolour\tab@saverowcolour%
4639   \global\let\tab@colouroverhangs\tab@savecolouroverhangs%
4640   \global\let\tab@cellcolour\tab@savecellcolour%
4641 }
4642 %    \end{macrocode}
4643 %
4644 % Initially, there are no colours.
4645 %
4646 %    \begin{macrocode}
4647 \let\tab@rowcolour\@empty%
4648 \let\tab@cellcolour\@empty%
4649 \let\tab@colouroverhangs\@empty%
4650 %    \end{macrocode}
4651 %
4652 % \begin{macro}{\@snarfcolour}
4653 %
4654 % Reading a colour specification is something we'll need to do a few times,
4655 % so an abstraction is useful.  Its single argument is a continuation to
4656 % which we pass a colour-spec acceptable to the |\color| command.  (This is
4657 % the same code as found in the \package{sverb} package.  Remember to keep
4658 % them in step.)
4659 %
4660 %    \begin{macrocode}
4661 \def\@snarfcolour#1{%
4662   \@ifnextchar[{\@snarfcolour@i{#1}}{\@snarfcolour@ii{#1}{}}%
4663 }
4664 \def\@snarfcolour@i#1[#2]{\@snarfcolour@ii{#1}{[#2]}}
4665 \def\@snarfcolour@ii#1#2#3{#1{#2{#3}}}
4666 %    \end{macrocode}
4667 %
4668 % \end{macro}
4669 %
4670 % \begin{macro}{\cellcolour}
4671 %
4672 % Setting a cell colour is a matter of stashing the right declarations in
4673 % |\tab@cellcolour| and |\tab@colouroverhangs|.  Note that the overhangs end
4674 % up in |\dimen0| and |\dimen2|.
4675 %
4676 %    \begin{macrocode}
4677 \def\cellcolour{%
4678   \@ifstar{\tab@ccol@i{\let\tab@rowcolour\@empty}}{\tab@ccol@i{}}%
4679 }
4680 \def\tab@ccol@i#1{\@snarfcolour{\tab@ccol@ii{#1}}}
4681 \def\tab@ccol@ii#1#2{\@testopt{\tab@ccol@iii{#2#1}}\z@}
4682 \def\tab@ccol@iii#1[#2]{\@testopt{\tab@ccol@iv{#1}{#2}}{#2}}
4683 \def\tab@ccol@iv#1#2[#3]{%
4684   \gdef\tab@cellcolour{\color#1\tab@colourtrue}%
4685   \gdef\tab@colouroverhangs{%
4686     \setlength\dimen@{#2}%
4687     \setlength{\dimen\tw@}{#3}%
4688   }%
4689 }
4690 %    \end{macrocode}
4691 %
4692 % \end{macro}
4693 %
4694 % \begin{macro}{\rowcolour}
4695 %
4696 % Setting the global row colour is simpler, because we don't mess with
4697 % overhangs.
4698 %
4699 %    \begin{macrocode}
4700 \def\rowcolour{\@snarfcolour\tab@setrowcolour}
4701 \let\rowcolor\rowcolour
4702 \def\tab@setrowcolour#1{%
4703   \gdef\tab@rowcolour{\color#1\tab@colourtrue}%
4704 }
4705 %    \end{macrocode}
4706 %
4707 % \end{macro}
4708 %
4709 % \begin{macro}{\rowcolouroff}
4710 %
4711 % And turning the global colouring off is easy.
4712 %
4713 %    \begin{macrocode}
4714 \def\rowcolouroff{\global\let\tab@rowcolour\@empty}
4715 \let\rowcoloroff\rowcolouroff
4716 %    \end{macrocode}
4717 %
4718 % \end{macro}
4719 %
4720 % \begin{macro}{\tab@colourleft}
4721 %
4722 % Now we start on the table-cell hooks.  The left hook starts a box which
4723 % will capture the cell's text and natural width.  We add the hook to the
4724 % rule list as well, so that we can colour the bits in |\vgap|s correctly.
4725 %
4726 %    \begin{macrocode}
4727 \tab@addhookbefore\tab@lefttexthook\tab@colourleft
4728 \tab@addhookbefore\tab@leftruletexthook\tab@colourleft
4729 \def\tab@colourleft{%
4730   \global\let\tab@cellcolour\@empty%
4731   \global\let\tab@colouroverhangs\@empty%
4732   \setbox\z@\hbox\bgroup\color@begingroup%
4733 }
4734 %    \end{macrocode}
4735 %
4736 % \end{macro}
4737 %
4738 % \begin{macro}{\tab@colourright}
4739 %
4740 % The right hook will insert an appropriate rule behind the cell and
4741 % retypeset the cell contents over the top.  Note that the stretch in a table
4742 % cell is exactly 1\,fil.  Because we add (leaders) and subtract (negative
4743 % |\hskip|) 1\,fil, we retain this stretch exactly.  Don't bother unless
4744 % there's actually some colouring.
4745 %
4746 %    \begin{macrocode}
4747 \tab@addhookafter\tab@righttexthook\tab@colourright
4748 \tab@addhookafter\tab@rightruletexthook\tab@colourright
4749 \def\tab@colourright{%
4750   \color@endgroup\egroup%
4751   \color@begingroup%
4752   \global\tab@colourfalse%
4753   \tab@cellcolour\tab@rowcolour%
4754   \dimen@\z@\dimen\tw@\z@\tab@colouroverhangs%
4755   \iftab@colour%
4756     \skip@\wd\z@\advance\skip@\z@\@plus1fil%
4757     \skip\tw@\skip@%
4758     \kern-\dimen@%
4759     \advance\skip\tw@\dimen@%
4760     \advance\skip\tw@\dimen\tw@%
4761     \leaders\vrule\hskip\skip\tw@%
4762     \kern-\dimen\tw@%
4763     \hskip-\skip@%
4764   \fi%
4765   \color@endgroup%
4766   \unhbox\z@%
4767 }
4768 %    \end{macrocode}
4769 %
4770 % \end{macro}
4771 %
4772 %
4773 % \subsection{Coloured rules}
4774 %
4775 % We hook ourselves onto the rule-parameters edifice.  This is rather
4776 % straightforward.
4777 %
4778 %    \begin{macrocode}
4779 \tab@addhookafter\tab@rp@inithook{%
4780   \let\tab@rulecolour\@empty%
4781   \let\tab@rulecolourmodel\@empty%
4782 }
4783 \mkdef{mdwtab:rule}{colour}{\tab@setrulecolour{#1}}
4784 \mkdef{mdwtab:rule}{colourmodel}{\tab@setrulecolourmodel{#1}}
4785 \mkdef{mdwtab:rule}{color}{\tab@setrulecolour{#1}}
4786 \mkdef{mdwtab:rule}{colormodel}{\tab@setrulecolourmodel{#1}}
4787 \mkdef{mdwtab:rule}{nocolour}*{\let\tab@rulecolour\@empty}
4788 \mkdef{mdwtab:rule}{nocolor}*{\let\tab@rulecolour\@empty}
4789 \mkdef{mdwtab:rule}{nocolourmodel}*{\let\tab@rulecolourmodel\@empty}
4790 \mkdef{mdwtab:rule}{nocolormodel}*{\let\tab@rulecolourmodel\@empty}
4791 \def\tab@setrulecolour#1{%
4792   \def\tab@rulecolour{\color\tab@rulecolourmodel{#1}}%
4793 }
4794 \def\tab@setrulecolourmodel#1{\def\tab@rulecolourmodel{[#1]}}
4795 \tab@addhookafter\tab@rp@sethook{\tab@rulecolour}
4796 %    \end{macrocode}
4797 %
4798 %
4799 % \subsection{Other stuff}
4800 %
4801 % \begin{macro}{\ifmod}
4802 %
4803 % \syntax{"\\ifmod{"$x$"}{"$m$"}{"$y$"}{"<yes>"}{"<no>"}"} -- if $x \bmod m =
4804 % y$ then do \<yes>; otherwise do \<no>.
4805 %
4806 %    \begin{macrocode}
4807 \def\ifmod#1#2#3{%
4808   \begingroup%
4809   \@tempcnta#1%
4810   \@tempcntb#2%
4811   \count@\@tempcnta%
4812   \divide\count@\@tempcntb%
4813   \multiply\count@\@tempcntb%
4814   \advance\@tempcnta-\count@%
4815   \count@#3\relax%
4816   \ifnum\@tempcnta=\count@\endgroup\expandafter\@firstoftwo%
4817   \else\endgroup\expandafter\@secondoftwo\fi%
4818 }
4819 %    \end{macrocode}
4820 %
4821 % \end{macro}
4822 %
4823 % Done.
4824 %
4825 %    \begin{macrocode}
4826 %</colour>
4827 %    \end{macrocode}
4828 %
4829 %^^A-------------------------------------------------------------------------
4830 % \section{Implementation of \package{mathenv}}
4831 %
4832 %
4833 % This is in a separate package, mainly to avoid wasting people's memory.
4834 %
4835 %    \begin{macrocode}
4836 %<*mathenv>
4837 %    \end{macrocode}
4838 %
4839 %
4840 % \subsection{Options handling}
4841 %
4842 % We need to be able to cope with \textsf{fleqn} and \textsf{leqno} options.
4843 % This will adjust our magic modified \env{eqnarray} environment
4844 % appropriately.
4845 %
4846 %    \begin{macrocode}
4847 \newif\if@fleqn
4848 \newif\if@leqno
4849 \DeclareOption{fleqn}{\@fleqntrue}
4850 \DeclareOption{leqno}{\@leqnotrue}
4851 \ProcessOptions
4852 %    \end{macrocode}
4853 %
4854 % We use the \package{mdwtab} package for all its nice table handling things.
4855 % (Oh, and to inflict it on users who want to do nice equations and don't
4856 % care about our tables.)
4857 %
4858 %    \begin{macrocode}
4859 \RequirePackage{mdwtab}
4860 %    \end{macrocode}
4861 %
4862 %
4863 % \subsection{Some useful registers}
4864 %
4865 % The old \LaTeX\ version puts the equation numbers in by keeping a count of
4866 % where it is in the alignment.  Since I don't know how may columns there are
4867 % going to be, I'll just use a switch in the preamble to tell me to stop
4868 % tabbing.
4869 %
4870 %    \begin{macrocode}
4871 \newif\if@eqalast
4872 %    \end{macrocode}
4873 %
4874 % Now define some useful length parameters.  First allocate them:
4875 %
4876 %    \begin{macrocode}
4877 \newskip\eqaopenskip
4878 \newskip\eqacloseskip
4879 \newskip\eqacolskip
4880 \newskip\eqainskip
4881 \newskip\splitleft
4882 \newskip\splitright
4883 %    \end{macrocode}
4884 %
4885 % Now assign some default values.  Users can play with these if they really
4886 % want although I can't see the point myself.
4887 %
4888 %    \begin{macrocode}
4889 \AtBeginDocument{%
4890   \eqacloseskip\@centering%
4891   \eqacolskip1.5em\@plus\@m\p@
4892   \eqainskip\z@%
4893   \if@fleqn%
4894     \eqaopenskip\mathindent%
4895     \splitleft\mathindent\relax%
4896     \splitright\mathindent\@minus\mathindent\relax%
4897   \else%
4898     \eqaopenskip\@centering%
4899     \splitleft2.5em\@minus2.5em%
4900     \splitright\splitleft%
4901   \fi%
4902   \relax%
4903 }
4904 %    \end{macrocode}
4905 %
4906 %
4907 % \subsection{A little display handling}
4908 %
4909 % I'm probably going a little far here, and invading territory already
4910 % claimed by the \package{amsmath} stuff (and done a good deal better than
4911 % I can be bothered to do), but just for completeness, this is how we handle
4912 % attempts to put displays inside other displays without screwing up the
4913 % spacing.
4914 %
4915 % \begin{macro}{\dsp@startouter}
4916 %
4917 % This is how we start an outermost display.  It's fairly easy really.  We
4918 % make |\dsp@start| start an inner display, and make |\dsp@end| close the
4919 % outer display.
4920 %
4921 %    \begin{macrocode}
4922 \def\dsp@startouter{%
4923   \let\dsp@end\dsp@endouter%
4924   $$%
4925 }
4926 %    \end{macrocode}
4927 %
4928 % \end{macro}
4929 %
4930 % \begin{macro}{\dsp@endouter}
4931 %
4932 % Ending the outer display is utterly trivial.
4933 %
4934 %    \begin{macrocode}
4935 \def\dsp@endouter{$$}
4936 %    \end{macrocode}
4937 %
4938 % \end{macro}
4939 %
4940 % \begin{macro}{\dsp@startinner}
4941 %
4942 % Starting inner displays is done in a vbox (actually I choose |\vbox| or
4943 % |\vtop| depending on the setting of \textsf{leqno} to put the equation
4944 % number the right way round).
4945 %
4946 %    \begin{macrocode}
4947 \def\dsp@startinner{%
4948   \let\dsp@end\dsp@endinner%
4949   \if@fleqn\kern-\mathindent\fi%
4950   \if@leqno\vtop\else\vtop\fi\bgroup%
4951 }
4952 %    \end{macrocode}
4953 %
4954 % \end{macro}
4955 %
4956 % \begin{macro}{\dsp@endinner}
4957 %
4958 % Ending an inner display is also really easy.
4959 %
4960 %    \begin{macrocode}
4961 \def\dsp@endinner{\egroup}
4962 %    \end{macrocode}
4963 %
4964 % \end{macro}
4965 %
4966 % \begin{macro}{\dsp@start}
4967 %
4968 % This is what other bits of code uses to start displays.  It's one of the
4969 % start macros up above, and outer by default.
4970 %
4971 %    \begin{macrocode}
4972 \def\dsp@start{%
4973   \ifmmode%
4974     %\ifinner\mth@err@mdsp\fi%
4975     \expandafter\dsp@startinner%
4976   \else%
4977     \ifhmode\ifinner\mth@err@hdsp\fi\fi%
4978     \expandafter\dsp@startouter%
4979   \fi%
4980 }
4981 %    \end{macrocode}
4982 %
4983 % \end{macro}
4984 %
4985 % \begin{macro}{\dsp@tabpause}
4986 %
4987 % This sets up the correct pre- and postambles for the |\tabpause| macro in
4988 % maths displays.  This is fairly simple stuff.
4989 %
4990 %    \begin{macrocode}
4991 \def\dsp@tabpause{%
4992   \def\tab@startpause%
4993     {\penalty\postdisplaypenalty\vskip\belowdisplayskip}%
4994   \def\tab@endpause%
4995     {\penalty\predisplaypenalty\vskip\abovedisplayskip}%
4996 }
4997 %    \end{macrocode}
4998 %
4999 % \end{macro}
5000 %
5001 %
5002 % \subsection{The \env{eqnarray} environment}
5003 %
5004 % We allow the user to play with the style if this is really wanted.  I dunno
5005 % why, really.  Maybe someone wants very small alignments.
5006 %
5007 %    \begin{macrocode}
5008 \let\eqastyle\displaystyle
5009 %    \end{macrocode}
5010 %
5011 % \subsubsection{The main environments}
5012 %
5013 % \begin{environment}{eqnarray}
5014 % \begin{environment}{eqnarray*}
5015 %
5016 % We define the toplevel commands here.  They just add in default arguments
5017 % and then call |\@eqnarray| with a preamble string.  We handle equation
5018 % numbers by setting up a default (|\eqa@defnumber|) which is put into
5019 % the final column.  At the beginning of each row, we globally |\let|
5020 % |\eqa@number| equal to |\eqa@defnumber|.  The |\eqnumber| macro just
5021 % changes |\eqa@number| as required.  Since |\eqa@number| is changed globally
5022 % we must save it in this environment.
5023 %
5024 % First, we must sort out the optional arguments and things.  This is really
5025 % easy.  The only difference between the starred and non-starred environments
5026 % is the default definition of |\eqa@defnumber|.
5027 %
5028 %    \begin{macrocode}
5029 \def\eqnarray{%
5030   \eqnarray@i\eqa@eqcount%
5031 }
5032 \@namedef{eqnarray*}{\eqnarray@i{}}
5033 \def\eqnarray@i#1{\@testopt{\eqnarray@ii{#1}}{rcl}}
5034 %    \end{macrocode}
5035 %
5036 % Right.  Now for the real work.  The first argument is the default numbering
5037 % tokens; the second is the preamble string.
5038 %
5039 %    \begin{macrocode}
5040 \def\eqnarray@ii#1[#2]{%
5041 %    \end{macrocode}
5042 %
5043 % Set up the equation counter and labels correctly.
5044 %
5045 % \medskip\par\noindent|\begin{rant}|\par
5046 % The hacking with |\@currentlabel| is here because (in the author's opinion)
5047 % \LaTeX's |\refstepcounter| macro is broken.  It's currently defined as
5048 % \begin{listing}
5049 %\def\refstepcounter#1{%
5050 %  \stepcounter{#1}%
5051 %  \protected@edef\@currentlabel%
5052 %    {\csname p@#1\endcsname\csname the#1\endcsname}%
5053 %}
5054 % \end{listing}
5055 % which means that the current label gets `frozen' as soon as you do the
5056 % counter step.  By redefining the macro as
5057 % \begin{listing}
5058 %\def\refstepcounter#1{%
5059 %  \stepcounter{#1}%
5060 %  \edef\@currentlabel{%
5061 %    \expandafter\noexpand\csname p@#1\endcsname%
5062 %    \expandafter\noexpand\csname the#1\endcsname%
5063 %  }%
5064 %}
5065 % \end{listing}
5066 % these sorts of problems would be avoided, without any loss of functionality
5067 % or compatibility that I can see.
5068 % \par\noindent|\end{rant}|\par
5069 %
5070 %    \begin{macrocode}
5071   \stepcounter{equation}%
5072   \def\@currentlabel{\p@equation\theequation}%
5073 %    \end{macrocode}
5074 %
5075 % The next step is to set up the numbering.  I must save the old numbering
5076 % so I can restore it later (once in the alignment, I must assign these
5077 % things globally).
5078 %
5079 %    \begin{macrocode}
5080   \let\eqa@oldnumber\eqa@number%
5081   \def\eqa@defnumber{#1}%
5082   \global\let\eqa@number\eqa@defnumber%
5083 %    \end{macrocode}
5084 %
5085 % The |\if@eqalastfalse| switch is false everywhere except when we're in the
5086 % final column.
5087 %
5088 %    \begin{macrocode}
5089   \@eqalastfalse%
5090 %    \end{macrocode}
5091 %
5092 % Remove the |\mathsurround| kerning, since it will look very odd inside
5093 % the display.  We have our own spacing parameters for configuring these
5094 % things, so |\mathsurround| is unnecessary.
5095 %
5096 %    \begin{macrocode}
5097   \m@th%
5098 %    \end{macrocode}
5099 %
5100 % Time to parse the preamble string now.  I must choose the correct column
5101 % set, initialise the preamble parser and set up the various macros.  The%
5102 % extra `|@{\tabskip\eqacloseskip}|' item sets up the tabskip glue to centre
5103 % the alignment properly.
5104 %
5105 %    \begin{macrocode}
5106   \colset{eqnarray}%
5107   \tab@initread%
5108   \def\tab@tabtext{&\tabskip\z@skip}%
5109   \tab@preamble{\tabskip\z@skip}%
5110   \tab@readpreamble{#2@{\tabskip\eqacloseskip}}%
5111   \dsp@tabpause%
5112 %    \end{macrocode}
5113 %
5114 % Now for some final setting up.  The column separation is set from the
5115 % user's parameter, the |\everycr| tokens are cleared, and I set up the
5116 % newline command appropriately.
5117 %
5118 %    \begin{macrocode}
5119   \col@sep.5\eqainskip%
5120   \everycr{}%
5121   \let\\\@eqncr%
5122 %    \end{macrocode}
5123 %
5124 % Now start a maths display and do the alignment.  Set up the left hand
5125 % tabskip glue to centre the alignment, and do the actual alignment.
5126 % The preamble used is mainly that generated from the user's string, although
5127 % the stuff at the end is how we set up the equation number -- it repeats
5128 % appropriately so we can always find it.
5129 %
5130 %    \begin{macrocode}
5131   \dsp@start%
5132   \tabskip\eqaopenskip%
5133   \halign to\displaywidth\expandafter\bgroup%
5134     \the\tab@preamble%
5135     &&\eqa@lastcol\hb@xt@\z@{\hss##}\tabskip\z@\cr%
5136 }
5137 %    \end{macrocode}
5138 %
5139 % Now for the end of the environment.  This is really easy.  Set the final
5140 % equation number, close the |\halign|, tidy up the equation counter (it's
5141 % been stepped once too many times) and close the display.
5142 %
5143 %    \begin{macrocode}
5144 \def\endeqnarray{%
5145   \eqa@eqnum%
5146   \egroup%
5147   \dsp@end%
5148   \global\let\eqa@number\eqa@oldnumber%
5149   \global\@ignoretrue%
5150   \global\advance\c@equation\m@ne%
5151 }
5152 \expandafter\let\csname endeqnarray*\endcsname\endeqnarray
5153 %    \end{macrocode}
5154 %
5155 % \end{environment}
5156 % \end{environment}
5157 %
5158 % Now we can define the column types.
5159 %
5160 %    \begin{macrocode}
5161 \colpush{eqnarray}
5162 %    \end{macrocode}
5163 %
5164 % Note the positioning of ord atoms in the stuff below.  This will space out
5165 % relations and binops correctly when they occur at the edges of columns, and
5166 % won't affect ord atoms at the edges, because ords pack closely.
5167 %
5168 % First the easy ones.  Just stick |\hfil| in the right places and
5169 % everything will be all right.
5170 %
5171 %    \begin{macrocode}
5172 \coldef r{\tabcoltype{\hfil$\eqastyle}{{}$}}
5173 \coldef c{\tabcoltype{\hfil$\eqastyle{}}{{}$\hfil}}
5174 \coldef l{\tabcoltype{$\eqastyle{}}{$\hfil}}
5175 \coldef x{\tabcoltype{\if@fleqn\else\hfil\fi$\eqastyle}{$\hfil}}
5176 %    \end{macrocode}
5177 %
5178 % Now for the textual ones.  This is also fairly easy.
5179 %
5180 %    \begin{macrocode}
5181 \collet T [tabular]T
5182 %    \end{macrocode}
5183 %
5184 % Sort of split types of equations.  I mustn't use |\rlap| here, or
5185 % everything goes wrong -- |\\| doesn't get noticed by \TeX\ in the same way
5186 % as |\cr| does.
5187 %
5188 %    \begin{macrocode}
5189 \coldef L{\tabcoltype{\hb@xt@2em\bgroup$\eqastyle}{$\hss\egroup}}
5190 %    \end{macrocode}
5191 %
5192 % The \lit{:} column type is fairly simple.
5193 %
5194 %    \begin{macrocode}
5195 \coldef :{\tabspctype{\tabskip\eqacolskip}}
5196 \coldef q{\tabspctype{\quad}}
5197 %    \end{macrocode}
5198 %
5199 % The other column types just insert given text in an appropriate way.
5200 %
5201 %    \begin{macrocode}
5202 \collet > [tabular]>
5203 \collet < [tabular]<
5204 \collet * [tabular]*
5205 \collet @ [tabular]@
5206 %    \end{macrocode}
5207 %
5208 % Finally, the magical `|\magic|' column type, which sets the equation
5209 % number.  We set up the |\tabskip| glue properly, tab on, and set the flag
5210 % which marks the final column.  The |\eqa@lastcol| command is there to
5211 % raise an error if the user tabs over to this column.  I'll temporarily
5212 % redefine it to |\@eqalasttrue| when I enter this column legitimately.
5213 % The extra magical bits here will make the final column repeat, so that we
5214 % can find it if necessary.  Well is this column type named.
5215 %
5216 % That's it.  We can return to normal now.
5217 %
5218 %    \begin{macrocode}
5219 \colpop
5220 %    \end{macrocode}
5221 %
5222 % \subsubsection{Newline codes}
5223 %
5224 % Newline sequences (|\\|) get turned into calls of |\@eqncr|.  The job is
5225 % fairly simple, really.
5226 %
5227 %    \begin{macrocode}
5228 \def\@eqncr{\tab@cr\eqacr@i\interdisplaylinepenalty\@M}%
5229 \def\eqacr@i#1#2{%
5230   \eqa@eqnum%
5231   \noalign{\penalty#2\vskip\jot\vskip#1}%
5232 }
5233 %    \end{macrocode}
5234 %
5235 % \subsubsection{Setting equation numbers}
5236 %
5237 % \begin{macro}{\eqa@eqpos}
5238 %
5239 % Before we start, we need to generalise the flush-left number handling bits.
5240 % The macro |\eqa@eqpos| will put its argument in the right place.
5241 %
5242 %    \begin{macrocode}
5243 \def\eqa@eqpos#1{%
5244   \if@leqno%
5245     \hb@xt@.01\p@{}\rlap{\normalfont\normalcolor\hskip-\displaywidth#1}%
5246   \else%
5247     \normalfont\normalcolor#1%
5248   \fi%
5249 }
5250 %    \end{macrocode}
5251 %
5252 % \end{macro}
5253 %
5254 % \begin{macro}{\eqa@eqnum}
5255 %
5256 % Here we typeset an equation number in roughly the right place.  First I'll
5257 % redefine |\eqa@lastcol| so that it tells me I'm in the right place, and
5258 % start a loop to find that place.
5259 %
5260 %    \begin{macrocode}
5261 \def\eqa@eqnum{%
5262   \global\let\eqa@lastcol\@eqalasttrue%
5263   \eqa@eqnum@i%
5264 }
5265 %    \end{macrocode}
5266 %
5267 % Now for the loop.  The |\relax| here is absolutely vital -- it starts the
5268 % table column, inserting useful tokens like `|\eqa@lastcol|' which tell
5269 % me where I am in the alignment.  Then, if I've reached the end, I can
5270 % typeset the equation number; otherwise I go off into another macro and
5271 % step on to the next column.
5272 %
5273 %    \begin{macrocode}
5274 \def\eqa@eqnum@i{%
5275   \relax%
5276   \if@eqalast%
5277     \expandafter\eqa@eqnum@ii%
5278   \else%
5279     \expandafter\eqa@eqnum@iii%
5280   \fi%
5281 }
5282 \def\eqa@eqnum@ii{%
5283   \eqa@eqpos\eqa@number%
5284   \global\let\eqa@number\eqa@defnumber%
5285   \global\let\eqa@lastcol\eqa@@lastcol%
5286   \cr%
5287 }
5288 \def\eqa@eqnum@iii{&\eqa@eqnum@i}
5289 %    \end{macrocode}
5290 %
5291 % \end{macro}
5292 %
5293 % \begin{macro}{\eqa@lastcol}
5294 %
5295 % This is used as a marker for the final column in an \env{eqnarray}
5296 % environment.  By default it informs the user that they've been very
5297 % silly and swallows the contents of the column.  I'll redefine it to
5298 % something more useful at appropriate times, and then turn it back again.
5299 %
5300 %    \begin{macrocode}
5301 \def\eqa@@lastcol{\mth@err@number\setbox\z@}
5302 \let\eqa@lastcol\eqa@@lastcol
5303 %    \end{macrocode}
5304 %
5305 % \end{macro}
5306 %
5307 % \subsubsection{Numbering control}
5308 %
5309 % \begin{macro}{\eqnumber}
5310 %
5311 % The |\eqnumber| command sets the equation number on the current equation.
5312 % This is really easy, actually.
5313 %
5314 %    \begin{macrocode}
5315 \newcommand\eqnumber[1][\eqa@eqcount]{\gdef\eqa@number{#1}}
5316 %    \end{macrocode}
5317 %
5318 % \end{macro}
5319 %
5320 % \begin{macro}{\eqa@eqcount}
5321 %
5322 % This is how a standard equation number is set, stepping the counter and
5323 % all.  It's really easy and obvious.
5324 %
5325 %    \begin{macrocode}
5326 \def\eqa@eqcount{(\theequation)\global\advance\c@equation\@ne}
5327 %    \end{macrocode}
5328 %
5329 % \end{macro}
5330 %
5331 % \begin{macro}{\nonumber}
5332 %
5333 % The \LaTeX\ |\nonumber| command could be defined by saying
5334 % \begin{listing}
5335 %\renewcommand{\nonumber}{\eqnumber[]}
5336 % \end{listing}
5337 % but I'll be slightly more efficient and redefine |\eqa@number| directly.
5338 %
5339 %    \begin{macrocode}
5340 \def\nonumber{\global\let\eqa@number\@empty}
5341 %    \end{macrocode}
5342 %
5343 % \end{macro}
5344 %
5345 % \subsubsection{The \env{eqnalign} environment}
5346 %
5347 % As a sort of companion to \env{eqnarray}, here's an environment which does
5348 % similar things inside a box, rather than taking up the whole display width.
5349 % It uses the same column types that we've already created, so there should
5350 % be no problems.
5351 %
5352 % \begin{environment}{eqnalign}
5353 %
5354 % First, sort out some simple things like optional arguments.
5355 %
5356 %    \begin{macrocode}
5357 \def\eqnalign{\@testopt\eqnalign@i{rcl}}
5358 \def\eqnalign@i[#1]{\@testopt{\eqnalign@ii{#1}}c}
5359 %    \end{macrocode}
5360 %
5361 % Now we actually do the environment.  This is fairly easy, actually.
5362 %
5363 %    \begin{macrocode}
5364 \def\eqnalign@ii#1[#2]{%
5365   \let\\\eqn@cr%
5366   \colset{eqnarray}%
5367   \tab@initread%
5368   \def\tab@tabtext{&\tabskip\z@skip}%
5369   \tabskip\z@skip%
5370   \col@sep.5\eqainskip%
5371   \tab@readpreamble{#1}%
5372   \everycr{}%
5373   \if#2t\vtop\else%
5374     \if#2b\vbox\else%
5375       \vcenter%
5376     \fi%
5377   \fi%
5378   \bgroup%
5379   \halign\expandafter\bgroup\the\tab@preamble\cr%
5380 }
5381 %    \end{macrocode}
5382 %
5383 % Finishing the environment is even simpler.
5384 %
5385 %    \begin{macrocode}
5386 \def\endeqnalign{%
5387   \crcr%
5388   \egroup%
5389   \egroup%
5390 }
5391 %    \end{macrocode}
5392 %
5393 % \end{environment}
5394 %
5395 % \begin{macro}{\eqn@cr}
5396 %
5397 % Newlines are really easy here.
5398 %
5399 %    \begin{macrocode}
5400 \def\eqn@cr{\tab@cr\eqn@cr@i{}{}}
5401 \def\eqn@cr@i#1{\cr\noalign{\vskip\jot\vskip#1}\@gobble}
5402 %    \end{macrocode}
5403 %
5404 % \end{macro}
5405 %
5406 %
5407 % \subsection{Simple multiline equations}
5408 %
5409 % As a sort of example and abbreviation, here's a multiline display
5410 % environment which just centres everything.
5411 %
5412 % \begin{environment}{eqlines}
5413 %
5414 % We just get |\eqnarray| to do everything for us.  This is really easy.
5415 %
5416 %    \begin{macrocode}
5417 \def\eqlines{\eqnarray[x]}
5418 \let\endeqlines\endeqnarray
5419 %    \end{macrocode}
5420 %
5421 % \end{environment}
5422 %
5423 % \begin{environment}{eqlines*}
5424 %
5425 % There's a $*$ version which omits numbers.  This is easy too.  Lots of
5426 % hacking with expansion here to try and reduce the number of tokens being
5427 % used.  Is it worth it?
5428 %
5429 %    \begin{macrocode}
5430 \expandafter\edef\csname eqlines*\endcsname{%
5431   \expandafter\noexpand\csname eqnarray*\endcsname[x]%
5432 }
5433 \expandafter\let\csname endeqlines*\expandafter\endcsname
5434                 \csname endeqnarray*\endcsname
5435 %    \end{macrocode}
5436 %
5437 % \end{environment}
5438 %
5439 %
5440 % \subsection{Split equations}
5441 %
5442 % Based on an idea from \textit{The \TeX book}, we provide some simple
5443 % environments for doing split equations.  There's plenty of scope for
5444 % improvement here, though.
5445 %
5446 % \begin{environment}{spliteqn}
5447 % \begin{environment}{spliteqn*}
5448 %
5449 % The only difference between these two is that the $*$-version doesn't put
5450 % in an equation number by default (although this behaviour can be
5451 % changed by |\eqnumber|).
5452 %
5453 % The fun here mainly concerns putting in the equation number at the right
5454 % place -- for |leqno| users, we need to put the number on the first line;
5455 % otherwise we put it on the last line.
5456 %
5457 % The way we handle this is to have two macros, |\\| (which clearly does
5458 % all the user line breaks) and |\seq@lastcr| which is used at the end of
5459 % the environment to wrap everything up.  The |\seq@eqnocr| macro puts an
5460 % equation number on the current line and then does a normal |\\|.  It also
5461 % resets |\\| and |\seq@lastcr| so that they don't try to put another
5462 % equation number in.  This must be done globally, although anyone who tries
5463 % to nest maths displays will get what they deserve.
5464 %
5465 % For the non-$*$ environment, then, we need to step the equation counter,
5466 % and set |\\| to |\seq@cr| or |\seq@eqnocr| as appropriate for the setting
5467 % of the |leqno| flag -- |\seq@lastcr| always gets set to put an equation
5468 % number in (because it will be reset if the number actually gets done
5469 % earlier -- this catches stupid users trying to put a single row into
5470 % a split environment).
5471 %
5472 %    \begin{macrocode}
5473 \def\spliteqn{%
5474   \let\eqa@oldnumber\eqa@number%
5475   \global\let\eqa@number\eqa@eqcount%
5476   \spliteqn@i%
5477 }
5478 %    \end{macrocode}
5479 %
5480 % For the $*$ variant, we don't need to bother with equation numbering, so
5481 % this is really easy.
5482 %
5483 %    \begin{macrocode}
5484 \@namedef{spliteqn*}{%
5485   \let\eqa@oldnumber\eqa@number%
5486   \gdef\eqa@number{}%
5487   \spliteqn@i%
5488 }
5489 %    \end{macrocode}
5490 %
5491 % Ending the environments is easy.  Most of the stuff here will be described
5492 % later.
5493 %
5494 %    \begin{macrocode}
5495 \def\endspliteqn{%
5496   \hfilneg\seq@lastcr%
5497   \egroup%
5498   \dsp@end%
5499   \global\let\eqa@number\eqa@oldnumber%
5500   \global\advance\c@equation\m@ne%
5501   \global\@ignoretrue%
5502 }
5503 \expandafter\let\csname endspliteqn*\endcsname\endspliteqn
5504 %    \end{macrocode}
5505 %
5506 % \end{environment}
5507 % \end{environment}
5508 %
5509 % \begin{macro}{\spliteqn@i}
5510 %
5511 % Here we handle the full display splits.  Start a maths display, and make
5512 % each row of the alignment take up the full display width.
5513 %
5514 % The macro |\seq@dosplit| does most of the real work for us -- setting up
5515 % the alignment and so forth.  The template column is interesting.  There
5516 % are two items glue on both sides of the actual text:
5517 %
5518 % \begin{itemize}
5519 %
5520 % \item Some glue which can shrink.  This keeps the display from the edges
5521 %       of the page unless we get a really wide item.
5522 %
5523 % \item An |\hfil| to do the alignment.  By default, this centres the
5524 %       equations.  On the first line, however, we put a leading |\hfilneg|
5525 %       which cancels the first |\hfil|, making the first row left aligned.
5526 %       Similarly, at the end, we put an |\hfilneg| after the last equation
5527 %       to right align the last line.
5528 %
5529 % \end{itemize}
5530 %
5531 % We pass this information on as an argument.  It's easy really.
5532 %
5533 %    \begin{macrocode}
5534 \def\spliteqn@i{%
5535 %    \end{macrocode}
5536 %
5537 % First, set up equation numbering properly.  See my rant about
5538 % |\refstepcounter| above.
5539 %
5540 %    \begin{macrocode}
5541   \stepcounter{equation}%
5542   \def\@currentlabel{\p@equation\theequation}%
5543 %    \end{macrocode}
5544 %
5545 % Right; now to sort out the numbering and newline handling.  If the number's
5546 % meant to be on the first line (for \textsf{leqno} users), then it gets
5547 % typeset on the first like; otherwise we just do a normal newline on
5548 % all lines except the first.  Once |\seq@eqnocr| has done its stuff, it
5549 % redefines all the newline handling not to insert another number.
5550 %
5551 %    \begin{macrocode}
5552   \if@leqno%
5553     \global\let\seq@docr\seq@eqnocr%
5554   \else%
5555     \global\let\seq@docr\seq@cr%
5556   \fi%
5557   \global\let\seq@lastcr\seq@eqnocr%
5558 %    \end{macrocode}
5559 %
5560 % For my next trick, I'll do some display handling -- start a (possibly
5561 % nested) maths display, set up the |\tabpause| macro appropriately, and
5562 % set the newline command to do the right thing.
5563 %
5564 %    \begin{macrocode}
5565   \dsp@start%
5566   \dsp@tabpause%
5567   \def\\{\seq@docr}%
5568 %    \end{macrocode}
5569 %
5570 % Finally, call another macro to do the remaining bits of setting up.
5571 %
5572 %    \begin{macrocode}
5573   \seq@dosplit%
5574     {\hb@xt@\displaywidth{%
5575       \hskip\splitleft\hfil$\displaystyle##$%
5576       \hfil\hskip\splitright}}%
5577     {\hfilneg}%
5578 }
5579 %    \end{macrocode}
5580 %
5581 % \end{macro}
5582 %
5583 % \begin{environment}{subsplit}
5584 %
5585 % For doing splits in the middle of equations, we provide a similar
5586 % environment.  Here, we make |\\| just start a new line.  We also use
5587 % a |\vcenter| rather than a full maths display.  The glue items are also
5588 % a bit different: we use plain double-quads on each side of the item, and
5589 % we need to remove them by hand at the extremities of the environment.
5590 %
5591 %    \begin{macrocode}
5592 \def\subsplit{\@ifnextchar[\subsplit@i{\subsplit@i[c]}}
5593 \def\subsplit@i[#1]{%
5594   \let\@tempa\vcenter%
5595   \if#1t\let\@tempa\vtop\fi%
5596   \if#1b\let\@tempa\vbox\fi%
5597   \let\\\seq@cr%
5598   \@tempa\bgroup%
5599   \seq@dosplit{\hfil\qquad$##$\qquad\hfil}{\hfilneg\hskip-2em}%
5600 }
5601 %    \end{macrocode}
5602 %
5603 % Ending the environment is fairly easy.  We remove the final glue item,
5604 % and close the alignment and the vbox.
5605 %
5606 %    \begin{macrocode}
5607 \def\endsubsplit{%
5608   \hfilneg\hskip-2em\cr%
5609   \egroup\egroup%
5610 }
5611 %    \end{macrocode}
5612 %
5613 % \end{environment}
5614 %
5615 % \begin{macro}{\seq@dosplit}
5616 %
5617 % Here we do most of the real work.  Actually, since the preamble is passed
5618 % in as an argument, most of the work is already done.  The only thing to
5619 % really note is the template for subsequent columns.  To stop users putting
5620 % in extra columns (which is where we put the equation number) we raise an
5621 % error and discard the input in a scratch box register.  This template is
5622 % repeated infinitely so as to allow us to put the equation number in nicely.
5623 % However, the final negative glue item won't work properly, so the equation
5624 % will look awful.
5625 %
5626 %    \begin{macrocode}
5627 \def\seq@dosplit#1#2{%
5628   \halign\bgroup%
5629     #1&&\mth@err@number\setbox\z@\hbox{##}\cr%
5630   #2\relax%
5631 }
5632 %    \end{macrocode}
5633 %
5634 % \end{macro}
5635 %
5636 % \begin{macro}{\seq@eqnocr}
5637 %
5638 % Here's how we set equation numbers.  Since the column provided raises
5639 % errors as soon as a token finds its way into it, we start with a |&\omit|.
5640 % Then we just put the equation number in a zero-width box.  Finally, we
5641 % reset the newline commands to avoid putting in more than one equation
5642 % number, and do normal newline things.
5643 %
5644 %    \begin{macrocode}
5645 \def\seq@eqnocr{%
5646   &\omit%
5647   \hb@xt@\z@{\hss\eqa@eqpos\eqa@number}%
5648   \global\let\seq@docr\seq@cr%
5649   \global\let\seq@lastcr\seq@cr%
5650   \seq@cr%
5651 }
5652 %    \end{macrocode}
5653 %
5654 % \end{macro}
5655 %
5656 % \begin{macro}{\seq@cr}
5657 %
5658 % Newlines are very easy.  We add a |\jot| of extra space, since this is
5659 % a nice thing to do.
5660 %
5661 %    \begin{macrocode}
5662 \def\seq@cr{\tab@cr\seq@cr@i\interdisplaylinepenalty\@M}
5663 \def\seq@cr@i#1#2{\cr\noalign{\penalty#2\vskip\jot\vskip#1}}
5664 %    \end{macrocode}
5665 %
5666 % \end{macro}
5667 %
5668 %
5669 % \subsection{Matrix handling}
5670 %
5671 % There's been a complete and total overhaul of the spacing calculations
5672 % for matrices here.  The vertical spacing now bears an uncanny similarity
5673 % to the rules \TeX\ uses to space out |\atop|-like fractions, the difference
5674 % being that you can have more than one column in a matrix.  This has the
5675 % interesting side-effect that we get an \package{amsmath}-style
5676 % sub/superscript environment almost free of charge with the matrix handling
5677 % (it just ends up being a script-size single-column matrix).
5678 %
5679 % What is rather gratifying is that our \env{matrix} environment looks
5680 % rather nicer than \package{amsmath}'s (which is based directly on
5681 % \env{array}, giving it nasty restrictions on the numbers of columns and
5682 % so on); in particular, the version here gives the `correct' result for
5683 % Knuth's exercise~18.42 (which states categorically that a |\smallskip|
5684 % should be placed between the rows of the big matrix).
5685 %
5686 % The reason the interrow space doesn't come out in the AMS version is
5687 % that \env{array} inserts extra vertical space by extending the depth of
5688 % the final row using a strut: the big matrix already extends deeper than
5689 % this, so the strut doesn't make any difference.  If the space was added
5690 % by |\hlx{s[\smallskipamount]}| instead of the |\\| command, things would
5691 % be different.
5692 %
5693 % \begin{figure}
5694 %
5695 % ^^A This is essentially what amsmath (version 1.2b) does.  The real
5696 % ^^A implementation requires a counter MaxMatrixCols, and has fewer braces:
5697 % ^^A that's all the difference.  Oh, and I turn off \arrayextrasep here,
5698 % ^^A since amsmath doesn't expect it to be there (accurate emulation, see?)
5699 % ^^A and I've used \hspace instead of \hskip since everything else is
5700 % ^^A `proper' LaTeX stuff.
5701 %
5702 % \newenvironment{ams-pmatrix}{^^A
5703 %   \setlength{\arrayextrasep}{0pt}^^A
5704 %   \left(^^A
5705 %   \hspace{-\arraycolsep}^^A
5706 %   \begin{array}{*{10}{c}}^^A
5707 % }{^^A
5708 %  \end{array}^^A
5709 %  \hspace{-\arraycolsep}^^A
5710 %  \right)^^A
5711 % }
5712 %
5713 % \begin{demo}{Exercise 18.42 from \emph{The \TeX book}}
5714 %\newcommand{\domatrix}[1]{
5715 %  \def\mat##1
5716 %    {\begin{#1}##1\end{#1}}
5717 %  \[ \begin{#1}
5718 %     \mat{a & b \\ c & d} &
5719 %     \mat{e & f \\ g & h}
5720 %     \\[\smallskipamount]
5721 %     0 &
5722 %     \mat{i & j \\ k & l}
5723 %     \end{#1}
5724 %  \]
5725 %}
5726 %\domatrix{pmatrix}
5727 %\domatrix{ams-pmatrix}
5728 % \end{demo}
5729 %
5730 % \end{figure}
5731 %
5732 % \begin{environment}{genmatrix}
5733 %
5734 % The first job is to store my maths style and font away, because I'll be
5735 % needing it lots later.
5736 %
5737 %    \begin{macrocode}
5738 \def\genmatrix#1#2#3#4#5{%
5739   \let\mat@style#1%
5740   \ifx#2\scriptstyle%
5741     \let\mat@font\scriptfont%
5742   \else\ifx#2\scriptscriptstyle%
5743     \let\mat@font\scriptscriptfont%
5744   \else%
5745     \let\mat@font\textfont%
5746   \fi\fi%
5747 %    \end{macrocode}
5748 %
5749 % Now to cope with inserted text.  This is easy.
5750 %
5751 %    \begin{macrocode}
5752   \ifx\mat@style\scriptstyle%
5753     \let\mat@textsize\scriptsize%
5754   \else\ifx\mat@style\scriptscriptstyle%
5755     \let\mat@textsize\scriptscriptsize%
5756   \else%
5757     \let\mat@textsize\relax%
5758   \fi\fi%
5759 %    \end{macrocode}
5760 %
5761 % Now for some fun.  I'll remember how to start and end the matrix in a
5762 % couple of macros |\mat@left| and |\mat@right|.  I haven't yet worked out
5763 % exactly what needs to be in |\mat@right| yet, though, so I'll build that
5764 % up in a scratch token list while I'm making my mind up.
5765 %
5766 % Initially, I want to open a group (to trap the style changes), set the
5767 % maths style (to get the right spacing), insert the left delimiter, insert
5768 % some spacing around the matrix, and start a centred box.  The ending just
5769 % closes all the groups and delimiters I opened.
5770 %
5771 %    \begin{macrocode}
5772   \def\mat@left{\bgroup\mat@style\left#4#3\vcenter\bgroup}%
5773   \toks@{\egroup#3\right#5\egroup}%
5774 %    \end{macrocode}
5775 %
5776 % Now comes a slightly trickier bit.  If the maths style is script or
5777 % scriptscript, then I need to raise the box by a little bit to make it look
5778 % really good.  The right amount is somewhere around \smallf 3/4\,pt, I
5779 % think, so that's what I'll use.
5780 %
5781 %    \begin{macrocode}
5782   \@tempswatrue%
5783   \ifx\mat@style\displaystyle\else\ifx\mat@style\textstyle\else%
5784     \@tempswafalse%
5785     \setbox\z@\hbox\bgroup$%
5786     \toks@\expandafter{\the\toks@$\m@th\egroup\raise.75\p@\box\z@}%
5787   \fi\fi%
5788 %    \end{macrocode}
5789 %
5790 % If I'm not in maths mode right now, then I should enter maths mode, and
5791 % remember to leave it later.
5792 %
5793 %    \begin{macrocode}
5794   \if@tempswa\ifmmode\else%
5795     $\m@th%
5796     \toks@\expandafter{\the\toks@$}%
5797   \fi\fi%
5798 %    \end{macrocode}
5799 %
5800 % Now I've sorted out how to end the environment properly, so I can set up
5801 % the macro, using |\edef|.
5802 %
5803 %    \begin{macrocode}
5804   \edef\mat@right{\the\toks@}%
5805 %    \end{macrocode}
5806 %
5807 % Now see if there's an optional argument.  If not, create lots of centred
5808 % columns.
5809 %
5810 %    \begin{macrocode}
5811   \@testopt\genmatrix@i{[c}%
5812 }
5813 %    \end{macrocode}
5814 %
5815 % Now to sort out everything else.
5816 %
5817 %    \begin{macrocode}
5818 \def\genmatrix@i[#1]{%
5819 %    \end{macrocode}
5820 %
5821 % Some initial setting up: choose the correct column set, and set up some
5822 % variables for reading the preamble.
5823 %
5824 %    \begin{macrocode}
5825   \colset{matrix}%
5826   \tab@initread%
5827 %    \end{macrocode}
5828 %
5829 % Now comes some of the tricky stuff.  The space between columns should be
5830 % 12\,mu (by trial and error).  We put the space in a box so we can measure
5831 % it in the correct mathstyle.
5832 %
5833 %    \begin{macrocode}
5834   \setbox\z@\hbox{$\mat@style\mskip12mu$}%
5835   \edef\tab@tabtext{&\kern\the\wd\z@}%
5836   \tab@readpreamble{#1}%
5837 %    \end{macrocode}
5838 %
5839 % Now we need to decide how to space out the rows.  The code here is based
5840 % on the information in appendix~G of \emph{The \TeX book}: I think it'd be
5841 % nice if my matrices were spaced out in the same way as normal fractions
5842 % (particularly |\choose|y things).  The standard |\baselineskip| and
5843 % |\lineskip| parameters come in really handy here.
5844 %
5845 % The parameters vary according to the size of the text, so I need to see
5846 % if we have scriptsize or less, or not.  The tricky |\if| sorts this out.
5847 %
5848 %    \begin{macrocode}
5849   \if1\ifx\mat@style\scriptstyle1\else%
5850       \ifx\mat@style\scriptscriptstyle1\else0\fi\fi%
5851     \baselineskip\fontdimen10\mat@font\tw@%
5852     \advance\baselineskip\fontdimen12\mat@font\tw@%
5853     \lineskip\thr@@\fontdimen8\mat@font\thr@@%
5854   \else%
5855     \baselineskip\fontdimen8\mat@font\tw@%
5856     \advance\baselineskip\fontdimen11\mat@font\tw@%
5857     \lineskip7\fontdimen8\mat@font\thr@@%
5858   \fi%
5859   \lineskiplimit\lineskip%
5860 %    \end{macrocode}
5861 %
5862 % Now actually set up for the alignment.  Assign |\\| to the correct value.
5863 % Set up the |\tabskip|.  Do the appropriate |\mat@left| thing set up above.
5864 % And then start the alignment.
5865 %
5866 %    \begin{macrocode}
5867   \let\\\mat@cr%
5868   \tabskip\z@skip%
5869   \col@sep\z@%
5870   \mat@left%
5871   \halign\expandafter\bgroup\the\tab@preamble\tabskip\z@skip\cr%
5872 %    \end{macrocode}
5873 %
5874 % Now for a little hack to make the spacing consistent between matrices of
5875 % the same height.  This comes directly from \PlainTeX.  This appears to
5876 % make the spacing \emph{exactly} the same as the \TeX\ primitives, oddly
5877 % enough.
5878 %
5879 %    \begin{macrocode}
5880   \ifx\mat@font\textfont%
5881     \omit$\mat@style\mathstrut$\cr\noalign{\kern-\baselineskip}%
5882   \fi%
5883 }
5884 %    \end{macrocode}
5885 %
5886 % Finishing the environment is really easy.  We do the spacing hack again
5887 % at the bottom, close the alignment and then tidy whatever we started in
5888 % |\mat@left|.
5889 %
5890 %    \begin{macrocode}
5891 \def\endgenmatrix{%
5892   \crcr%
5893   \ifx\mat@font\textfont%
5894     \omit$\mat@style\mathstrut$\cr\noalign{\kern-\baselineskip}%
5895   \fi%
5896   \egroup%
5897   \mat@right%
5898 }
5899 %    \end{macrocode}
5900 %
5901 % \end{environment}
5902 %
5903 % \begin{macro}{\mat@cr}
5904 %
5905 % Newlines are really easy.  The $*$-form means nothing here, so we ignore
5906 % it.
5907 %
5908 %    \begin{macrocode}
5909 \def\mat@cr{\tab@cr\mat@cr@i{}{}}
5910 \def\mat@cr@i#1{\cr\noalign{\vskip#1}\@gobble}
5911 %    \end{macrocode}
5912 %
5913 % \end{macro}
5914 %
5915 % \begin{macro}{\newmatrix}
5916 %
5917 % This is how we define new matrix environments.  It's simple fun with
5918 % |\csname| and |\expandafter|.
5919 %
5920 %    \begin{macrocode}
5921 \def\newmatrix#1#2{%
5922   \@namedef{#1}{\genmatrix#2}%
5923   \expandafter\let\csname end#1\endcsname\endgenmatrix%
5924 }
5925 %    \end{macrocode}
5926 %
5927 % \end{macro}
5928 %
5929 % \begin{environment}{matrix}
5930 % \begin{environment}{pmatrix}
5931 % \begin{environment}{dmatrix}
5932 % \begin{environment}{smatrix}
5933 % \begin{environment}{spmatrix}
5934 % \begin{environment}{sdmatrix}
5935 % \begin{environment}{smatrix*}
5936 % \begin{environment}{spmatrix*}
5937 % \begin{environment}{sdmatrix*}
5938 %
5939 % Now we define all the other environments we promised.  This is easy.
5940 %
5941 %    \begin{macrocode}
5942 \newmatrix{matrix}{{\textstyle}{\textstyle}{\,}{.}{.}}
5943 \newmatrix{pmatrix}{{\textstyle}{\textstyle}{\,}{(}{)}}
5944 \newmatrix{dmatrix}{{\textstyle}{\textstyle}{\,}}
5945 \newmatrix{smatrix}{{\scriptstyle}{\scriptstyle}{}{.}{.}}
5946 \newmatrix{spmatrix}{{\scriptstyle}{\scriptstyle}{}{(}{)}}
5947 \newmatrix{sdmatrix}{{\scriptstyle}{\scriptstyle}{}}
5948 \newmatrix{smatrix*}{{\scriptstyle}{\textstyle}{}{.}{.}}
5949 \newmatrix{spmatrix*}{{\scriptstyle}{\textstyle}{}{(}{)}}
5950 \newmatrix{sdmatrix*}{{\scriptstyle}{\textstyle}{}}
5951 %    \end{macrocode}
5952 %
5953 % \end{environment}
5954 % \end{environment}
5955 % \end{environment}
5956 % \end{environment}
5957 % \end{environment}
5958 % \end{environment}
5959 % \end{environment}
5960 % \end{environment}
5961 % \end{environment}
5962 %
5963 % \begin{environment}{script}
5964 %
5965 % Now for superscripts and subscripts.  This is fairly easy, because I
5966 % took so much care over the matrix handling.
5967 %
5968 %    \begin{macrocode}
5969 \def\script{%
5970   \let\mat@style\scriptstyle%
5971   \def\mat@left{\vcenter\bgroup}%
5972   \def\mat@right{\egroup}%
5973   \let\mat@font\scriptfont%
5974   \let\mat@textsize\scriptsize%
5975   \@testopt\genmatrix@i c%
5976 }
5977 \let\endscript\endgenmatrix
5978 %    \end{macrocode}
5979 %
5980 % \end{environment}
5981 %
5982 % Now define the column types.
5983 %
5984 %    \begin{macrocode}
5985 \colpush{matrix}
5986 \coldef l{\tabcoltype{\kern\z@$\mat@style}{\m@th$\hfil}}
5987 \coldef c{\tabcoltype{\hfil$\mat@style}{\m@th$\hfil}}
5988 \coldef r{\tabcoltype{\hfil$\mat@style}{\m@th$}}
5989 \coldef T#1{\tab@aligncol{#1}{\begingroup\mat@textsize}{\endgroup}}
5990 \collet > [tabular]>
5991 \collet < [tabular]<
5992 \collet * [tabular]*
5993 \collet @ [tabular]@
5994 %    \end{macrocode}
5995 %
5996 % The repeating type is more awkward.  Things will go wrong if this is
5997 % given before the first column, so we must do a whole repeat by hand.  We
5998 % can tell if we haven't contributed a column yet, since |\tab@column| will
5999 % be zero.  Otherwise, we fiddle the parser state to start a new column, and
6000 % insert the |&| character to make \TeX\ repeat the preamble.
6001 %
6002 %    \begin{macrocode}
6003 \coldef {[}{%
6004   \@firstoftwo{%
6005     \ifnum\tab@columns=\z@%
6006       \def\@tempa##1\q@delim{%
6007         \tab@mkpreamble##1[##1\q@delim%
6008       }%
6009       \expandafter\@tempa%
6010     \else%
6011       \tab@setstate\tab@prestate%
6012       \tab@append\tab@preamble{&}%
6013       \expandafter\tab@mkpreamble%
6014     \fi%
6015   }%
6016 }
6017 %    \end{macrocode}
6018 %
6019 % We're done defining columns now.
6020 %
6021 %    \begin{macrocode}
6022 \colpop
6023 %    \end{macrocode}
6024 %
6025 %
6026 % \subsection{Dots\dots}
6027 %
6028 % Nothing whatsoever to do with alignments, although vertical and diagonal
6029 % dots in small matrices look really silly.  The following hacky definitions
6030 % work rather better.
6031 %
6032 % \begin{macro}{\mdw@dots}
6033 %
6034 % First of all, here's some definitions common to both of the dots macros.
6035 % The macro takes as an argument the actual code to draw the dots, passing
6036 % it the scaled size of a point in the scratch register |\dimen@|; the
6037 % register |\box 0| is set to contain a dot of the appropriate size.
6038 %
6039 %    \begin{macrocode}
6040 \def\mdw@dots#1{\ensuremath{\mathpalette\mdw@dots@i{#1}}}
6041 \def\mdw@dots@i#1#2{%
6042   \setbox\z@\hbox{$#1\mskip1.8mu$}%
6043   \dimen@\wd\z@%
6044   \setbox\z@\hbox{$#1.$}%
6045   #2%
6046 }
6047 %    \end{macrocode}
6048 %
6049 % \end{macro}
6050 %
6051 % \begin{macro}{\vdots}
6052 %
6053 % I'll start with the easy one.  This is a simple translation of the original
6054 % implementation.
6055 %
6056 %    \begin{macrocode}
6057 \def\vdots{%
6058   \mdw@dots{\vbox{%
6059     \baselineskip4\dimen@%
6060     \lineskiplimit\z@%
6061     \kern6\dimen@%
6062     \copy\z@\copy\z@\box\z@%
6063   }}%
6064 }
6065 %    \end{macrocode}
6066 %
6067 % \end{macro}
6068 %
6069 % \begin{macro}{\ddots}
6070 %
6071 % And I'll end with the other easy one\dots
6072 %
6073 %    \begin{macrocode}
6074 \def\ddots{%
6075   \mdw@dots{\mathinner{%
6076     \mkern1mu%
6077     \raise7\dimen@\vbox{\kern7\dimen@\copy\z@}%
6078     \mkern2mu%
6079     \raise4\dimen@\copy\z@%
6080     \mkern2mu%
6081     \raise\dimen@\box\z@%
6082     \mkern1mu%
6083   }}%
6084 }
6085 %    \end{macrocode}
6086 %
6087 % \end{macro}
6088 %
6089 %
6090 % \subsection{Lucky dip}
6091 %
6092 % Time to round off with some trivial environments, just to show how easy
6093 % this stuff is.
6094 %
6095 % \begin{environment}{cases}
6096 % \begin{environment}{smcases}
6097 %
6098 % These are totally and utterly trivial.
6099 %
6100 %    \begin{macrocode}
6101 \def\cases{\left\{\,\array{@{}lTl@{}}}
6102 \def\endcases{\endarray\,\right.}
6103 \def\smcases{\left\{\smarray{@{}lTl@{}}}
6104 \def\endsmcases{\endsmarray\,\right.}
6105 %    \end{macrocode}
6106 %
6107 % \end{environment}
6108 % \end{environment}
6109 %
6110 % \subsection{Error messages}
6111 %
6112 % Some token saving:
6113 %
6114 %    \begin{macrocode}
6115 \def\mth@error{\PackageError{mathenv}}
6116 %    \end{macrocode}
6117 %
6118 % Now for the error messages.
6119 %
6120 %    \begin{macrocode}
6121 \def\mth@err@number{%
6122   \mth@error{Too many `&' characters found}{%
6123     You've put too many `&' characters in an alignment^^J%
6124     environment (like `eqnarray' or `spliteqn') and wandered^^J%
6125     into trouble.  I've gobbled the contents of that column^^J%
6126     and hopefully I can recover fairly easily.%
6127   }%
6128 }
6129 %    \end{macrocode}
6130 %
6131 %    \begin{macrocode}
6132 \def\mth@err@mdsp{%
6133   \mth@error{Can't do displays in nondisplay maths mode}{%
6134     You're trying to start a display environment, but you're^^J%
6135     in nondisplay maths mode.  The display will appear but^^J%
6136     don't blame me when it looks horrible.%
6137   }%
6138 }
6139 %    \end{macrocode}
6140 %
6141 %    \begin{macrocode}
6142 \def\mth@err@hdsp{%
6143   \mth@error{Can't do displays in LR mode}{%
6144     You're trying to start a display environment, but you're^^J%
6145     in LR (restricted horizontal) mode.  Everything will go^^J%
6146     totally wrong, so your best bet is to type `X', fix the^^J%
6147     mistake and start again.%
6148   }%
6149 }
6150 %    \end{macrocode}
6151 %
6152 % \vskip\parskip\vbox{ ^^A The best way I could find of keeping this lot
6153 %                      ^^A together, I'm afraid.
6154 % That's all there is.  Byebye.
6155 %
6156 %    \begin{macrocode}
6157 %</mathenv>
6158 %    \end{macrocode}
6159 % \nopagebreak
6160 %
6161 % \hfill Mark Wooding, \today
6162 % }
6163 %
6164 % \Finale
6165 %
6166 \endinput