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