infra: Expunge revision history clutter.
[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%
b57a27e1 47% \CheckSum{3373}
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
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
654% looks very odd.
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).
893%
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> "}"
1085% \[ "[" <left-overhang> "]"
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}
1949%
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.
1956%
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%
2717 \else\expandafter\tabusrposttype\fi%
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}%
2858 \def\tab@midtext{\ignorespaces\@sharp\@sharp\@maybe@unskip}%
2859 \tab@readpreamble{#1}%
2860 \gdef\@preamble{\the\tab@preamble}%
2861 \let\tab@bgroup\begingroup%
2862 \let\tab@egroup\endgroup%
2863 \begingroup%
2864}
2865% \end{macrocode}
2866%
86f6a31e 2867% \subsection{Debugging}
2868%
2869% This macro just parses a preamble and displays it on the terminal. It
2870% means I can see whether the thing's working.
2871%
2872% \begin{macrocode}
2873\def\showpream#1{%
2874 \tab@initread%
2875 \tab@readpreamble{#1}%
2876 \showthe\tab@preamble%
2877 \showthe\tab@shortline%
2878}
2879% \end{macrocode}
2880%
2881% A quick macro for showing column types.
2882%
2883% \begin{macrocode}
2884\def\showcol#1{%
2885 \expandafter\show\csname\tab@colset!col.\string#1\endcsname%
2886}
2887% \end{macrocode}
2888%
2889%
2890% \subsection{The \env{tabular} and \env{array} environments}
2891%
2892% This is where we define the actual environments which users play with.
2893%
2894% \subsubsection{The environment routines}
2895%
2896% The real work is done in the |\@array| macro later. We just set up lots
2897% (and I mean \emph{lots}) of parameters first, and then call |\@array|.
2898%
2899% \begin{macro}{\tab@array}
2900%
2901% The |\tab@array| macro does most of the common array things.
2902%
2903% \begin{macrocode}
2904\def\tab@array{%
2905 \tab@width\z@%
2906 \let\tab@bgroup\tab@bmaths%
2907 \let\tab@egroup\tab@emaths%
2908 \@tabarray%
2909}
2910% \end{macrocode}
2911%
2912% \end{macro}
2913%
2914% \begin{macro}{\tab@btext}
2915% \begin{macro}{\tab@bmaths}
2916% \begin{macro}{\tab@etext}
2917% \begin{macro}{\tab@emaths}
2918%
2919% These macros contain appropriate things to use when typesetting
2920% text or maths macros. They're all trivial. They're here only for
2921% later modification by funny things like the \env{smarray} environment.
2922%
2923% \begin{macrocode}
2924\def\tab@btext{\begingroup}
7abe48b3 2925\def\tab@bmaths{\color@begingroup$}
86f6a31e 2926\def\tab@etext{\endgroup}
7abe48b3 2927\def\tab@emaths{\m@th$\color@endgroup}
86f6a31e 2928% \end{macrocode}
2929%
2930% \end{macro}
2931% \end{macro}
2932% \end{macro}
2933% \end{macro}
2934%
2935% \begin{environment}{array}
2936%
2937% Now for the \env{array} environment. The `|$|' signs act as a group, so we
2938% don't need to do extra grouping this time. Closing the environment is
2939% easy.
2940%
2941% \begin{macrocode}
2942\def\array{%
2943 \col@sep\arraycolsep%
2944 \let\tab@extrasep\arrayextrasep%
2945 \tab@normalstrut%
2946 \tab@array%
2947}
2948\def\endarray{%
2949 \crcr%
2950 \egroup%
2951 \tab@right%
7abe48b3 2952 \endgroup%
86f6a31e 2953 \tab@restorehlstate%
7abe48b3 2954 \global\c@tabrow\count@%
2955 \def\@currentlabel{\p@tabrow\thetabrow}%
2956 \tab@endhook%
86f6a31e 2957}
2958% \end{macrocode}
2959%
2960% \end{environment}
2961%
2962% \begin{environment}{smarray}
2963%
2964% Now for something a little different. The \env{smarray} environment
2965% gives you an array with lots of small text.
2966%
2967% \begin{macrocode}
2968\def\smarray{%
2969 \extrarowheight\z@%
2970 \col@sep\smarraycolsep%
2971 \let\tab@extrasep\smarrayextrasep%
7abe48b3 2972 \def\tab@bmaths{$\color@begingroup\scriptstyle}%
86f6a31e 2973 \def\tab@btext{\begingroup\scriptsize}%
2974 \setbox\z@\hbox{\scriptsize\strut}%
2975 \dimen@\ht\z@\dimen@ii\dp\z@\tab@setstrut%
2976 \tab@array%
2977}
2978\let\endsmarray\endarray
2979% \end{macrocode}
2980%
2981% \end{environment}
2982%
2983% \begin{macro}{\tabstyle}
2984%
2985% This is a little hook that document designers can use to modify the
2986% appearance of tables throughout a document. For example, I've set it to
2987% make the text size |\small| in all tables in this document. Macro writers
2988% shouldn't try to use it as a hook for their own evilness, though. I've
2989% used |\providecommand| to avoid nobbling an existing definition.
2990%
2991% \begin{macrocode}
2992\providecommand\tabstyle{}
2993% \end{macrocode}
2994%
2995% \end{macro}
2996%
2997% \begin{macro}{\@tabular}
2998%
2999% The two \env{tabular} environments share lots of common code, so we
3000% separate that out. (This needs to be done better.) All we really do here
3001% is set up the |\tab@bgroup| and |\tab@egroup| to localise things properly,
3002% and then go.
3003%
3004% \begin{macrocode}
3005\def\@tabular#1{%
3006 \tabstyle%
3007 \setlength\tab@width{#1}%
3008 \let\tab@bgroup\tab@btext%
3009 \let\tab@egroup\tab@etext%
3010 \col@sep\tabcolsep%
3011 \let\tab@extrasep\tabextrasep%
3012 \tab@normalstrut%
3013 \@tabarray%
3014}
3015% \end{macrocode}
3016%
3017% \end{macro}
3018%
3019% \begin{environment}{tabular}
3020% \begin{environment}{tabular*}
3021%
3022% These environments just call a macro which does all the common stuff.
3023%
3024% \begin{macrocode}
3025\def\tabular{\@tabular\z@}
3026\expandafter\let\csname tabular*\endcsname\@tabular
3027\let\endtabular\endarray
3028\expandafter\let\csname endtabular*\endcsname\endarray
3029% \end{macrocode}
3030%
3031% \end{environment}
3032% \end{environment}
3033%
3034% \subsubsection{Setting the strut height}
3035%
3036% \begin{macro}{\tab@setstrut}
3037%
3038% We use a magical strut, called |\@arstrut|, which keeps the table from
3039% collapsing around our heads. This is where we set it up.
3040%
3041% It bases the array strut size on the given values of |\dimen@| and
3042% |\dimen@ii|, amended by various appropriate fiddle values added in by
3043% various people.
3044%
3045% \begin{macrocode}
3046\def\tab@setstrut{%
3047 \setbox\@arstrutbox\hbox{%
3048 \vrule%
3049 \@height\arraystretch\dimen@%
3050 \@depth\arraystretch\dimen@ii%
3051 \@width\z@%
3052 }%
3053}
3054% \end{macrocode}
3055%
3056% \end{macro}
3057%
3058% \begin{macro}{\tab@normalstrut}
3059%
3060% This sets the strut the normal way, from the size of |\strutbox|.
3061%
3062% \begin{macrocode}
3063\def\tab@normalstrut{%
3064 \dimen@\ht\strutbox\advance\dimen@\extrarowheight%
3065 \dimen\tw@\dp\strutbox%
3066 \tab@setstrut%
3067}
3068% \end{macrocode}
3069%
3070% \end{macro}
3071%
3072% \subsubsection{Setting up the alignment}
3073%
3074% The following bits are mainly for other packages to hook themselves onto.
3075%
3076% \begin{macrocode}
7abe48b3 3077\let\@arrayleft\relax
3078\let\@arrayright\relax
3079\let\tab@beginhook\@empty
3080\let\tab@lefttexthook\@empty
3081\let\tab@righttexthook\@empty
3082\let\tab@leftruletexthook\@empty
3083\let\tab@rightruletexthook\@empty
3084\let\tab@endhook\@empty
3085% \end{macrocode}
3086%
3087% For setting these hooks, we provide some handy commands.
3088%
3089% \begin{macrocode}
3090\def\tab@addhookbefore#1#2{%
3091 \toks@{#2}\toks@\expandafter{\the\expandafter\toks@#1}%
3092 \edef#1{\the\toks@}%
3093}
3094\def\tab@addhookafter#1#2{%
3095 \toks@\expandafter{#1#2}%
3096 \edef#1{\the\toks@}%
3097}
86f6a31e 3098% \end{macrocode}
3099%
7abe48b3 3100% And now we get on with the real thing.
3101%
86f6a31e 3102% \begin{macrocode}
3103\def\@tabarray{%
3104 \let\@arrayleft\relax%
3105 \let\@arrayright\relax%
7abe48b3 3106 \@testopt\@array c%
86f6a31e 3107}
3108% \end{macrocode}
3109%
3110% \begin{macro}{\@array}
3111%
3112% The |\@array| macro does most of the real work for the environments. The
3113% first job is to set up the row strut, which keeps the table rows at the
3114% right height. We just take the normal strut box, and extend its height by
3115% the |\extrarowheight| length parameter.
3116%
3117% \begin{macrocode}
3118\def\@array[#1]#2{%
3119% \end{macrocode}
3120%
3121% Sort out the hline state variable. We'll store the old value in a
3122% control sequence to avoid wasting any more count registers.
3123%
3124% \begin{macrocode}
7abe48b3 3125 \tab@beginhook%
3126 \count@\c@tabrow%
3127 \global\c@tabrow\z@%
86f6a31e 3128 \edef\tab@restorehlstate{%
3129 \global\tab@endheight\the\tab@endheight%
3130 \gdef\noexpand\tab@hlstate{\tab@hlstate}%
3131 }%
7abe48b3 3132 \begingroup%
86f6a31e 3133 \def\tab@hlstate{n}%
3134% \end{macrocode}
3135%
3136% Now we read the preamble. All the clever things we've already done are
3137% terribly useful here.
3138%
3139% The |\tab@setcr| sets up |\\| to be a newline even if users have changed it
3140% using something like |\raggedright|.
3141%
3142% \begin{macrocode}
3143 \colset{tabular}%
3144 \tab@initread%
7abe48b3 3145 \let\tab@lefttext\tab@lefttexthook%
3146 \let\tab@righttext\tab@righttexthook%
3147 \let\tab@leftruletext\tab@leftruletexthook%
3148 \let\tab@rightruletext\tab@rightruletexthook%
86f6a31e 3149 \def\tab@midtext{\tab@setcr\ignorespaces####\@maybe@unskip}%
3150 \def\tab@multicol{\@arstrut\tab@startrow}%
3151 \tab@preamble{\tab@multicol\tabskip\z@skip}%
3152 \tab@readpreamble{#2}%
3153% \end{macrocode}
3154%
3155% Set up the default tabskip glue. This is easy: there isn't any.
3156%
3157% \begin{macrocode}
3158 \tab@leftskip\z@skip%
3159 \tab@rightskip\z@skip%
3160% \end{macrocode}
3161%
3162% Now set up the positioning of the table. This is put into a separate macro
3163% because it's rather complicated.
3164%
3165% \begin{macrocode}
3166 \tab@setposn{#1}%
3167% \end{macrocode}
3168%
3169% Now work out how to start the alignment.
3170%
3171% \begin{macrocode}
3172 \ifdim\tab@width=\z@%
3173 \def\tab@halign{}%
3174 \else%
3175 \def\tab@halign{to\tab@width}%
3176 \fi%
3177% \end{macrocode}
3178%
3179% Finally, do all the normal things we need to do before an alignment. Note
3180% that we define |\tabularnewline| first, then set |\\| from that (using
3181% |\tab@setcr|). Since |\\| is reset in the |\tab@midtext| of every table
3182% cell, it becomes secondary to |\tabularnewline|. Doing things this way
3183% avoids the problems with declarations like |\raggedright| which redefine
3184% |\\| in their own (usually rather strange) way, so you don't need to mess
3185% about with things like the |\PreserveBackslash| command given in the
3186% \textit{\LaTeX\ Companion}.
3187%
3188% \begin{macrocode}
3189 \lineskip\z@\baselineskip\z@%
3190 \m@th%
3191 \def\tabularnewline{\tab@arraycr\tab@penalty}%
3192 \tab@setcr%
3193 \let\par\@empty%
3194 \everycr{}\tabskip\tab@leftskip%
3195 \tab@left\halign\tab@halign\expandafter\bgroup%
3196 \the\tab@preamble\tabskip\tab@rightskip\cr%
3197}
3198% \end{macrocode}
3199%
3200% \end{macro}
3201%
3202% You've no doubt noticed the |\tab@left| and |\tab@right| macros above.
3203% These are set up here and elsewhere to allow other things to gain control
3204% at various points of the table (they include and take the place of the
3205% |\@arrayleft| and |\@arrayright| hooks in \package{array}, put in for
3206% \package{delarray}'s use.
3207%
3208% \subsubsection{Positioning the table}
3209%
3210% \begin{macro}{\tab@setposn}
3211%
3212% This macro sets everything up for the table's positioning. It's rather
3213% long, but not all that complicated. Honest.
3214%
3215% First, we set up some defaults (for centring). If anything goes wrong, we
3216% just do the centring things.
3217%
3218% \begin{macrocode}
3219\def\tab@setposn#1{%
3220 \def\tab@left{%
3221 \savenotes%
3222 \leavevmode\hbox\bgroup$\@arrayleft\vcenter\bgroup%
3223 }%
3224 \def\tab@right{%
3225 \egroup%
3226 \m@th\@arrayright$\egroup%
3227 \spewnotes%
3228 }%
3229 \global\tab@endheight\z@%
3230% \end{macrocode}
3231%
3232% For the standard positioning things, we just do appropriate boxing things.
3233% Note that the dollar signs are important, since \package{delarray} might
3234% want to put its delimiters in here.
3235%
3236% The |\if@tempswa| switch it used to decide if we're doing an unboxed
3237% tabular. We'll set it if we find an unbox-type position code, and then
3238% check that everything's OK for this.
3239%
3240% \begin{macrocode}
3241 \@tempswafalse%
3242 \let\tab@penalty\relax%
3243 \if#1t%
3244 \def\tab@left{%
3245 \savenotes%
3246 \leavevmode\setbox\z@\hbox\bgroup$\@arrayleft\vtop\bgroup%
3247 }%
3248 \def\tab@right{%
3249 \egroup%
3250 \m@th\@arrayright$\egroup%
3251 \tab@raisebase%
3252 \spewnotes%
3253 }%
3254 \gdef\tab@hlstate{t}%
3255 \global\tab@endheight\ht\@arstrutbox%
3256 \else\if#1b%
3257 \def\tab@left{%
3258 \savenotes%
3259 \leavevmode\setbox\z@\hbox\bgroup$\@arrayleft\vbox\bgroup%
3260 }%
3261 \def\tab@right{%
3262 \egroup%
3263 \m@th\@arrayright$\egroup%
3264 \tab@lowerbase%
3265 \spewnotes%
3266 }%
3267 \gdef\tab@hlstate{b}%
3268 \else%
3269 \if#1L\@tempswatrue\fi%
3270 \if#1C\@tempswatrue\fi%
3271 \if#1R\@tempswatrue\fi%
3272 \fi\fi%
3273% \end{macrocode}
3274%
3275% Now for some tests to make sure we're allowed to do the unboxing. We text
3276% for |\@arrayleft| being defined, because people trying to hook us won't
3277% understand unboxed tabulars.
3278%
3279% \begin{macrocode}
3280 \if@tempswa\ifhmode%
3281 \ifinner\tab@err@unbrh\@tempswafalse\else\par\fi%
3282 \fi\fi%
3283 \if@tempswa\ifmmode\tab@err@unbmm\@tempswafalse\fi\fi%
3284 \if@tempswa\ifx\@arrayleft\relax\else%
3285 \tab@err@unbext\@tempswafalse%
3286 \fi\fi%
3287% \end{macrocode}
3288%
3289% Finally, if we're still doing an unboxed alignment, we need to sort out the
3290% spacing. We know that no-one's tried to hook on to the environment, so we
3291% clear |\tab@left| and |\tab@right|.
3292%
3293% \begin{macrocode}
3294 \if@tempswa%
3295 \def\tab@left{\vskip\parskip\medskip}%
3296 \def\tab@right{\par\@endpetrue\global\@ignoretrue}%
3297% \end{macrocode}
3298%
3299% Now we need to sort out the alignment. The only way we can do this is by
3300% playing with tabskip glue. There are two possibilities:
3301%
3302% \begin{itemize}
3303%
3304% \item If this is a straight \env{tabular} or an \env{array}, we just use
3305% infinite glue. This is reasonable, I think.
3306%
3307% \item If we have a width for the table, we calculate the fixed values of
3308% glue on either side. This is fairly easy, and forces the table to
3309% the required width.
3310%
3311% \end{itemize}
3312%
3313% First, set up the left and right glues to represent the prevailing
3314% margins set up by \env{list} environments. I think this is the right
3315% thing to do.
3316%
3317% \begin{macrocode}
3318 \tab@leftskip\@totalleftmargin%
3319 \tab@rightskip\hsize%
3320 \advance\tab@rightskip-\linewidth%
3321 \advance\tab@rightskip-\@totalleftmargin%
3322% \end{macrocode}
3323%
3324% First of all, deal with the simple case. I'm using 10000\,fill glue here,
3325% in an attempt to suppress |\extracolsep| glue from making the table the
3326% wrong width. It can always use filll glue if it really needs to, though.
3327%
3328% \begin{macrocode}
3329 \ifdim\tab@width=\z@%
3330 \if#1L\else\advance\tab@leftskip\z@\@plus10000fill\fi%
3331 \if#1R\else\advance\tab@rightskip\z@\@plus10000fill\fi%
3332% \end{macrocode}
3333%
3334% Now for the fun bit. This isn't too hard really. The extra space I must
3335% add around the table adds up to $|\linewidth| - |\tab@width|$. I just
3336% need to add this onto the appropriate sides of the table.
3337%
3338% \begin{macrocode}
3339 \else%
3340 \dimen@\linewidth%
3341 \advance\dimen@-\tab@width%
3342 \if#1L\advance\tab@rightskip\dimen@\fi%
3343 \if#1R\advance\tab@leftskip\dimen@\fi%
3344 \if#1C%
3345 \advance\tab@leftskip.5\dimen@%
3346 \advance\tab@rightskip.5\dimen@%
3347 \fi%
3348 \fi%
3349% \end{macrocode}
3350%
3351% Don't allow page breaks. David Carlisle's wonderful \env{longtable}
3352% package does page breaks far better than I could possibly do here, and
3353% we're compatible with it (wahey!).
3354%
3355% \begin{macrocode}
3356 \def\tab@penalty{\penalty\@M}%
3357% \end{macrocode}
3358%
3359% Finally, set the new width of the table, and leave.
3360%
3361% \begin{macrocode}
3362 \tab@width\hsize%
3363 \fi%
3364}
3365% \end{macrocode}
3366%
3367% \end{macro}
3368%
3369% \subsubsection{Handling tops and bottoms}
3370%
3371% This is how the tops and bottoms of tables are made to line up with the
3372% text on the same line, in the presence of arbitrary rules and space. The
3373% old method, based on the way the \package{array} package worked, wasn't
3374% terribly good. This new version copes much better with almost anything
3375% that gets thrown at it.
3376%
3377% I'll keep a state in a macro (|\tab@hlstate|), which tells me what I'm
3378% meant to be doing. The possible values are \lit{n}, which means I don't
3379% have to do anything, \lit{t}, which means that I'm meant to be handling
3380% top-aligned tables, and \lit{b}, which means that I'm meant to be lining
3381% up the bottom. There are several other `substates' which have various
3382% magic meanings.
3383%
3384% \begin{macrocode}
3385\def\tab@hlstate{n}
3386% \end{macrocode}
3387%
3388% When all's said and done, I extract the box containing the table, and
3389% play with the height and depth to try and make it correct.
3390%
3391% \begin{macro}{\tab@addruleheight}
3392%
3393% This macro is called by `inter-row' things to add their height to our
3394% dimen register.
3395%
3396% Only do this if the state indicates that it's sensible.
3397%
3398% \begin{macrocode}
3399\def\tab@addruleheight#1{%
3400 \if\tab@hlstate n\else%
3401 \global\advance\tab@endheight#1\relax%
3402 \fi%
3403}
3404% \end{macrocode}
3405%
3406% \end{macro}
3407%
3408% \begin{macro}{\tab@startrow}
3409%
3410% This is called at the start of a row, from within the array preamble.
3411% Currently, this assumes that the rows aren't bigger than their struts:
3412% this is reasonable, although slightly limiting, and it could be done better
3413% if I was willing to rip the alignment apart and put it back together
3414% again.
3415%
3416% \begin{macrocode}
3417\def\tab@startrow{%
3418 \if\tab@hlstate t%
3419 \gdef\tab@hlstate{n}%
3420 \else\if\tab@hlstate b%
3421 \global\tab@endheight\dp\@arstrutbox%
3422 \fi\fi%
3423}
3424% \end{macrocode}
3425%
3426% \end{macro}
3427%
3428% \begin{macro}{\tab@raisebase}
3429%
3430% This macro is called at the end of it all, to set the height and depth
3431% of the box correctly. It sets the height to |\tab@endheight|, and the
3432% depth to everything else. The box is in |\box|~0 currently.
3433%
3434% \begin{macrocode}
3435\def\tab@raisebase{%
3436 \global\advance\tab@endheight-\ht\z@%
3437 \raise\tab@endheight\box\z@%
3438}
3439% \end{macrocode}
3440%
3441% \end{macro}
3442%
3443% \begin{macro}{\tab@lowerbase}
3444%
3445% And, for symmetry's sake, here's how to set the bottom properly instead.
3446%
3447% \begin{macrocode}
3448\def\tab@lowerbase{%
3449 \global\advance\tab@endheight-\dp\z@%
3450 \lower\tab@endheight\box\z@%
3451}
3452% \end{macrocode}
3453%
3454% \end{macro}
3455%
3456%
3457% \subsection{Breaking tables into bits}
3458%
3459% Unboxed tables have a wonderful advantage over boxed ones: you can stop
3460% halfway through and do something else for a bit. Here's how:
3461%
3462% \begin{macro}{\tabpause}
3463%
3464% I'd like to avoid forbidding catcode changes here. I'll use |\doafter|
3465% now I've got it, to ensure that colour handling and things occur
3466% \emph{inside} the |\noalign| (otherwise they'll mess up the alignment
3467% very seriously). We selectively include lots of stuff from
3468% |\arrayparboxrestore|.
3469%
3470% We have to be careful here to ensure that everything works correctly within
3471% lists. (The \package{amsmath} package had this problem in its
3472% |\intertext| macro, so I'm not alone here.)
3473%
3474% \begin{macrocode}
3475\def\tabpause#{%
3476 \noalign{\ifnum0=`}\fi%
3477 \let\if@nobreak\iffalse
3478 \let\if@noskipsec\iffalse
3479 \let\par\@@par
3480 \let\-\@dischyph
3481 \let\'\@acci\let\`\@accii\let\=\@acciii
3482 \everypar{}%
3483 \lineskip\normallineskip%
3484 \let\\\@normalcr%
7abe48b3 3485 \color@begingroup%
86f6a31e 3486 \tab@startpause%
3487 \vskip-\parskip%
3488 \parshape\@ne\@totalleftmargin\linewidth%
3489 \noindent%
3490 \doafter\tabpause@i%
3491}
3492\def\tabpause@i{%
3493 \nobreak%
3494 \tab@endpause%
7abe48b3 3495 \color@endgroup%
86f6a31e 3496 \ifnum0=`{\fi}%
3497}
3498% \end{macrocode}
3499%
3500% \end{macro}
3501%
3502%
3503% \subsection{The wonderful world of \cmd\multicolumn}
3504%
3505% \begin{macro}{\multicolumn}
3506%
3507% This is actually fantastically easy. Watch and learn. Make sure you
3508% notice the |\long|s here: remember that some table cells can contain
3509% paragraphs, so it seems sensible to allow |\par| into the argument.
3510% (As far as I know, most other |\multicolumn| commands don't do this,
3511% which seems a little silly. Then again, I forgot to do it the first
3512% time around.)
3513%
3514% \begin{macrocode}
3515\long\def\multicolumn#1#2#3{%
3516 \multispan{#1}%
3517 \begingroup%
3518 \tab@multicol%
7abe48b3 3519 \tab@initsubread%
86f6a31e 3520 \long\def\tab@midtext{#3}%
3521 \let\tab@looped\tab@err@multi%
3522 \tab@readpreamble{#2}%
3523 \the\tab@preamble%
3524 \endgroup%
3525 \ignorespaces%
3526}
3527% \end{macrocode}
3528%
3529% \end{macro}
3530%
3531%
3532% \subsection{Interlude: range lists}
3533%
3534% For processing arguments to |\vgap| and |\cline|, we need to be able to
3535% do things with lists of column ranges. To save space, and to make my
3536% fingers do less typing, here's some routines which do range handling.
3537%
3538% \begin{macro}{\ranges}
3539%
3540% Given a macro name and a comma separated list of ranges and simple numbers,
3541% this macro will call the macro giving it each range in the list in turn.
3542% Single numbers~$n$ will be turned into ranges $n$--$n$.
3543%
3544% The first job is to read the macro to do (which may already have some
3545% arguments attached to it). We'll also start a group to make sure that
3546% our changes to temp registers don't affect anyone else.
3547%
3548% There's a space before the delimiting |\q@delim| to stop numbers being
3549% parsed to far and expanding our quark (which will stop \TeX\ dead in its
3550% tracks). Since we use |\@ifnextchar| to look ahead, spaces in range lists
3551% are perfectly all right.
3552%
3553% \begin{macrocode}
3554\def\ranges#1#2{%
3555 \gdef\ranges@temp{#1}%
3556 \begingroup%
3557 \ranges@i#2 \q@delim%
3558}
3559% \end{macrocode}
3560%
3561%
3562% We're at the beginning of the list. We expect either the closing marker
3563% (if this is an empty list) or a number, which we can scoop up into a
3564% scratch register.
3565%
3566% \begin{macrocode}
3567\def\ranges@i{%
3568 \@ifnextchar\q@delim\ranges@done{\afterassignment\ranges@ii\count@}%
3569}
3570% \end{macrocode}
3571%
3572% We've read the first number in the range. If there's another number, we'll
3573% expect a `|-|' sign to be next. If there is no `|-|', call the user's code
3574% with the number duplicated and then do the rest of the list.
3575%
3576% \begin{macrocode}
3577\def\ranges@ii{%
3578 \@ifnextchar-\ranges@iii{\ranges@do\count@\count@\ranges@v}%
3579}
3580% \end{macrocode}
3581%
3582% Now we strip the `|-|' off and read the other number into a temporary
3583% register.
3584%
3585% \begin{macrocode}
3586\def\ranges@iii-{\afterassignment\ranges@iv\@tempcnta}
3587% \end{macrocode}
3588%
3589% We have both ends of the range now, so call the user's code, passing it
3590% both ends of the range.
3591%
3592% \begin{macrocode}
3593\def\ranges@iv{\ranges@do\count@\@tempcnta\ranges@v}
3594% \end{macrocode}
3595%
3596% We've finished doing an item now. If we have a `|,|' next, then start
3597% over with the next item. Otherwise, if we're at the end of the list,
3598% we can end happily. Finally, if we're totally confused, raise an
3599% error.
3600%
3601% \begin{macrocode}
3602\def\ranges@v{%
3603 \@ifnextchar,%
3604 \ranges@vi%
3605 {%
3606 \@ifnextchar\q@delim%
3607 \ranges@done%
3608 {\tab@err@range\ranges@vi,}%
3609 }%
3610}
3611% \end{macrocode}
3612%
3613% We had a comma, so gobble it, read the next number, and go round again.
3614%
3615% \begin{macrocode}
3616\def\ranges@vi,{\afterassignment\ranges@ii\count@}
3617% \end{macrocode}
3618%
3619% Here's how we call the user's code, now. We close the group, so that the
3620% user's code doesn't have to do global things to remember its results, and
3621% we expand the two range ends from their count registers. We also ensure
3622% that the range is the right way round.
3623%
3624% \begin{macrocode}
3625\def\ranges@do#1#2{%
3626 \ifnum#1>#2\else%
3627 \expandafter\endgroup%
3628 \expandafter\ranges@temp%
3629 \expandafter{%
3630 \the\expandafter#1%
3631 \expandafter}%
3632 \expandafter{%
3633 \the#2%
3634 }%
3635 \begingroup%
3636 \fi%
3637}
3638% \end{macrocode}
3639%
3640% And finishing the scan is really easy. We close the group after gobbling
3641% the close token.
3642%
3643% \begin{macrocode}
3644\def\ranges@done\q@delim{\endgroup}
3645% \end{macrocode}
3646%
3647% \end{macro}
3648%
3649% \begin{macro}{\ifinrange}
3650%
3651% Something a little more useful, now. |\ifinrange| takes four arguments:
3652% a number, a range list (as above), and two token lists which I'll call
3653% \emph{then} and \emph{else}. If the number is in the list, I'll do
3654% \emph{then}, otherwise I'll do \emph{else}.
3655%
3656% \begin{macrocode}
3657\def\ifinrange#1#2{%
3658 \@tempswafalse%
3659 \count@#1%
3660 \ranges\ifinrange@i{#2}%
3661 \if@tempswa%
3662 \expandafter\@firstoftwo%
3663 \else%
3664 \expandafter\@secondoftwo%
3665 \fi%
3666}
3667\def\ifinrange@i#1#2{%
3668 \ifnum\count@<#1 \else\ifnum\count@>#2 \else\@tempswatrue\fi\fi%
3669}
3670% \end{macrocode}
3671%
3672% \end{macro}
3673%
3674%
3675% \subsection{Horizontal rules OK}
3676%
3677% This is where all the gubbins for |\vgap| and friends is kept, lest it
3678% contaminate fairly clean bits of code found elsewhere.
3679%
7abe48b3 3680% \subsubsection{Common parsing for rule parameters twiddling}
3681%
3682% \begin{macro}{\tab@ruleparams}
86f6a31e 3683%
7abe48b3 3684% Given a macro name, make a (global) macro |\tab@ruledecls|, which sets
3685% |\dimen0| to be the chosen rule thickness, and sets up colours and whatnot,
3686% and then and calls the macro. We parse a `|*|' to mean
3687% |\arraythickrulewidth|, an optional argument which should be something
3688% |\setlength| can understand, or nothing, which gives the default
3689% |\arrayrulewidth|.
86f6a31e 3690%
7abe48b3 3691% To make this properly hookable, we need to make a list of properties and
3692% gather them together.
86f6a31e 3693%
3694% \begin{macrocode}
7abe48b3 3695\let\tab@rp@inithook\@empty
3696\let\tab@rp@sethook\@empty
3697\let\tab@rp@donehook\@empty
3698\let\tab@rp@default\@empty
3699\def\tab@ruleparams#1{%
3700 {\ifnum0=`}\fi%
3701 \tab@rp@inithook%
3702 \def\tab@rp@next{\ifnum0=`{\fi}#1}%
3703 \expandafter\tab@rp@keys\expandafter{\tab@rp@default}%
3704 \@ifstar\tab@rp@star\tab@rp@what%
86f6a31e 3705}
7abe48b3 3706\def\tab@rp@star{\dimen@\arraythickrulewidth\tab@rp@what}
3707\def\tab@rp@what{\@ifnextchar[\tab@rp@opt\tab@rp@done}
3708\def\tab@rp@opt[#1]{\tab@rp@keys{#1}\tab@rp@done}
3709\def\tab@rp@keys{\mkparse{mdwtab:rule}}
3710\def\tab@rp@done{%
3711 \protected@xdef\tab@rp@{\tab@rp@sethook}%
3712 \tab@rp@donehook%
3713 \tab@rp@next%
86f6a31e 3714}
7abe48b3 3715\def\tab@withrp#1{\tab@ruleparams{\tab@withrp@i{#1}}}
3716\def\tab@withrp@i#1{%
3717 \toks@{#1}%
3718 \toks@\expandafter{\the\expandafter\toks@\expandafter{\tab@rp@}}%
3719 \the\toks@%
3720}
3721% \end{macrocode}
3722%
3723% And now to define the width parameters.
3724%
3725% \begin{macrocode}
3726\tab@addhookafter\tab@rp@inithook{\dimen@\arrayrulewidth}
3727\tab@addhookafter\tab@rp@sethook{\dimen@\the\dimen@}
3728\tab@addhookafter\tab@rp@donehook{\global\tab@rulewidth\dimen@}
3729\mkdef{mdwtab:rule}{width}{\setlength\dimen@{#1}}
3730\mkdef{mdwtab:rule}{thin}*{\dimen@\arrayrulewidth}
3731\mkdef{mdwtab:rule}{thick}*{\dimen@\arraythickrulewidth}
3732\mkdef*{mdwtab:rule}*{\setlength\dimen@{#1}}
3733% \end{macrocode}
3734%
3735% \end{macro}
3736%
3737% \begin{macro}{\tabsetruleparams}
3738%
3739% And the user default-parameter list.
3740%
3741% \begin{macrocode}
3742\def\tabsetruleparams{\def\tab@rp@default}
86f6a31e 3743% \end{macrocode}
3744%
3745% \end{macro}
3746%
3747% \subsubsection{Drawing horizontal rules}
3748%
3749% \begin{macro}{\hline}
3750%
3751% Note the funny use of |\noalign| to allow \TeX\ stomach ops like
3752% |\futurelet| without starting a new table row. This lets us see if there's
3753% another |\hline| coming up, so we can see if we need to insert extra
3754% vertical space.
3755%
3756% \begin{macrocode}
7abe48b3 3757\def\hline{\noalign\tab@ruleparams\hline@prep}
86f6a31e 3758\def\hline@prep{%
3759 \tab@dohline%
3760 \noalign{\ifnum0=`}\fi%
3761 \tab@penalty%
3762 \futurelet\@let@token\hline@i%
3763}
3764% \end{macrocode}
3765%
3766% We check here for another |\hline| command, and insert glue if there is.
3767% This looks terrible, though, and |\hlx{hvh}| is much nicer. Still\dots
3768%
3769% \begin{macrocode}
3770\def\hline@i{%
3771 \@tempswafalse%
3772 \ifx\@let@token\hline\@tempswatrue\fi%
3773 \ifx\@let@token\hline@prep\@tempswatrue\fi%
3774 \if@tempswa%
3775 \vskip\doublerulesep%
3776 \tab@addruleheight\doublerulesep%
3777 \fi%
3778 \ifnum0=`{\fi}%
3779}
3780% \end{macrocode}
3781%
3782% \end{macro}
3783%
3784% \begin{macro}{\tab@dohline}
3785%
3786% This is where hlines actually get drawn.
3787% Drawing lines is more awkward than it used to be, particularly in unboxed
3788% tables. It used to be a case simply of saying |\noalign{\hrule}|.
3789% However, since unboxed tables are actually much wider than they look, this
3790% would make the rules stretch right across the page and look generally
3791% horrible.
3792%
3793% The solution is simple: we basically do a dirty big |\cline|.
3794%
3795% \begin{macrocode}
3796\def\tab@dohline{%
7abe48b3 3797 \multispan\tab@columns%
3798 \color@begingroup%
3799 \tab@rp@\leaders\hrule\@height\dimen@\hfil%
3800 \tab@addruleheight\dimen@%
3801 \color@endgroup%
86f6a31e 3802 \cr%
3803}
3804% \end{macrocode}
3805%
3806% \end{macro}
3807%
3808% \subsubsection{Vertical rules}
3809%
3810% I couldn't fit these in anywhere else, so they'll have to go here. I'll
3811% provide a new optional argument which specifies the width of the rule; this
3812% gets rid of the problem described in the \emph{Companion}, where to get
3813% an unusually wide vertical rule, you have to play with things like
3814% \syntax{"\\vrule width" <dimen>} which really isn't too nice.
3815%
3816% \begin{macro}{\vline}
3817%
7abe48b3 3818% Now uses the general |\tab@ruleparams| parser. We save and restore the
3819% global |\tab@rulewidth| parameter here.
86f6a31e 3820%
3821% \begin{macrocode}
3822\def\vline{%
3823 \begingroup%
7abe48b3 3824 \@tempdima\tab@rulewidth\let\safe@\tab@rp@%
3825 \tab@ruleparams\tab@vline%
3826}
3827\def\tab@vline{%
3828 \tab@rp@\vrule\@width\dimen@%
3829 \global\tab@rulewidth\@tempdima\global\let\tab@rp@\safe@%
3830 \endgroup%
86f6a31e 3831}
3832% \end{macrocode}
3833%
3834% \end{macro}
3835%
3836% \subsubsection{Drawing bits of lines}
3837%
3838% Just for a bit of fun, here's an extended version of |\cline| which takes
3839% a list of columns to draw lines under, rather than just a single range.
3840%
3841% \begin{macro}{\cline}
3842%
3843% Not a single line of code written yet, and we already have a dilemma on
3844% our hands. Multiple consecutive |\cline| commands are meant to draw
3845% on the same vertical bit of table. But horizontal lines are meant to have
7abe48b3 3846% thickness now. Worse, if the lines have real thickness then we leave gaps
3847% in the vertical rules which aren't covered by our line. But if we
3848% backspace over the line, then we overwrite it with coloured blobs.
3849%
3850% We give up on doing the job properly -- that's just doomed. Backspace over
3851% the previous row, and provide a hack for doing the spacing right elsewhere.
86f6a31e 3852%
3853% Now the problem remains how best to do the job. The way I see it, there
3854% are three possibilities:
3855%
3856% \begin{itemize}
3857%
3858% \item We can start a table row, and then for each column of the table
3859% (as recorded in |\tab@columns|) we look to see if that column is
3860% listed in the range list and if so draw the rule. This requires
3861% lots of scanning of the range list.
3862%
3863% \item We can take each range in the list, and draw rules appropriately,
3864% just like the old |\cline| used to do, and starting a new table row
3865% for each.
3866%
3867% \item We can start a table row, and then for each range remember where we
3868% stopped drawing the last row, move to the start of the new one, and
3869% draw it. If we start moving backwards, we close the current row
3870% and open a new one.
3871%
3872% \end{itemize}
3873%
3874% The last option looks the most efficient, and the most difficult. This
3875% is therefore what I shall do |;-)|.
3876%
3877% The first thing to do is to add in a little negative space, and start a
3878% table row (omitting the first item). Then scan the range list, and finally
3879% close the table row and add some negative space again.
3880%
3881% We need a global count register to keep track of where we are. Mixing
3882% local and global assignments causes all sorts of tragedy, so I shall hijack
3883% |\tab@state|.
3884%
3885% \begin{macrocode}
7abe48b3 3886\def\cline{\noalign\tab@ruleparams\cline@do}
86f6a31e 3887% \end{macrocode}
3888%
3889% Now for the tricky bit. When we're given a range, we look to see if the
3890% first number is less than |\tab@state|. If so, we quickly close the
3891% current row, kern backwards and start again with an |\omit| and reset
7abe48b3 3892% |\tab@state| to 1, and try again. This is hardly perfect, but gets the job
3893% done in many cases. Correct |\vgap| insertion fixes the remaining bugs.
86f6a31e 3894%
3895% \begin{macrocode}
7abe48b3 3896\def\cline@do#1{%
3897 \noalign{\kern-\tab@rulewidth}%
3898 \omit%
3899 \global\tab@state\@ne%
3900 \ranges\cline@do@i{#1}\cr%
3901}
3902\def\cline@do@i#1#2{%
86f6a31e 3903 \ifnum#1<\tab@state\relax%
3904 \tab@@cr%
3905 \noalign{\kern-\tab@rulewidth\tab@penalty}%
3906 \omit%
3907 \global\tab@state\@ne%
3908 \fi%
3909% \end{macrocode}
3910%
3911% We are now either at or in front of the column position required. If
7abe48b3 3912% we're too far back, we must |\hfil&\omit| our way over to the correct
86f6a31e 3913% column.
3914%
3915% \begin{macrocode}
3916 \@whilenum\tab@state<#1\do{%
3917 \hfil\tab@@tab@omit%
3918 \global\advance\tab@state\@ne%
3919 }%
3920% \end{macrocode}
3921%
3922% We've found the start correctly. We must deal with a tiny problem now:
3923% if this is not the first table cell, the left hand vertical rule is in the
3924% column to the left, so our horizontal rule won't match up properly. So
3925% we skip back by a bit to compensate. If there isn't actually a vertical
3926% rule to line up with, no-one will notice, because the rules are so thin.
3927% This adds a little touch of quality to the whole thing, which is after all
3928% the point of this whole exercise.
3929%
3930% \begin{macrocode}
3931 \ifnum\tab@state>\@ne%
3932 \kern-\arrayrulewidth%
3933 \fi%
3934% \end{macrocode}
3935%
3936% Now we must stretch this table cell to the correct width.
3937%
3938% \begin{macrocode}
3939 \@whilenum\tab@state<#2\do{%
3940 \tab@@span@omit%
3941 \global\advance\tab@state\@ne%
3942 }%
3943% \end{macrocode}
3944%
3945% We're ready. Draw the rule. Note that this is |\hfill| glue, just in case
3946% we start putting in |\hfil| glue when we step onto the next cell.
3947%
3948% \begin{macrocode}
7abe48b3 3949 \color@begingroup%
3950 \tab@rp@%
86f6a31e 3951 \leaders\hrule\@height\tab@rulewidth\hfill%
7abe48b3 3952 \color@endgroup%
86f6a31e 3953}
3954% \end{macrocode}
3955%
3956% Some alignment primitives are hidden inside macros so they don't get seen
3957% at the wrong time. This is what they look like:
3958%
3959% \begin{macrocode}
3960\def\tab@@cr{\cr}
3961\def\tab@@tab@omit{&\omit}
3962\def\tab@@span@omit{\span\omit}
3963% \end{macrocode}
3964%
3965% \end{macro}
3966%
3967% \subsubsection{Drawing short table rows}
3968%
3969% Before I start on a description of more code, I think I'll briefly discuss
3970% my reasons for leaving the |\vgap| command in its current state. There's a
3971% reasonable case for introducing an interface between |\vgap| and
3972% |\multicolumn|, to avoid all the tedious messing about with column
3973% ranges. There are good reasons why I'm not going to do this:
3974%
3975% \begin{itemize}
3976%
3977% \item It's very difficult to do: it requires either postprocessing of
3978% the table or delaying processing of each row until I know exactly
3979% what's in it; a |\multicolumn| in a row should be able to affect
3980% a |\vgap| before the row, which gets very nasty. This package is
3981% probably far too large already, and adding more complexity and
3982% running the risk of exhausting \TeX's frustratingly finite capacity
3983% for the sake of relieving the user of a fairly trivial job doesn't
3984% seem worthwhile.
3985%
3986% \item Perhaps more importantly, there are perfectly valid occasions when
3987% it's useful to have the current vgap behaviour. For example, the
3988% \texttt{MIX} word layout diagrams found in \emph{The Art of
3989% Computer Programming} use the little `stub lines' to show where
3990% data items cross byte boundaries:
3991%
3992% ^^A This actually looks terrifyingly similar to the original.
3993% ^^A The leading @{} is there to stop the table looking off-centre,
3994% ^^A because there's no left hand rule telling you where the table
3995% ^^A starts, like there is on the right, just the \tabcolsep glue.
3996%
3997% \begingroup
3998% \newcommand{\wide}[2]{\multicolumn{#1}{c|}{\ttfamily #2}}
3999% \begin{tabular}[C]{@{} r @{\qquad} | Mc | *{5}{c|}} \hlx{c{2-7} v}
4000% empty & - & 1 & 0 & 0 & 0 & 0 \\ \hlx{v c{2-7} v}
4001% occupied & + & \wide{2}{LINK} & \wide{3}{KEY} \\ \hlx{v c{2-7}}
4002% \end{tabular}
4003% \endgroup
4004%
4005% \end{itemize}
4006%
4007% That's my excuses out of the way; now I'll press on with the actual
4008% programming.
4009%
4010% \begin{macro}{\tab@checkrule}
4011%
4012% We have a range list in |\tab@xcols| and a number as an argument. If we
4013% find the number in the list, we just space out the following group,
4014% otherwise we let it be.
4015%
4016% \begin{macrocode}
4017\def\tab@checkrule#1{%
4018 \count@#1\relax%
4019 \expandafter\ifinrange%
4020 \expandafter\count@%
4021 \expandafter{\tab@xcols}%
4022 {\tab@checkrule@i}%
4023 {}%
4024}
4025\def\tab@checkrule@i#1{\setbox\z@\hbox{#1}\hb@xt@\wd\z@{}}
4026% \end{macrocode}
4027%
4028% \end{macro}
4029%
4030% \begin{macro}{\vgap}
4031%
4032% We must tread carefully here. A single misplaced stomach operation can
4033% cause error messages. We therefore start with an |\omit| so we can search
4034% for optional arguments.
4035%
4036% So that |\hlx| can get control after |\vgap| has finished, we provide a
4037% hook called |\vgap@after| which is expanded after |\vgap| has finished.
4038% Here we make it work like |\@empty|, which expands to nothing. (Note that
4039% |\relax| will start a new table row, so we can't use that.) There are
4040% some penalty items here to stick the |\vgap| row to the text row and
4041% |\hline| that are adjacent to it. The \package{longtable} package will
4042% split an |\hline| in half, so this is the correct thing to do.
4043%
4044% \begin{macrocode}
4045\def\vgap{%
4046 \noalign{\nobreak}%
4047 \omit%
4048 \global\let\vgap@after\@empty%
4049 \iffalse{\fi\ifnum0=`}\fi%
4050 \@ifnextchar[\vgap@i\vgap@simple%
4051}
4052% \end{macrocode}
4053%
4054% We set up two different sorts of |\vgap| -- a simple one which allows all
4055% rules to be passed through, and a specific one which carefully vets each
4056% one (and is therefore slower). We decide which to so based on the presence
4057% of an optional argument.
4058%
4059% The optional argument handler just passes its argument to an interface
4060% routine which is used by |\hlx|.
4061%
4062% \begin{macrocode}
4063\def\vgap@i[#1]{\vgap@spec{#1}}
4064% \end{macrocode}
4065%
4066% Now we handle specified columns. Since we're in an omitted table cell, we
4067% must set things up globally. Assign the column spec to a macro, and set up
4068% vetting by the routine above. Then just go and do the job.
4069%
4070% \begin{macrocode}
4071\def\vgap@spec#1#2{%
4072 \gdef\tab@xcols{#1}%
4073 \global\let\tab@ckr\tab@checkrule%
4074 \vgap@do{#2}%
4075}
4076% \end{macrocode}
4077%
4078% Handle all columns. Just gobble the column number for each rule, and let
4079% the drawing pass unharmed. Easy.
4080%
4081% \begin{macrocode}
4082\def\vgap@simple#1{%
4083 \global\let\tab@ckr\@gobble%
4084 \vgap@do{#1}%
4085}
4086% \end{macrocode}
4087%
4088% This is where stuff actually gets done. We set the |\vgap| flag on while
4089% we do the short row. Then just expand the token list we built while
4090% scanning the preamble.
4091%
4092% Note that the flag is cleared at the end of the last column, to allow other
4093% funny things like |\noalign| and |\omit| before a new row is started.
4094%
4095% \begin{macrocode}
4096\def\vgap@do#1{%
4097 \ifnum0=`{}\fi%
4098 \global\tab@vgaptrue%
4099 \the\tab@shortline%
4100 \vrule\@height#1\@width\z@%
4101 \global\tab@vgapfalse
4102 \tab@addruleheight{#1}%
4103 \cr%
4104 \noalign{\nobreak}%
4105 \vgap@after%
4106}
4107% \end{macrocode}
4108%
4109% \end{macro}
4110%
4111% \subsubsection{Prettifying syntax}
4112%
4113% \begin{macro}{\hlx}
4114%
4115% This is like a poor cousin to the preamble parser. The whole loop is
4116% carefully written to take place \emph{only} in \TeX's mouth, so the
4117% alignment handling bits half way down the gullet don't see any of this.
4118%
4119% First, pass the string to another routine.
4120%
4121% \begin{macrocode}
7abe48b3 4122\def\hlx{\noalign\tab@ruleparams\hlx@prep}
86f6a31e 4123\def\hlx@prep#1{\hlx@loop#1\q@delim}
4124% \end{macrocode}
4125%
4126% Now peel off a token, and dispatch using |\csname|. We handle
4127% undefinedness of the command in a fairly messy way, although it probably
4128% works. Maybe.
4129%
4130% \begin{macrocode}
4131\def\hlx@loop#1{%
4132 \ifx#1\q@delim\else%
4133 \@ifundefined{hlx@cmd@\string#1}{%
4134 \expandafter\hlx@loop%
4135 }{%
4136 \csname hlx@cmd@\string#1\expandafter\endcsname%
4137 }%
4138 \fi%
4139}
4140% \end{macrocode}
4141%
4142% \end{macro}
4143%
4144% \begin{macro}{\hlxdef}
4145%
4146% New |\hlx| commands can be defined using |\hlxdef|. This is a simple
4147% abbreviation.
4148%
4149% \begin{macrocode}
4150\def\hlxdef#1{\@namedef{hlx@cmd@#1}}
4151% \end{macrocode}
4152%
4153% \end{macro}
4154%
4155% \begin{macro}{\hlx h}
4156%
4157% Handle an \lit{h} character. Just do an |\hline| and return to the loop.
4158% We look ahead to see if there's another \lit{h} coming up, and if so
4159% insert two |\hline| commands. This strange (and inefficient) behaviour
4160% keeps packages which redefine |\hline| happy.
4161%
4162% \begin{macrocode}
4163\hlxdef h#1{%
4164 \noalign{%
7abe48b3 4165 \ifx#1h\def\@tempa{\hline@prep\hline@prep\hlx@loop}%
4166 \else\def\@tempa{\hline@prep\hlx@loop#1}%
4167 \fi\expandafter
86f6a31e 4168 }%
4169 \@tempa%
4170}
4171% \end{macrocode}
4172%
4173% \end{macro}
4174%
4175% \begin{macro}{\hlx b}
4176%
4177% The \lit{b} character does a nifty backspace, for \package{longtable}'s
4178% benefit.
4179%
4180% \begin{macrocode}
4181\hlxdef b{\noalign{\kern-\arrayrulewidth}\hlx@loop}
4182% \end{macrocode}
4183%
4184% \end{macro}
4185%
4186% \begin{macro}{\hlx /}
4187%
4188% The `"/"' character allows a page break at the current position.
4189%
4190% \begin{macrocode}
7abe48b3 4191\hlxdef /{\noalign{\ifnum0=`}\fi\@testopt\hlx@cmd@break@i0}
4192\def\hlx@cmd@break@i[#1]{\ifnum0=`{\fi}\pagebreak[#1]\hlx@loop}
86f6a31e 4193% \end{macrocode}
4194%
4195% \end{macro}
4196%
4197% \begin{macro}{\hlx v}
7abe48b3 4198% \begin{macro}{\hlx z}
86f6a31e 4199%
7abe48b3 4200% Handle a \lit{v} or \lit{z} character. This is rather like the |\vgap|
4201% code above, although there are syntactic differences.
86f6a31e 4202%
4203% \begin{macrocode}
7abe48b3 4204\hlxdef v{\hlx@vgap\doublerulesep}
4205\hlxdef z{\hlx@vgap\tab@rulewidth}
4206\def\hlx@vgap#1{%
86f6a31e 4207 \noalign{\nobreak}%
4208 \omit%
4209 \iffalse{\fi\ifnum0=`}\fi%
4210 \global\let\vgap@after\hlx@loop%
7abe48b3 4211 \@ifnextchar[{\hlx@vgap@i{#1}}{\hlx@vgap@ii\vgap@simple{#1}}%
86f6a31e 4212}
7abe48b3 4213\def\hlx@vgap@i#1[#2]{%
4214 \ifx!#2!\def\@tempa{\hlx@vgap@ii\vgap@simple{#1}}%
4215 \else\def\@tempa{\hlx@vgap@ii{\vgap@spec{#2}}{#1}}\fi%
86f6a31e 4216 \@tempa%
4217}
7abe48b3 4218\def\hlx@vgap@ii#1#2{\@testopt{\hlx@vgap@iii{#1}}{#2}}
86f6a31e 4219\def\hlx@vgap@iii#1[#2]{#1{#2}}
4220% \end{macrocode}
4221%
4222% \end{macro}
7abe48b3 4223% \end{macro}
86f6a31e 4224%
4225% \begin{macro}{\hlx s}
4226%
4227% Allow the user to leave a small gap using the \lit{s} command.
4228%
4229% \begin{macrocode}
4230\hlxdef s{%
4231 \noalign{\ifnum0=`}\fi%
4232 \nobreak%
7abe48b3 4233 \@testopt\hlx@space@i\doublerulesep%
86f6a31e 4234}
4235\def\hlx@space@i[#1]{%
4236 \vskip#1%
4237 \tab@addruleheight{#1}%
4238 \ifnum0=`{\fi}%
4239 \hlx@loop%
4240}
4241% \end{macrocode}
4242%
4243% \end{macro}
4244%
4245% \begin{macro}{\hlx c}
4246%
7abe48b3 4247% We might as well allow a \lit{c} command to do a |\cline|. The fix to
4248% |\cline| permeates here.
86f6a31e 4249%
4250% \begin{macrocode}
7abe48b3 4251\hlxdef c#1{\cline@do{#1}\hlx@loop}
4252% \end{macrocode}
4253%
4254% \end{macro}
4255%
4256% \begin{macro}{\hlx ?}
4257%
4258% Do some arbitrary stuff which won't typeset. Put the stuff in a box which
4259% is discarded, just in case.
4260%
4261% \begin{macrocode}
4262\hlxdef ?#1{%
4263 \noalign{\setbox\z@\hbox{\color@begingroup#1\color@endgroup}}\hlx@loop%
4264}
4265% \end{macrocode}
4266%
4267% \end{macro}
4268%
4269% \begin{macro}{\hlx !}
4270%
4271% Change parameters in mid-flow.
4272%
4273% \begin{macrocode}
4274\hlxdef !#1{\noalign\tab@ruleparams\hlx@loop[{#1}]}
86f6a31e 4275% \end{macrocode}
4276%
4277% \end{macro}
4278%
4279% \begin{macro}{\hlx .}
4280%
4281% The \lit{.} character forces a start of the new column. There's a little
4282% problem here. Since the \lit{.} character starts the next column, we need
4283% to gobble any spaces following the |\hlx| command before the cell contents
4284% actually starts. Unfortunately, |\ignorespaces| will start the column for
4285% us, so we can't put it in always. We'll handle it here, then. We'll take
4286% the rest of the `preamble' string, and warn if it's not empty. Then we'll
4287% |\ignorespaces| -- this will start the column for us, so we don't need to
4288% |\relax| any more.
4289%
4290% \begin{macrocode}
4291\hlxdef .#1\q@delim{%
4292 \ifx @#1@\else%
4293 \PackageWarning{mdwtab}{%
4294 Ignoring \protect\hlx\space command characters following a
4295 `.'\MessageBreak command%
4296 }%
4297 \fi%
4298 \ignorespaces%
4299}
4300% \end{macrocode}
4301%
4302% \end{macro}
4303%
7abe48b3 4304% \begin{macro}{\hlx +}
4305% \begin{macro}{\nextrow}
4306%
4307% The \lit{+} subcommand just steps the table-row counter.
4308%
4309% \begin{macrocode}
4310\hlxdef +{\nextrow\hlx@loop}
4311\def\nextrow{\noalign{\ifnum0=`}\fi\@testopt\nextrow@i\@ne}
4312\def\nextrow@i[#1]{\global\advance\c@tabrow#1\ifnum0=`{\fi}}
4313% \end{macrocode}
4314%
4315% \end{macro}
4316% \end{macro}
4317%
86f6a31e 4318%
4319% \subsection{Starting new table rows}
4320%
4321% We take a break from careful mouthery at last, and start playing with
4322% newlines. The standard one allows pagebreaks in unboxed tables, which
4323% isn't really too desirable.
4324%
4325% Anyway, we'll try to make this macro rather more reusable than the standard
4326% one. Here goes.
4327%
4328% \begin{macro}{\@arraycr}
4329%
4330% We pass lots of information to a main parser macro, and expect it to cope.
4331%
4332% \begin{macrocode}
4333\def\@arraycr{\tab@arraycr{}}
4334\def\tab@arraycr#1{\tab@cr{\tab@tabcr{#1}}{}{}}
4335% \end{macrocode}
4336%
4337% Now to actually do the work. |\tab@cr| passes us the skip size, and the
4338% appropriate one of the two arguments given above (both of which are empty)
4339% depending on the presence of the $*$.
4340%
4341% \begin{macrocode}
4342\def\tab@tabcr#1#2{%
4343% \end{macrocode}
4344%
4345% If the total height I need to add between rows (from the optional argument
4346% and the `extrasep' parameter) is greater than zero, I'll handle this by
4347% extending the strut slightly. I'm not actually sure whether this is the
4348% right thing to do, to be honest, although it's easier than trying to
4349% to an automatic |\vgap|, because I need to know which columns to skip.
4350% If the space is less than zero, I'll just insert the vertical space with
4351% in a |\noalign|.
4352%
4353% First, to calculate how much space needs adding.
4354%
4355% \begin{macrocode}
4356 \setlength\dimen@{#2}%
4357 \advance\dimen@\tab@extrasep%
4358% \end{macrocode}
4359%
4360% If the height is greater than zero, I need to play with the strut. I must
4361% bear in mind that the current table cell (which I'm still in, remember)
4362% may be in vertical mode, and I may or may not be in a paragraph.
4363%
4364% If I am in vertical mode, I'll backpedal to the previous box and put the
4365% strut in an hbox superimposed on the previous baseline. Otherwise, I can
4366% just put the strut at the end of the text. (This works in either LR
4367% or paragraph mode as long as I'm not between paragraphs.) Again, Rowland's
4368% empty cell bug strikes. (See |\tab@epar| for details.)
4369%
4370% \begin{macrocode}
4371 \ifdim\dimen@>\z@%
4372 \ifvmode%
4373 \unskip\ifdim\prevdepth>-\@m\p@\kern-\prevdepth\fi%
4374 \nointerlineskip\expandafter\hbox%
4375 \else%
4376 \@maybe@unskip\expandafter\@firstofone%
4377 \fi%
4378 {\advance\dimen@\dp\@arstrutbox\vrule\@depth\dimen@\@width\z@}%
4379 \fi%
4380% \end{macrocode}
4381%
4382% This table cell works as a group (which is annoying here). I'll copy the
4383% interrow gap into a global register so that I can use it in the |\noalign|.
4384%
4385% \begin{macrocode}
4386 \global\dimen\@ne\dimen@%
4387 \cr%
4388 \noalign{%
4389 #1%
4390 \ifdim\dimen\@ne<\z@\vskip\dimen\@ne\relax\fi%
4391 }%
4392 \@gobble%
4393}
4394% \end{macrocode}
4395%
4396% \end{macro}
4397%
4398% \begin{macro}{\tab@setcr}
4399%
4400% To set the |\\| command correctly in each table cell, we make it a part of
4401% the preamble (in |\tab@midtext|) to call this routine. It's easy -- just
4402% saves the preamble from being huge.
4403%
4404% \begin{macrocode}
4405\def\tab@setcr{\let\\\tabularnewline}
4406% \end{macrocode}
4407%
4408% \end{macro}
4409%
4410% \begin{macro}{\tab@cr}
4411%
4412% Now we do the parsing work. This is fun. Note the revenge of the funny
4413% braces here. Nothing to worry about, honest. The tricky bit is to keep
4414% track of which arguments are which. (Thanks to David Carlisle for pointing
4415% out that I'd missed out the |\relax| here.)
4416%
4417% \begin{macrocode}
4418\def\tab@cr#1#2#3{%
4419 \relax%
4420 \iffalse{\fi\ifnum0=`}\fi%
4421 \@ifstar{\tab@cr@i{#1}{#3}}{\tab@cr@i{#1}{#2}}%
4422}
7abe48b3 4423\def\tab@cr@i#1#2{\@testopt{\tab@cr@ii{#1}{#2}}\z@}
4424\def\tab@cr@ii#1#2[#3]{\ifnum0=`{}\fi#1{#3}{#2}}
86f6a31e 4425% \end{macrocode}
4426%
4427% \end{macro}
4428%
4429%
4430% \subsection{Gratuitous grotesquery}
4431%
4432% So far we've had an easy-ish ride (or should that be \emph{queasy}?). Now
4433% for something unexplainably evil. We convince \LaTeX\ that it's loaded the
4434% \package{array} package, so that packages which need it think they've got
4435% it.
4436%
4437% The bogus date is the same as the date for the \package{array} package I've
4438% got here -- this will raise a warning if Frank updates his package which
4439% should filter back to me telling me that there's something I need to
4440% know about.
4441%
4442% The messing with |\xdef| and the funny parsing ought to insert the current
4443% \package{mdwtab} version and date into the fake \package{array} version
4444% string, giving a visible clue to the user that this isn't the real
4445% \package{array} package.
4446%
4447% \begin{macrocode}
4448\begingroup
4449\catcode`.=11
4450\def\@tempa#1 #2 #3\@@{#1 #2}
4451\xdef\ver@array.sty
4452 {1995/11/19 [mdwtab.sty \expandafter\@tempa\ver@mdwtab.sty\@@]}
4453\endgroup
4454% \end{macrocode}
4455%
4456%
4457% \subsection{Error messages}
4458%
4459% I've put all the error messages together, where I can find them, translate
4460% them or whatever.
4461%
4462% First, some token-space saving (which also saves my fingers):
4463%
4464% \begin{macrocode}
4465\def\tab@error{\PackageError{mdwtab}}
4466% \end{macrocode}
4467%
4468% Now do the error messages.
4469%
4470% \begin{macrocode}
4471\def\tab@err@misscol{%
4472 \tab@error{Missing column type}{%
4473 I'm lost. I was expecting something describing^^J%
4474 the type of the current column, but you seem to^^J%
4475 have missed it out. I've inserted a type `l'^^J%
4476 column here in the hope that this makes sense.%
4477 }%
4478}
4479% \end{macrocode}
4480%
4481% \begin{macrocode}
4482\def\tab@err@oddgroup{%
4483 \tab@error{Misplaced group in table preamble}{%
4484 I've found an open brace character in your preamble^^J%
4485 when I was expecting a specifier character. I'm^^J%
4486 going to gobble the whole group and carry on as if^^J%
4487 I'd never seen it.%
4488 }%
4489}
4490% \end{macrocode}
4491%
4492% \begin{macrocode}
4493\def\tab@err@undef#1{%
4494 \tab@error{Unknown `\tab@colset' preamble character `\string#1'}{%
4495 I don't understand what you meant by typing this^^J%
4496 character. Anyway, I'll ignore it this time around.^^J%
4497 Just don't you do it again.%
4498 }%
4499}
4500% \end{macrocode}
4501%
4502% \begin{macrocode}
4503\def\tab@err@unbrh{%
4504 \tab@error{Can't use unboxed tabular in LR mode}{%
4505 You've asked for a tabular or array environment with^^J%
4506 `L', `C' or `R' as the position specifier, but you're^^J%
4507 in LR (restricted horizontal) mode, so it won't work.^^J%
4508 I'll assume you really meant `c' and soldier on.%
4509 }%
4510}
4511% \end{macrocode}
4512%
4513% \begin{macrocode}
4514\def\tab@err@unbmm{%
4515 \tab@error{Can't use unboxed tabular in maths mode}{%
4516 You've asked for a tabular or array environment with^^J%
4517 `L', `C' or `R' as the position specifier, but you're^^J%
4518 in maths mode, so it won't work. I'll pretend that^^J%
4519 you really typed `c', and that this is all a bad dream.%
4520 }%
4521}
4522% \end{macrocode}
4523%
4524% \begin{macrocode}
4525\def\tab@err@unbext{%
4526 \tab@error{Can't extend unboxed tabulars}{%
4527 You're trying to use kludgy extensions (e.g.,^^J%
4528 `delarray') on an array or tabular with `L', `C'^^J%
4529 or `R' as the position specifier. I'll assume you^^J%
4530 subconsciously wanted a `c' type all along.%
4531 }%
4532}
4533% \end{macrocode}
4534%
4535% \begin{macrocode}
4536\def\tab@err@multi{%
4537 \tab@error{More than one column in a \protect\multicolumn}{%
4538 You've put more than one column into a \string\multicolumn^^J%
4539 descriptor. It won't work. I have no idea what^^J%
4540 will happen, although it won't be pleasant. Hold^^J%
4541 on tight now...%
4542 }%
4543}
4544% \end{macrocode}
4545%
4546% \begin{macrocode}
4547\def\tab@err@range{%
4548 \tab@error{Expected `,' or `<end>' in range list}{%
4549 I was expecting either the end of the range list,^^J%
4550 or a comma, followed by another range. I've^^J%
4551 inserted a comma to try and get me back on track.^^J%
4552 Good luck.%
4553 }%
4554}
4555% \end{macrocode}
4556%
7abe48b3 4557%
4558% \subsection{Loading the colour package}
4559%
4560% If requested, we load the \package{mtcolour} package here. This ensures
4561% that it can patch this code if it needs to.
4562%
4563% \begin{macrocode}
4564\iftab@colour
4565 \RequirePackage{mtcolour}
4566\fi
4567% \end{macrocode}
4568%
86f6a31e 4569% That's it. No more. Move along please.
4570%
4571% \begin{macrocode}
4572%</mdwtab>
4573% \end{macrocode}
4574%
4575%
4576%^^A-------------------------------------------------------------------------
7abe48b3 4577% \section{Implementation of \package{mtcolour}}
4578%
4579%
4580% This is in a separate package to avoid dragging in the \package{color}
4581% package if it's unwanted.
4582%
4583% I prefer English spellings. Here's a trivial redirection for Americans.
4584%
4585% \begin{macrocode}
4586%<*color>
4587\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{mtcolour}}
4588\ProcessOptions
4589\RequirePackage{mtcolour}
4590%</color>
4591% \end{macrocode}
4592%
4593% And now we can start the thing properly.
4594%
4595% \begin{macrocode}
4596%<*colour>
4597\RequirePackage{color}
4598% \end{macrocode}
4599%
4600%
4601% \subsection{Cell background colours}
4602%
4603% First, some simple preliminaries. The |\iftab@colour| switch is set if the
4604% current cell is meant to have a colour.
4605%
4606% \begin{macrocode}
4607\newif\iftab@colour
4608\tab@colourfalse
4609% \end{macrocode}
4610%
4611% We shall store the cell colour information in |\tab@cellcolour|, and the
4612% row colour information as |\tab@rowcolour|. Because of the structure of
4613% tables, we need to make global assignments; so we must copy the current
4614% value away at the start of a table and put the value back at the end. In
4615% order to transfer the overhang information reliably, we use a separate
4616% control sequence |\tab@colouroverhangs| for that -- otherwise |\color| can
4617% corrupt it.
4618%
4619% \begin{macrocode}
4620\tab@addhookbefore\tab@beginhook{%
4621 \let\tab@saverowcolour\tab@rowcolour%
4622 \let\tab@savecolouroverhangs\tab@colouroverhangs%
4623 \let\tab@savecellcolour\tab@cellcolour%
4624}
4625\tab@addhookafter\tab@endhook{%
4626 \global\let\tab@rowcolour\tab@saverowcolour%
4627 \global\let\tab@colouroverhangs\tab@savecolouroverhangs%
4628 \global\let\tab@cellcolour\tab@savecellcolour%
4629}
4630% \end{macrocode}
4631%
4632% Initially, there are no colours.
4633%
4634% \begin{macrocode}
4635\let\tab@rowcolour\@empty%
4636\let\tab@cellcolour\@empty%
4637\let\tab@colouroverhangs\@empty%
4638% \end{macrocode}
4639%
4640% \begin{macro}{\@snarfcolour}
4641%
4642% Reading a colour specification is something we'll need to do a few times,
4643% so an abstraction is useful. Its single argument is a continuation to
4644% which we pass a colour-spec acceptable to the |\color| command. (This is
4645% the same code as found in the \package{sverb} package. Remember to keep
4646% them in step.)
4647%
4648% \begin{macrocode}
4649\def\@snarfcolour#1{%
4650 \@ifnextchar[{\@snarfcolour@i{#1}}{\@snarfcolour@ii{#1}{}}%
4651}
4652\def\@snarfcolour@i#1[#2]{\@snarfcolour@ii{#1}{[#2]}}
4653\def\@snarfcolour@ii#1#2#3{#1{#2{#3}}}
4654% \end{macrocode}
4655%
4656% \end{macro}
4657%
4658% \begin{macro}{\cellcolour}
4659%
4660% Setting a cell colour is a matter of stashing the right declarations in
4661% |\tab@cellcolour| and |\tab@colouroverhangs|. Note that the overhangs end
4662% up in |\dimen0| and |\dimen2|.
4663%
4664% \begin{macrocode}
4665\def\cellcolour{%
4666 \@ifstar{\tab@ccol@i{\let\tab@rowcolour\@empty}}{\tab@ccol@i{}}%
4667}
4668\def\tab@ccol@i#1{\@snarfcolour{\tab@ccol@ii{#1}}}
4669\def\tab@ccol@ii#1#2{\@testopt{\tab@ccol@iii{#2#1}}\z@}
4670\def\tab@ccol@iii#1[#2]{\@testopt{\tab@ccol@iv{#1}{#2}}{#2}}
4671\def\tab@ccol@iv#1#2[#3]{%
4672 \gdef\tab@cellcolour{\color#1\tab@colourtrue}%
4673 \gdef\tab@colouroverhangs{%
4674 \setlength\dimen@{#2}%
4675 \setlength{\dimen\tw@}{#3}%
4676 }%
4677}
4678% \end{macrocode}
4679%
4680% \end{macro}
4681%
4682% \begin{macro}{\rowcolour}
4683%
4684% Setting the global row colour is simpler, because we don't mess with
4685% overhangs.
4686%
4687% \begin{macrocode}
4688\def\rowcolour{\@snarfcolour\tab@setrowcolour}
4689\let\rowcolor\rowcolour
4690\def\tab@setrowcolour#1{%
4691 \gdef\tab@rowcolour{\color#1\tab@colourtrue}%
4692}
4693% \end{macrocode}
4694%
4695% \end{macro}
4696%
4697% \begin{macro}{\rowcolouroff}
4698%
4699% And turning the global colouring off is easy.
4700%
4701% \begin{macrocode}
4702\def\rowcolouroff{\global\let\tab@rowcolour\@empty}
4703\let\rowcoloroff\rowcolouroff
4704% \end{macrocode}
4705%
4706% \end{macro}
4707%
4708% \begin{macro}{\tab@colourleft}
4709%
4710% Now we start on the table-cell hooks. The left hook starts a box which
4711% will capture the cell's text and natural width. We add the hook to the
4712% rule list as well, so that we can colour the bits in |\vgap|s correctly.
4713%
4714% \begin{macrocode}
4715\tab@addhookbefore\tab@lefttexthook\tab@colourleft
4716\tab@addhookbefore\tab@leftruletexthook\tab@colourleft
4717\def\tab@colourleft{%
4718 \global\let\tab@cellcolour\@empty%
4719 \global\let\tab@colouroverhangs\@empty%
4720 \setbox\z@\hbox\bgroup\color@begingroup%
4721}
4722% \end{macrocode}
4723%
4724% \end{macro}
4725%
4726% \begin{macro}{\tab@colourright}
4727%
4728% The right hook will insert an appropriate rule behind the cell and
4729% retypeset the cell contents over the top. Note that the stretch in a table
4730% cell is exactly 1\,fil. Because we add (leaders) and subtract (negative
4731% |\hskip|) 1\,fil, we retain this stretch exactly. Don't bother unless
4732% there's actually some colouring.
4733%
4734% \begin{macrocode}
4735\tab@addhookafter\tab@righttexthook\tab@colourright
4736\tab@addhookafter\tab@rightruletexthook\tab@colourright
4737\def\tab@colourright{%
4738 \color@endgroup\egroup%
4739 \color@begingroup%
4740 \global\tab@colourfalse%
4741 \tab@cellcolour\tab@rowcolour%
4742 \dimen@\z@\dimen\tw@\z@\tab@colouroverhangs%
4743 \iftab@colour%
4744 \skip@\wd\z@\advance\skip@\z@\@plus1fil%
4745 \skip\tw@\skip@%
4746 \kern-\dimen@%
4747 \advance\skip\tw@\dimen@%
4748 \advance\skip\tw@\dimen\tw@%
4749 \leaders\vrule\hskip\skip\tw@%
4750 \kern-\dimen\tw@%
4751 \hskip-\skip@%
4752 \fi%
4753 \color@endgroup%
4754 \unhbox\z@%
4755}
4756% \end{macrocode}
4757%
4758% \end{macro}
4759%
4760%
4761% \subsection{Coloured rules}
4762%
4763% We hook ourselves onto the rule-parameters edifice. This is rather
4764% straightforward.
4765%
4766% \begin{macrocode}
4767\tab@addhookafter\tab@rp@inithook{%
4768 \let\tab@rulecolour\@empty%
4769 \let\tab@rulecolourmodel\@empty%
4770}
4771\mkdef{mdwtab:rule}{colour}{\tab@setrulecolour{#1}}
4772\mkdef{mdwtab:rule}{colourmodel}{\tab@setrulecolourmodel{#1}}
4773\mkdef{mdwtab:rule}{color}{\tab@setrulecolour{#1}}
4774\mkdef{mdwtab:rule}{colormodel}{\tab@setrulecolourmodel{#1}}
4775\mkdef{mdwtab:rule}{nocolour}*{\let\tab@rulecolour\@empty}
4776\mkdef{mdwtab:rule}{nocolor}*{\let\tab@rulecolour\@empty}
4777\mkdef{mdwtab:rule}{nocolourmodel}*{\let\tab@rulecolourmodel\@empty}
4778\mkdef{mdwtab:rule}{nocolormodel}*{\let\tab@rulecolourmodel\@empty}
4779\def\tab@setrulecolour#1{%
4780 \def\tab@rulecolour{\color\tab@rulecolourmodel{#1}}%
4781}
4782\def\tab@setrulecolourmodel#1{\def\tab@rulecolourmodel{[#1]}}
4783\tab@addhookafter\tab@rp@sethook{\tab@rulecolour}
4784% \end{macrocode}
4785%
4786%
4787% \subsection{Other stuff}
4788%
4789% \begin{macro}{\ifmod}
4790%
4791% \syntax{"\\ifmod{"$x$"}{"$m$"}{"y"}{"<yes>"}{"<no>"}"} -- if $x \bmod m =
4792% y$ then do \<yes>; otherwise do \<no>.
4793%
4794% \begin{macrocode}
4795\def\ifmod#1#2#3{%
4796 \begingroup%
4797 \@tempcnta#1%
4798 \@tempcntb#2%
4799 \count@\@tempcnta%
4800 \divide\count@\@tempcntb%
4801 \multiply\count@\@tempcntb%
4802 \advance\@tempcnta-\count@%
4803 \count@#3\relax%
4804 \ifnum\@tempcnta=\count@\endgroup\expandafter\@firstoftwo%
4805 \else\endgroup\expandafter\@secondoftwo\fi%
4806}
4807% \end{macrocode}
4808%
4809% \end{macro}
4810%
4811% Done.
4812%
4813% \begin{macrocode}
4814%</colour>
4815% \end{macrocode}
4816%
4817%^^A-------------------------------------------------------------------------
86f6a31e 4818% \section{Implementation of \package{mathenv}}
4819%
4820%
4821% This is in a separate package, mainly to avoid wasting people's memory.
4822%
4823% \begin{macrocode}
4824%<*mathenv>
4825% \end{macrocode}
4826%
4827%
4828% \subsection{Options handling}
4829%
4830% We need to be able to cope with \textsf{fleqn} and \textsf{leqno} options.
4831% This will adjust our magic modified \env{eqnarray} environment
4832% appropriately.
4833%
4834% \begin{macrocode}
4835\newif\if@fleqn
4836\newif\if@leqno
4837\DeclareOption{fleqn}{\@fleqntrue}
4838\DeclareOption{leqno}{\@leqnotrue}
4839\ProcessOptions
4840% \end{macrocode}
4841%
4842% We use the \package{mdwtab} package for all its nice table handling things.
4843% (Oh, and to inflict it on users who want to do nice equations and don't
4844% care about our tables.)
4845%
4846% \begin{macrocode}
4847\RequirePackage{mdwtab}
4848% \end{macrocode}
4849%
4850%
4851% \subsection{Some useful registers}
4852%
4853% The old \LaTeX\ version puts the equation numbers in by keeping a count of
4854% where it is in the alignment. Since I don't know how may columns there are
4855% going to be, I'll just use a switch in the preamble to tell me to stop
4856% tabbing.
4857%
4858% \begin{macrocode}
4859\newif\if@eqalast
4860% \end{macrocode}
4861%
4862% Now define some useful length parameters. First allocate them:
4863%
4864% \begin{macrocode}
4865\newskip\eqaopenskip
4866\newskip\eqacloseskip
4867\newskip\eqacolskip
4868\newskip\eqainskip
4869\newskip\splitleft
4870\newskip\splitright
4871% \end{macrocode}
4872%
4873% Now assign some default values. Users can play with these if they really
4874% want although I can't see the point myself.
4875%
4876% \begin{macrocode}
4877\AtBeginDocument{%
4878 \eqacloseskip\@centering%
4879 \eqacolskip1.5em\@plus\@m\p@
4880 \eqainskip\z@%
4881 \if@fleqn%
4882 \eqaopenskip\mathindent%
4883 \splitleft\mathindent\relax%
4884 \splitright\mathindent\@minus\mathindent\relax%
4885 \else%
4886 \eqaopenskip\@centering%
4887 \splitleft2.5em\@minus2.5em%
4888 \splitright\splitleft%
4889 \fi%
4890 \relax%
4891}
4892% \end{macrocode}
4893%
4894%
4895% \subsection{A little display handling}
4896%
4897% I'm probably going a little far here, and invading territory already
4898% claimed by the \package{amsmath} stuff (and done a good deal better than
4899% I can be bothered to do), but just for completeness, this is how we handle
4900% attempts to put displays inside other displays without screwing up the
4901% spacing.
4902%
4903% \begin{macro}{\dsp@startouter}
4904%
4905% This is how we start an outermost display. It's fairly easy really. We
4906% make |\dsp@start| start an inner display, and make |\dsp@end| close the
4907% outer display.
4908%
4909% \begin{macrocode}
4910\def\dsp@startouter{%
4911 \let\dsp@end\dsp@endouter%
4912 $$%
4913}
4914% \end{macrocode}
4915%
4916% \end{macro}
4917%
4918% \begin{macro}{\dsp@endouter}
4919%
4920% Ending the outer display is utterly trivial.
4921%
4922% \begin{macrocode}
4923\def\dsp@endouter{$$}
4924% \end{macrocode}
4925%
4926% \end{macro}
4927%
4928% \begin{macro}{\dsp@startinner}
4929%
4930% Starting inner displays is done in a vbox (actually I choose |\vbox| or
4931% |\vtop| depending on the setting of \textsf{leqno} to put the equation
4932% number the right way round).
4933%
4934% \begin{macrocode}
4935\def\dsp@startinner{%
4936 \let\dsp@end\dsp@endinner%
4937 \if@fleqn\kern-\mathindent\fi%
4938 \if@leqno\vtop\else\vtop\fi\bgroup%
4939}
4940% \end{macrocode}
4941%
4942% \end{macro}
4943%
4944% \begin{macro}{\dsp@endinner}
4945%
4946% Ending an inner display is also really easy.
4947%
4948% \begin{macrocode}
4949\def\dsp@endinner{\egroup}
4950% \end{macrocode}
4951%
4952% \end{macro}
4953%
4954% \begin{macro}{\dsp@start}
4955%
4956% This is what other bits of code uses to start displays. It's one of the
4957% start macros up above, and outer by default.
4958%
4959% \begin{macrocode}
4960\def\dsp@start{%
4961 \ifmmode%
4962 %\ifinner\mth@err@mdsp\fi%
4963 \expandafter\dsp@startinner%
4964 \else%
4965 \ifhmode\ifinner\mth@err@hdsp\fi\fi%
4966 \expandafter\dsp@startouter%
4967 \fi%
4968}
4969% \end{macrocode}
4970%
4971% \end{macro}
4972%
4973% \begin{macro}{\dsp@tabpause}
4974%
4975% This sets up the correct pre- and postambles for the |\tabpause| macro in
4976% maths displays. This is fairly simple stuff.
4977%
4978% \begin{macrocode}
4979\def\dsp@tabpause{%
4980 \def\tab@startpause%
4981 {\penalty\postdisplaypenalty\vskip\belowdisplayskip}%
4982 \def\tab@endpause%
4983 {\penalty\predisplaypenalty\vskip\abovedisplayskip}%
4984}
4985% \end{macrocode}
4986%
4987% \end{macro}
4988%
4989%
4990% \subsection{The \env{eqnarray} environment}
4991%
4992% We allow the user to play with the style if this is really wanted. I dunno
4993% why, really. Maybe someone wants very small alignments.
4994%
4995% \begin{macrocode}
4996\let\eqastyle\displaystyle
4997% \end{macrocode}
4998%
4999% \subsubsection{The main environments}
5000%
5001% \begin{environment}{eqnarray}
5002% \begin{environment}{eqnarray*}
5003%
5004% We define the toplevel commands here. They just add in default arguments
5005% and then call |\@eqnarray| with a preamble string. We handle equation
5006% numbers by setting up a default (|\eqa@defnumber|) which is put into
5007% the final column. At the beginning of each row, we globally |\let|
5008% |\eqa@number| equal to |\eqa@defnumber|. The |\eqnumber| macro just
5009% changes |\eqa@number| as required. Since |\eqa@number| is changed globally
5010% we must save it in this environment.
5011%
5012% First, we must sort out the optional arguments and things. This is really
5013% easy. The only difference between the starred and non-starred environments
5014% is the default definition of |\eqa@defnumber|.
5015%
5016% \begin{macrocode}
5017\def\eqnarray{%
5018 \eqnarray@i\eqa@eqcount%
5019}
5020\@namedef{eqnarray*}{\eqnarray@i{}}
7abe48b3 5021\def\eqnarray@i#1{\@testopt{\eqnarray@ii{#1}}{rcl}}
86f6a31e 5022% \end{macrocode}
5023%
5024% Right. Now for the real work. The first argument is the default numbering
5025% tokens; the second is the preamble string.
5026%
5027% \begin{macrocode}
5028\def\eqnarray@ii#1[#2]{%
5029% \end{macrocode}
5030%
5031% Set up the equation counter and labels correctly.
5032%
5033% \medskip\par\noindent|\begin{rant}|\par
5034% The hacking with |\@currentlabel| is here because (in the author's opinion)
5035% \LaTeX's |\refstepcounter| macro is broken. It's currently defined as
5036% \begin{listing}
5037%\def\refstepcounter#1{%
5038% \stepcounter{#1}%
5039% \protected@edef\@currentlabel%
5040% {\csname p@#1\endcsname\csname the#1\endcsname}%
5041%}
5042% \end{listing}
5043% which means that the current label gets `frozen' as soon as you do the
5044% counter step. By redefining the macro as
5045% \begin{listing}
5046%\def\refstepcounter#1{%
5047% \stepcounter{#1}%
5048% \edef\@currentlabel{%
5049% \expandafter\noexpand\csname p@#1\endcsname%
5050% \expandafter\noexpand\csname the#1\endcsname%
5051% }%
5052%}
5053% \end{listing}
5054% these sorts of problems would be avoided, without any loss of functionality
5055% or compatibility that I can see.
5056% \par\noindent|\end{rant}|\par
5057%
5058% \begin{macrocode}
5059 \stepcounter{equation}%
5060 \def\@currentlabel{\p@equation\theequation}%
5061% \end{macrocode}
5062%
5063% The next step is to set up the numbering. I must save the old numbering
5064% so I can restore it later (once in the alignment, I must assign these
5065% things globally).
5066%
5067% \begin{macrocode}
5068 \let\eqa@oldnumber\eqa@number%
5069 \def\eqa@defnumber{#1}%
5070 \global\let\eqa@number\eqa@defnumber%
5071% \end{macrocode}
5072%
5073% The |\if@eqalastfalse| switch is false everywhere except when we're in the
5074% final column.
5075%
5076% \begin{macrocode}
5077 \@eqalastfalse%
5078% \end{macrocode}
5079%
5080% Remove the |\mathsurround| kerning, since it will look very odd inside
5081% the display. We have our own spacing parameters for configuring these
5082% things, so |\mathsurround| is unnecessary.
5083%
5084% \begin{macrocode}
5085 \m@th%
5086% \end{macrocode}
5087%
5088% Time to parse the preamble string now. I must choose the correct column
5089% set, initialise the preamble parser and set up the various macros. The%
5090% extra `|@{\tabskip\eqacloseskip}|' item sets up the tabskip glue to centre
5091% the alignment properly.
5092%
5093% \begin{macrocode}
5094 \colset{eqnarray}%
5095 \tab@initread%
5096 \def\tab@tabtext{&\tabskip\z@skip}%
5097 \tab@preamble{\tabskip\z@skip}%
5098 \tab@readpreamble{#2@{\tabskip\eqacloseskip}}%
5099 \dsp@tabpause%
5100% \end{macrocode}
5101%
5102% Now for some final setting up. The column separation is set from the
5103% user's parameter, the |\everycr| tokens are cleared, and I set up the
5104% newline command appropriately.
5105%
5106% \begin{macrocode}
5107 \col@sep.5\eqainskip%
5108 \everycr{}%
5109 \let\\\@eqncr%
5110% \end{macrocode}
5111%
5112% Now start a maths display and do the alignment. Set up the left hand
5113% tabskip glue to centre the alignment, and do the actual alignment.
5114% The preamble used is mainly that generated from the user's string, although
5115% the stuff at the end is how we set up the equation number -- it repeats
5116% appropriately so we can always find it.
5117%
5118% \begin{macrocode}
5119 \dsp@start%
5120 \tabskip\eqaopenskip%
5121 \halign to\displaywidth\expandafter\bgroup%
5122 \the\tab@preamble%
5123 &&\eqa@lastcol\hb@xt@\z@{\hss##}\tabskip\z@\cr%
5124}
5125% \end{macrocode}
5126%
5127% Now for the end of the environment. This is really easy. Set the final
5128% equation number, close the |\halign|, tidy up the equation counter (it's
5129% been stepped once too many times) and close the display.
5130%
5131% \begin{macrocode}
5132\def\endeqnarray{%
5133 \eqa@eqnum%
5134 \egroup%
5135 \dsp@end%
5136 \global\let\eqa@number\eqa@oldnumber%
5137 \global\@ignoretrue%
5138 \global\advance\c@equation\m@ne%
5139}
5140\expandafter\let\csname endeqnarray*\endcsname\endeqnarray
5141% \end{macrocode}
5142%
5143% \end{environment}
5144% \end{environment}
5145%
5146% Now we can define the column types.
5147%
5148% \begin{macrocode}
5149\colpush{eqnarray}
5150% \end{macrocode}
5151%
5152% Note the positioning of ord atoms in the stuff below. This will space out
5153% relations and binops correctly when they occur at the edges of columns, and
5154% won't affect ord atoms at the edges, because ords pack closely.
5155%
5156% First the easy ones. Just stick |\hfil| in the right places and
5157% everything will be all right.
5158%
5159% \begin{macrocode}
5160\coldef r{\tabcoltype{\hfil$\eqastyle}{{}$}}
5161\coldef c{\tabcoltype{\hfil$\eqastyle{}}{{}$\hfil}}
5162\coldef l{\tabcoltype{$\eqastyle{}}{$\hfil}}
5163\coldef x{\tabcoltype{\if@fleqn\else\hfil\fi$\eqastyle}{$\hfil}}
5164% \end{macrocode}
5165%
5166% Now for the textual ones. This is also fairly easy.
5167%
5168% \begin{macrocode}
5169\collet T [tabular]T
5170% \end{macrocode}
5171%
5172% Sort of split types of equations. I mustn't use |\rlap| here, or
5173% everything goes wrong -- |\\| doesn't get noticed by \TeX\ in the same way
5174% as |\cr| does.
5175%
5176% \begin{macrocode}
5177\coldef L{\tabcoltype{\hb@xt@2em\bgroup$\eqastyle}{$\hss\egroup}}
5178% \end{macrocode}
5179%
5180% The \lit{:} column type is fairly simple.
5181%
5182% \begin{macrocode}
5183\coldef :{\tabspctype{\tabskip\eqacolskip}}
5184\coldef q{\tabspctype{\quad}}
5185% \end{macrocode}
5186%
5187% The other column types just insert given text in an appropriate way.
5188%
5189% \begin{macrocode}
5190\collet > [tabular]>
5191\collet < [tabular]<
5192\collet * [tabular]*
5193\collet @ [tabular]@
5194% \end{macrocode}
5195%
5196% Finally, the magical `|\magic|' column type, which sets the equation
5197% number. We set up the |\tabskip| glue properly, tab on, and set the flag
5198% which marks the final column. The |\eqa@lastcol| command is there to
5199% raise an error if the user tabs over to this column. I'll temporarily
5200% redefine it to |\@eqalasttrue| when I enter this column legitimately.
5201% The extra magical bits here will make the final column repeat, so that we
5202% can find it if necessary. Well is this column type named.
5203%
5204% That's it. We can return to normal now.
5205%
5206% \begin{macrocode}
5207\colpop
5208% \end{macrocode}
5209%
5210% \subsubsection{Newline codes}
5211%
5212% Newline sequences (|\\|) get turned into calls of |\@eqncr|. The job is
5213% fairly simple, really.
5214%
5215% \begin{macrocode}
5216\def\@eqncr{\tab@cr\eqacr@i\interdisplaylinepenalty\@M}%
5217\def\eqacr@i#1#2{%
5218 \eqa@eqnum%
5219 \noalign{\penalty#2\vskip\jot\vskip#1}%
5220}
5221% \end{macrocode}
5222%
5223% \subsubsection{Setting equation numbers}
5224%
5225% \begin{macro}{\eqa@eqpos}
5226%
5227% Before we start, we need to generalise the flush-left number handling bits.
5228% The macro |\eqa@eqpos| will put its argument in the right place.
5229%
5230% \begin{macrocode}
5231\if@leqno
5232 \def\eqa@eqpos#1{%
5233 \hb@xt@.01\p@{}\rlap{\normalfont\normalcolor\hskip-\displaywidth#1}%
5234 }
5235\else
5236 \def\eqa@eqpos#1{\normalfont\normalcolor#1}
5237\fi
5238% \end{macrocode}
5239%
5240% \end{macro}
5241%
5242% \begin{macro}{\eqa@eqnum}
5243%
5244% Here we typeset an equation number in roughly the right place. First I'll
5245% redefine |\eqa@lastcol| so that it tells me I'm in the right place, and
5246% start a loop to find that place.
5247%
5248% \begin{macrocode}
5249\def\eqa@eqnum{%
5250 \global\let\eqa@lastcol\@eqalasttrue%
5251 \eqa@eqnum@i%
5252}
5253% \end{macrocode}
5254%
5255% Now for the loop. The |\relax| here is absolutely vital -- it starts the
5256% table column, inserting useful tokens like `|\eqa@lastcol|' which tell
5257% me where I am in the alignment. Then, if I've reached the end, I can
5258% typeset the equation number; otherwise I go off into another macro and
5259% step on to the next column.
5260%
5261% \begin{macrocode}
5262\def\eqa@eqnum@i{%
5263 \relax%
5264 \if@eqalast%
5265 \expandafter\eqa@eqnum@ii%
5266 \else%
5267 \expandafter\eqa@eqnum@iii%
5268 \fi%
5269}
5270\def\eqa@eqnum@ii{%
5271 \eqa@eqpos\eqa@number%
5272 \global\let\eqa@number\eqa@defnumber%
5273 \global\let\eqa@lastcol\eqa@@lastcol%
5274 \cr%
5275}
5276\def\eqa@eqnum@iii{&\eqa@eqnum@i}
5277% \end{macrocode}
5278%
5279% \end{macro}
5280%
5281% \begin{macro}{\eqa@lastcol}
5282%
5283% This is used as a marker for the final column in an \env{eqnarray}
5284% environment. By default it informs the user that they've been very
5285% silly and swallows the contents of the column. I'll redefine it to
5286% something more useful at appropriate times, and then turn it back again.
5287%
5288% \begin{macrocode}
5289\def\eqa@@lastcol{\mth@err@number\setbox\z@}
5290\let\eqa@lastcol\eqa@@lastcol
5291% \end{macrocode}
5292%
5293% \end{macro}
5294%
5295% \subsubsection{Numbering control}
5296%
5297% \begin{macro}{\eqnumber}
5298%
5299% The |\eqnumber| command sets the equation number on the current equation.
5300% This is really easy, actually.
5301%
5302% \begin{macrocode}
5303\newcommand\eqnumber[1][\eqa@eqcount]{\gdef\eqa@number{#1}}
5304% \end{macrocode}
5305%
5306% \end{macro}
5307%
5308% \begin{macro}{\eqa@eqcount}
5309%
5310% This is how a standard equation number is set, stepping the counter and
5311% all. It's really easy and obvious.
5312%
5313% \begin{macrocode}
5314\def\eqa@eqcount{(\theequation)\global\advance\c@equation\@ne}
5315% \end{macrocode}
5316%
5317% \end{macro}
5318%
5319% \begin{macro}{\nonumber}
5320%
5321% The \LaTeX\ |\nonumber| command could be defined by saying
5322% \begin{listing}
5323%\renewcommand{\nonumber}{\eqnumber[]}
5324% \end{listing}
5325% but I'll be slightly more efficient and redefine |\eqa@number| directly.
5326%
5327% \begin{macrocode}
5328\def\nonumber{\global\let\eqa@number\@empty}
5329% \end{macrocode}
5330%
5331% \end{macro}
5332%
5333% \subsubsection{The \env{eqnalign} environment}
5334%
5335% As a sort of companion to \env{eqnarray}, here's an environment which does
5336% similar things inside a box, rather than taking up the whole display width.
5337% It uses the same column types that we've already created, so there should
5338% be no problems.
5339%
5340% \begin{environment}{eqnalign}
5341%
5342% First, sort out some simple things like optional arguments.
5343%
5344% \begin{macrocode}
7abe48b3 5345\def\eqnalign{\@testopt\eqnalign@i{rcl}}
5346\def\eqnalign@i[#1]{\@testopt{\eqnalign@ii{#1}}c}
86f6a31e 5347% \end{macrocode}
5348%
5349% Now we actually do the environment. This is fairly easy, actually.
5350%
5351% \begin{macrocode}
5352\def\eqnalign@ii#1[#2]{%
5353 \let\\\eqn@cr%
5354 \colset{eqnarray}%
5355 \tab@initread%
5356 \def\tab@tabtext{&\tabskip\z@skip}%
5357 \tabskip\z@skip%
5358 \col@sep.5\eqainskip%
5359 \tab@readpreamble{#1}%
5360 \everycr{}%
5361 \if#2t\vtop\else%
5362 \if#2b\vbox\else%
5363 \vcenter%
5364 \fi%
5365 \fi%
5366 \bgroup%
5367 \halign\expandafter\bgroup\the\tab@preamble\cr%
5368}
5369% \end{macrocode}
5370%
5371% Finishing the environment is even simpler.
5372%
5373% \begin{macrocode}
5374\def\endeqnalign{%
5375 \crcr%
5376 \egroup%
5377 \egroup%
5378}
5379% \end{macrocode}
5380%
5381% \end{environment}
5382%
5383% \begin{macro}{\eqn@cr}
5384%
5385% Newlines are really easy here.
5386%
5387% \begin{macrocode}
5388\def\eqn@cr{\tab@cr\eqn@cr@i{}{}}
5389\def\eqn@cr@i#1{\cr\noalign{\vskip\jot\vskip#1}\@gobble}
5390% \end{macrocode}
5391%
5392% \end{macro}
5393%
5394%
5395% \subsection{Simple multiline equations}
5396%
5397% As a sort of example and abbreviation, here's a multiline display
5398% environment which just centres everything.
5399%
5400% \begin{environment}{eqlines}
5401%
5402% We just get |\eqnarray| to do everything for us. This is really easy.
5403%
5404% \begin{macrocode}
5405\def\eqlines{\eqnarray[x]}
5406\let\endeqlines\endeqnarray
5407% \end{macrocode}
5408%
5409% \end{environment}
5410%
5411% \begin{environment}{eqlines*}
5412%
5413% There's a $*$ version which omits numbers. This is easy too. Lots of
5414% hacking with expansion here to try and reduce the number of tokens being
5415% used. Is it worth it?
5416%
5417% \begin{macrocode}
5418\expandafter\edef\csname eqlines*\endcsname{%
5419 \expandafter\noexpand\csname eqnarray*\endcsname[x]%
5420}
5421\expandafter\let\csname endeqlines*\expandafter\endcsname
5422 \csname endeqnarray*\endcsname
5423% \end{macrocode}
5424%
5425% \end{environment}
5426%
5427%
5428% \subsection{Split equations}
5429%
5430% Based on an idea from \textit{The \TeX book}, we provide some simple
5431% environments for doing split equations. There's plenty of scope for
5432% improvement here, though.
5433%
5434% \begin{environment}{spliteqn}
5435% \begin{environment}{spliteqn*}
5436%
5437% The only difference between these two is that the $*$-version doesn't put
5438% in an equation number by default (although this behaviour can be
5439% changed by |\eqnumber|).
5440%
5441% The fun here mainly concerns putting in the equation number at the right
5442% place -- for |leqno| users, we need to put the number on the first line;
5443% otherwise we put it on the last line.
5444%
5445% The way we handle this is to have two macros, |\\| (which clearly does
5446% all the user line breaks) and |\seq@lastcr| which is used at the end of
5447% the environment to wrap everything up. The |\seq@eqnocr| macro puts an
5448% equation number on the current line and then does a normal |\\|. It also
5449% resets |\\| and |\seq@lastcr| so that they don't try to put another
5450% equation number in. This must be done globally, although anyone who tries
5451% to nest maths displays will get what they deserve.
5452%
5453% For the non-$*$ environment, then, we need to step the equation counter,
5454% and set |\\| to |\seq@cr| or |\seq@eqnocr| as appropriate for the setting
5455% of the |leqno| flag -- |\seq@lastcr| always gets set to put an equation
5456% number in (because it will be reset if the number actually gets done
5457% earlier -- this catches stupid users trying to put a single row into
5458% a split environment).
5459%
5460% \begin{macrocode}
5461\def\spliteqn{%
5462 \let\eqa@oldnumber\eqa@number%
5463 \global\let\eqa@number\eqa@eqcount%
5464 \spliteqn@i%
5465}
5466% \end{macrocode}
5467%
5468% For the $*$ variant, we don't need to bother with equation numbering, so
5469% this is really easy.
5470%
5471% \begin{macrocode}
5472\@namedef{spliteqn*}{%
5473 \let\eqa@oldnumber\eqa@number%
5474 \gdef\eqa@number{}%
5475 \spliteqn@i%
5476}
5477% \end{macrocode}
5478%
5479% Ending the environments is easy. Most of the stuff here will be described
5480% later.
5481%
5482% \begin{macrocode}
5483\def\endspliteqn{%
5484 \hfilneg\seq@lastcr%
5485 \egroup%
5486 \dsp@end%
5487 \global\let\eqa@number\eqa@oldnumber%
5488 \global\advance\c@equation\m@ne%
b57a27e1 5489 \global\@ignoretrue%
86f6a31e 5490}
5491\expandafter\let\csname endspliteqn*\endcsname\endspliteqn
5492% \end{macrocode}
5493%
5494% \end{environment}
5495% \end{environment}
5496%
5497% \begin{macro}{\spliteqn@i}
5498%
5499% Here we handle the full display splits. Start a maths display, and make
5500% each row of the alignment take up the full display width.
5501%
5502% The macro |\seq@dosplit| does most of the real work for us -- setting up
5503% the alignment and so forth. The template column is interesting. There
5504% are two items glue on both sides of the actual text:
5505%
5506% \begin{itemize}
5507%
5508% \item Some glue which can shrink. This keeps the display from the edges
5509% of the page unless we get a really wide item.
5510%
5511% \item An |\hfil| to do the alignment. By default, this centres the
5512% equations. On the first line, however, we put a leading |\hfilneg|
5513% which cancels the first |\hfil|, making the first row left aligned.
5514% Similarly, at the end, we put an |\hfilneg| after the last equation
5515% to right align the last line.
5516%
5517% \end{itemize}
5518%
5519% We pass this information on as an argument. It's easy really.
5520%
5521% \begin{macrocode}
5522\def\spliteqn@i{%
5523% \end{macrocode}
5524%
5525% First, set up equation numbering properly. See my rant about
5526% |\refstepcounter| above.
5527%
5528% \begin{macrocode}
5529 \stepcounter{equation}%
5530 \def\@currentlabel{\p@equation\theequation}%
5531% \end{macrocode}
5532%
5533% Right; now to sort out the numbering and newline handling. If the number's
5534% meant to be on the first line (for \textsf{leqno} users), then it gets
5535% typeset on the first like; otherwise we just do a normal newline on
5536% all lines except the first. Once |\seq@eqnocr| has done its stuff, it
5537% redefines all the newline handling not to insert another number.
5538%
5539% \begin{macrocode}
5540 \if@leqno%
5541 \global\let\seq@docr\seq@eqnocr%
5542 \else%
5543 \global\let\seq@docr\seq@cr%
5544 \fi%
5545 \global\let\seq@lastcr\seq@eqnocr%
5546% \end{macrocode}
5547%
5548% For my next trick, I'll do some display handling -- start a (possibly
5549% nested) maths display, set up the |\tabpause| macro appropriately, and
5550% set the newline command to do the right thing.
5551%
5552% \begin{macrocode}
5553 \dsp@start%
5554 \dsp@tabpause%
5555 \def\\{\seq@docr}%
5556% \end{macrocode}
5557%
5558% Finally, call another macro to do the remaining bits of setting up.
5559%
5560% \begin{macrocode}
5561 \seq@dosplit%
5562 {\hb@xt@\displaywidth{%
5563 \hskip\splitleft\hfil$\displaystyle##$%
5564 \hfil\hskip\splitright}}%
5565 {\hfilneg}%
5566}
5567% \end{macrocode}
5568%
5569% \end{macro}
5570%
5571% \begin{environment}{subsplit}
5572%
5573% For doing splits in the middle of equations, we provide a similar
5574% environment. Here, we make |\\| just start a new line. We also use
5575% a |\vcenter| rather than a full maths display. The glue items are also
5576% a bit different: we use plain double-quads on each side of the item, and
5577% we need to remove them by hand at the extremities of the environment.
5578%
5579% \begin{macrocode}
5580\def\subsplit{%
5581 \let\\\seq@cr%
5582 \vcenter\bgroup%
5583 \seq@dosplit{\hfil\qquad$##$\qquad\hfil}{\hfilneg\hskip-2em}%
5584}
5585% \end{macrocode}
5586%
5587% Ending the environment is fairly easy. We remove the final glue item,
5588% and close the alignment and the vbox.
5589%
5590% \begin{macrocode}
5591\def\endsubsplit{%
5592 \hfilneg\hskip-2em\cr%
5593 \egroup\egroup%
5594}
5595% \end{macrocode}
5596%
5597% \end{environment}
5598%
5599% \begin{macro}{\seq@dosplit}
5600%
5601% Here we do most of the real work. Actually, since the preamble is passed
5602% in as an argument, most of the work is already done. The only thing to
5603% really note is the template for subsequent columns. To stop users putting
5604% in extra columns (which is where we put the equation number) we raise an
5605% error and discard the input in a scratch box register. This template is
5606% repeated infinitely so as to allow us to put the equation number in nicely.
5607% However, the final negative glue item won't work properly, so the equation
5608% will look awful.
5609%
5610% \begin{macrocode}
5611\def\seq@dosplit#1#2{%
5612 \halign\bgroup%
5613 #1&&\mth@err@number\setbox\z@\hbox{##}\cr%
5614 #2\relax%
5615}
5616% \end{macrocode}
5617%
5618% \end{macro}
5619%
5620% \begin{macro}{\seq@eqnocr}
5621%
5622% Here's how we set equation numbers. Since the column provided raises
5623% errors as soon as a token finds its way into it, we start with a |&\omit|.
5624% Then we just put the equation number in a zero-width box. Finally, we
5625% reset the newline commands to avoid putting in more than one equation
5626% number, and do normal newline things.
5627%
5628% \begin{macrocode}
5629\def\seq@eqnocr{%
5630 &\omit%
5631 \hb@xt@\z@{\hss\eqa@eqpos\eqa@number}%
5632 \global\let\seq@docr\seq@cr%
5633 \global\let\seq@lastcr\seq@cr%
5634 \seq@cr%
5635}
5636% \end{macrocode}
5637%
5638% \end{macro}
5639%
5640% \begin{macro}{\seq@cr}
5641%
5642% Newlines are very easy. We add a |\jot| of extra space, since this is
5643% a nice thing to do.
5644%
5645% \begin{macrocode}
5646\def\seq@cr{\tab@cr\seq@cr@i\interdisplaylinepenalty\@M}
5647\def\seq@cr@i#1#2{\cr\noalign{\penalty#2\vskip\jot\vskip#1}}
5648% \end{macrocode}
5649%
5650% \end{macro}
5651%
5652%
5653% \subsection{Matrix handling}
5654%
5655% There's been a complete and total overhaul of the spacing calculations
5656% for matrices here. The vertical spacing now bears an uncanny similarity
5657% to the rules \TeX\ uses to space out |\atop|-like fractions, the difference
5658% being that you can have more than one column in a matrix. This has the
5659% interesting side-effect that we get an \package{amsmath}-style
5660% sub/superscript environment almost free of charge with the matrix handling
5661% (it just ends up being a script-size single-column matrix).
5662%
5663% What is rather gratifying is that our \env{matrix} environment looks
5664% rather nicer than \package{amsmath}'s (which is based directly on
5665% \env{array}, giving it nasty restrictions on the numbers of columns and
5666% so on); in particular, the version here gives the `correct' result for
5667% Knuth's exercise~18.42 (which states categorically that a |\smallskip|
5668% should be placed between the rows of the big matrix).
5669%
5670% The reason the interrow space doesn't come out in the AMS version is
5671% that \env{array} inserts extra vertical space by extending the depth of
5672% the final row using a strut: the big matrix already extends deeper than
5673% this, so the strut doesn't make any difference. If the space was added
5674% by |\hlx{s[\smallskipamount]}| instead of the |\\| command, things would
5675% be different.
5676%
5677% \begin{figure}
5678%
5679% ^^A This is essentially what amsmath (version 1.2b) does. The real
5680% ^^A implementation requires a counter MaxMatrixCols, and has fewer braces:
5681% ^^A that's all the difference. Oh, and I turn off \arrayextrasep here,
5682% ^^A since amsmath doesn't expect it to be there (accurate emulation, see?)
5683% ^^A and I've used \hspace instead of \hskip since everything else is
5684% ^^A `proper' LaTeX stuff.
5685%
5686% \newenvironment{ams-pmatrix}{^^A
5687% \setlength{\arrayextrasep}{0pt}^^A
5688% \left(^^A
5689% \hspace{-\arraycolsep}^^A
5690% \begin{array}{*{10}{c}}^^A
5691% }{^^A
5692% \end{array}^^A
5693% \hspace{-\arraycolsep}^^A
5694% \right)^^A
5695% }
5696%
5697% \begin{demo}{Exercise 18.42 from \emph{The \TeX book}}
5698%\newcommand{\domatrix}[1]{
5699% \def\mat##1
5700% {\begin{#1}##1\end{#1}}
5701% \[ \begin{#1}
5702% \mat{a & b \\ c & d} &
5703% \mat{e & f \\ g & h}
5704% \\[\smallskipamount]
5705% 0 &
5706% \mat{i & j \\ k & l}
5707% \end{#1}
5708% \]
5709%}
5710%\domatrix{pmatrix}
5711%\domatrix{ams-pmatrix}
5712% \end{demo}
5713%
5714% \end{figure}
5715%
5716% \begin{environment}{genmatrix}
5717%
5718% The first job is to store my maths style and font away, because I'll be
5719% needing it lots later.
5720%
5721% \begin{macrocode}
5722\def\genmatrix#1#2#3#4#5{%
5723 \let\mat@style#1%
5724 \ifx#2\scriptstyle%
5725 \let\mat@font\scriptfont%
5726 \else\ifx#2\scriptscriptstyle%
5727 \let\mat@font\scriptscriptfont%
5728 \else%
5729 \let\mat@font\textfont%
5730 \fi\fi%
5731% \end{macrocode}
5732%
5733% Now to cope with inserted text. This is easy.
5734%
5735% \begin{macrocode}
5736 \ifx\mat@style\scriptstyle%
5737 \let\mat@textsize\scriptsize%
5738 \else\ifx\mat@style\scriptscriptstyle%
5739 \let\mat@textsize\scriptscriptsize%
5740 \else%
5741 \let\mat@textsize\relax%
5742 \fi\fi%
5743% \end{macrocode}
5744%
5745% Now for some fun. I'll remember how to start and end the matrix in a
5746% couple of macros |\mat@left| and |\mat@right|. I haven't yet worked out
5747% exactly what needs to be in |\mat@right| yet, though, so I'll build that
5748% up in a scratch token list while I'm making my mind up.
5749%
5750% Initially, I want to open a group (to trap the style changes), set the
5751% maths style (to get the right spacing), insert the left delimiter, insert
5752% some spacing around the matrix, and start a centred box. The ending just
5753% closes all the groups and delimiters I opened.
5754%
5755% \begin{macrocode}
5756 \def\mat@left{\bgroup\mat@style\left#4#3\vcenter\bgroup}%
5757 \toks@{\egroup#3\right#5\egroup}%
5758% \end{macrocode}
5759%
5760% Now comes a slightly trickier bit. If the maths style is script or
5761% scriptscript, then I need to raise the box by a little bit to make it look
5762% really good. The right amount is somewhere around \smallf 3/4\,pt, I
5763% think, so that's what I'll use.
5764%
5765% \begin{macrocode}
5766 \@tempswatrue%
5767 \ifx\mat@style\displaystyle\else\ifx\mat@style\textstyle\else%
5768 \@tempswafalse%
5769 \setbox\z@\hbox\bgroup$%
5770 \toks@\expandafter{\the\toks@$\m@th\egroup\raise.75\p@\box\z@}%
5771 \fi\fi%
5772% \end{macrocode}
5773%
5774% If I'm not in maths mode right now, then I should enter maths mode, and
5775% remember to leave it later.
5776%
5777% \begin{macrocode}
5778 \if@tempswa\ifmmode\else%
5779 $\m@th%
5780 \toks@\expandafter{\the\toks@$}%
5781 \fi\fi%
5782% \end{macrocode}
5783%
5784% Now I've sorted out how to end the environment properly, so I can set up
5785% the macro, using |\edef|.
5786%
5787% \begin{macrocode}
5788 \edef\mat@right{\the\toks@}%
5789% \end{macrocode}
5790%
5791% Now see if there's an optional argument. If not, create lots of centred
5792% columns.
5793%
5794% \begin{macrocode}
7abe48b3 5795 \@testopt\genmatrix@i{[c}%
86f6a31e 5796}
5797% \end{macrocode}
5798%
5799% Now to sort out everything else.
5800%
5801% \begin{macrocode}
5802\def\genmatrix@i[#1]{%
5803% \end{macrocode}
5804%
5805% Some initial setting up: choose the correct column set, and set up some
5806% variables for reading the preamble.
5807%
5808% \begin{macrocode}
5809 \colset{matrix}%
5810 \tab@initread%
5811% \end{macrocode}
5812%
5813% Now comes some of the tricky stuff. The space between columns should be
5814% 12\,mu (by trial and error). We put the space in a box so we can measure
5815% it in the correct mathstyle.
5816%
5817% \begin{macrocode}
5818 \setbox\z@\hbox{$\mat@style\mskip12mu$}%
5819 \edef\tab@tabtext{&\kern\the\wd\z@}%
5820 \tab@readpreamble{#1}%
5821% \end{macrocode}
5822%
5823% Now we need to decide how to space out the rows. The code here is based
5824% on the information in appendix~G of \emph{The \TeX book}: I think it'd be
5825% nice if my matrices were spaced out in the same way as normal fractions
5826% (particularly |\choose|y things). The standard |\baselineskip| and
5827% |\lineskip| parameters come in really handy here.
5828%
5829% The parameters vary according to the size of the text, so I need to see
5830% if we have scriptsize or less, or not. The tricky |\if| sorts this out.
5831%
5832% \begin{macrocode}
5833 \if1\ifx\mat@style\scriptstyle1\else%
5834 \ifx\mat@style\scriptscriptstyle1\else0\fi\fi%
5835 \baselineskip\fontdimen10\mat@font\tw@%
5836 \advance\baselineskip\fontdimen12\mat@font\tw@%
5837 \lineskip\thr@@\fontdimen8\mat@font\thr@@%
5838 \else%
5839 \baselineskip\fontdimen8\mat@font\tw@%
5840 \advance\baselineskip\fontdimen11\mat@font\tw@%
5841 \lineskip7\fontdimen8\mat@font\thr@@%
5842 \fi%
5843 \lineskiplimit\lineskip%
5844% \end{macrocode}
5845%
5846% Now actually set up for the alignment. Assign |\\| to the correct value.
5847% Set up the |\tabskip|. Do the appropriate |\mat@left| thing set up above.
5848% And then start the alignment.
5849%
5850% \begin{macrocode}
5851 \let\\\mat@cr%
5852 \tabskip\z@skip%
5853 \col@sep\z@%
5854 \mat@left%
5855 \halign\expandafter\bgroup\the\tab@preamble\tabskip\z@skip\cr%
5856% \end{macrocode}
5857%
5858% Now for a little hack to make the spacing consistent between matrices of
5859% the same height. This comes directly from \PlainTeX. This appears to
5860% make the spacing \emph{exactly} the same as the \TeX\ primitives, oddly
5861% enough.
5862%
5863% \begin{macrocode}
5864 \ifx\mat@font\textfont%
5865 \omit$\mat@style\mathstrut$\cr\noalign{\kern-\baselineskip}%
5866 \fi%
5867}
5868% \end{macrocode}
5869%
5870% Finishing the environment is really easy. We do the spacing hack again
5871% at the bottom, close the alignment and then tidy whatever we started in
5872% |\mat@left|.
5873%
5874% \begin{macrocode}
5875\def\endgenmatrix{%
5876 \crcr%
5877 \ifx\mat@font\textfont%
5878 \omit$\mat@style\mathstrut$\cr\noalign{\kern-\baselineskip}%
5879 \fi%
5880 \egroup%
5881 \mat@right%
5882}
5883% \end{macrocode}
5884%
5885% \end{environment}
5886%
5887% \begin{macro}{\mat@cr}
5888%
5889% Newlines are really easy. The $*$-form means nothing here, so we ignore
5890% it.
5891%
5892% \begin{macrocode}
5893\def\mat@cr{\tab@cr\mat@cr@i{}{}}
5894\def\mat@cr@i#1{\cr\noalign{\vskip#1}\@gobble}
5895% \end{macrocode}
5896%
5897% \end{macro}
5898%
5899% \begin{macro}{\newmatrix}
5900%
5901% This is how we define new matrix environments. It's simple fun with
5902% |\csname| and |\expandafter|.
5903%
5904% \begin{macrocode}
5905\def\newmatrix#1#2{%
5906 \@namedef{#1}{\genmatrix#2}%
5907 \expandafter\let\csname end#1\endcsname\endgenmatrix%
5908}
5909% \end{macrocode}
5910%
5911% \end{macro}
5912%
5913% \begin{environment}{matrix}
5914% \begin{environment}{pmatrix}
5915% \begin{environment}{dmatrix}
5916% \begin{environment}{smatrix}
5917% \begin{environment}{spmatrix}
5918% \begin{environment}{sdmatrix}
5919% \begin{environment}{smatrix*}
5920% \begin{environment}{spmatrix*}
5921% \begin{environment}{sdmatrix*}
5922%
5923% Now we define all the other environments we promised. This is easy.
5924%
5925% \begin{macrocode}
5926\newmatrix{matrix}{{\textstyle}{\textstyle}{\,}{.}{.}}
5927\newmatrix{pmatrix}{{\textstyle}{\textstyle}{\,}{(}{)}}
5928\newmatrix{dmatrix}{{\textstyle}{\textstyle}{\,}}
5929\newmatrix{smatrix}{{\scriptstyle}{\scriptstyle}{}{.}{.}}
5930\newmatrix{spmatrix}{{\scriptstyle}{\scriptstyle}{}{(}{)}}
5931\newmatrix{sdmatrix}{{\scriptstyle}{\scriptstyle}{}}
5932\newmatrix{smatrix*}{{\scriptstyle}{\textstyle}{}{.}{.}}
5933\newmatrix{spmatrix*}{{\scriptstyle}{\textstyle}{}{(}{)}}
5934\newmatrix{sdmatrix*}{{\scriptstyle}{\textstyle}{}}
5935% \end{macrocode}
5936%
5937% \end{environment}
5938% \end{environment}
5939% \end{environment}
5940% \end{environment}
5941% \end{environment}
5942% \end{environment}
5943% \end{environment}
5944% \end{environment}
5945% \end{environment}
5946%
5947% \begin{environment}{script}
5948%
5949% Now for superscripts and subscripts. This is fairly easy, because I
5950% took so much care over the matrix handling.
5951%
5952% \begin{macrocode}
5953\def\script{%
5954 \let\mat@style\scriptstyle%
5955 \def\mat@left{\vcenter\bgroup}%
5956 \def\mat@right{\egroup}%
5957 \let\mat@font\scriptfont%
5958 \let\mat@textsize\scriptsize%
7abe48b3 5959 \@testopt\genmatrix@i c%
86f6a31e 5960}
5961\let\endscript\endgenmatrix
5962% \end{macrocode}
5963%
5964% \end{environment}
5965%
5966% Now define the column types.
5967%
5968% \begin{macrocode}
5969\colpush{matrix}
5970\coldef l{\tabcoltype{\kern\z@$\mat@style}{\m@th$\hfil}}
5971\coldef c{\tabcoltype{\hfil$\mat@style}{\m@th$\hfil}}
5972\coldef r{\tabcoltype{\hfil$\mat@style}{\m@th$}}
5973\coldef T#1{\tab@aligncol{#1}{\begingroup\mat@textsize}{\endgroup}}
5974\collet > [tabular]>
5975\collet < [tabular]<
5976\collet * [tabular]*
5977\collet @ [tabular]@
5978% \end{macrocode}
5979%
5980% The repeating type is more awkward. Things will go wrong if this is
5981% given before the first column, so we must do a whole repeat by hand. We
5982% can tell if we haven't contributed a column yet, since |\tab@column| will
5983% be zero. Otherwise, we fiddle the parser state to start a new column, and
5984% insert the |&| character to make \TeX\ repeat the preamble.
5985%
5986% \begin{macrocode}
5987\coldef {[}{%
5988 \@firstoftwo{%
5989 \ifnum\tab@columns=\z@%
5990 \def\@tempa##1\q@delim{%
5991 \tab@mkpreamble##1[##1\q@delim%
5992 }%
5993 \expandafter\@tempa%
5994 \else%
5995 \tab@setstate\tab@prestate%
5996 \tab@append\tab@preamble{&}%
5997 \expandafter\tab@mkpreamble%
5998 \fi%
5999 }%
6000}
6001% \end{macrocode}
6002%
6003% We're done defining columns now.
6004%
6005% \begin{macrocode}
6006\colpop
6007% \end{macrocode}
6008%
6009%
6010% \subsection{Dots\dots}
6011%
6012% Nothing whatsoever to do with alignments, although vertical and diagonal
6013% dots in small matrices look really silly. The following hacky definitions
6014% work rather better.
6015%
6016% \begin{macro}{\mdw@dots}
6017%
6018% First of all, here's some definitions common to both of the dots macros.
6019% The macro takes as an argument the actual code to draw the dots, passing
6020% it the scaled size of a point in the scratch register |\dimen@|; the
6021% register |\box 0| is set to contain a dot of the appropriate size.
6022%
6023% \begin{macrocode}
6024\def\mdw@dots#1{\ensuremath{\mathpalette\mdw@dots@i{#1}}}
6025\def\mdw@dots@i#1#2{%
6026 \setbox\z@\hbox{$#1\mskip1.8mu$}%
6027 \dimen@\wd\z@%
6028 \setbox\z@\hbox{$#1.$}%
6029 #2%
6030}
6031% \end{macrocode}
6032%
6033% \end{macro}
6034%
6035% \begin{macro}{\vdots}
6036%
6037% I'll start with the easy one. This is a simple translation of the original
6038% implementation.
6039%
6040% \begin{macrocode}
6041\def\vdots{%
6042 \mdw@dots{\vbox{%
6043 \baselineskip4\dimen@%
6044 \lineskiplimit\z@%
6045 \kern6\dimen@%
6046 \copy\z@\copy\z@\box\z@%
6047 }}%
6048}
6049% \end{macrocode}
6050%
6051% \end{macro}
6052%
6053% \begin{macro}{\ddots}
6054%
6055% And I'll end with the other easy one\dots
6056%
6057% \begin{macrocode}
6058\def\ddots{%
6059 \mdw@dots{\mathinner{%
6060 \mkern1mu%
6061 \raise7\dimen@\vbox{\kern7\dimen@\copy\z@}%
6062 \mkern2mu%
6063 \raise4\dimen@\copy\z@%
6064 \mkern2mu%
6065 \raise\dimen@\box\z@%
6066 \mkern1mu%
6067 }}%
6068}
6069% \end{macrocode}
6070%
6071% \end{macro}
6072%
6073%
6074% \subsection{Lucky dip}
6075%
6076% Time to round off with some trivial environments, just to show how easy
6077% this stuff is.
6078%
6079% \begin{environment}{cases}
6080% \begin{environment}{smcases}
6081%
6082% These are totally and utterly trivial.
6083%
6084% \begin{macrocode}
6085\def\cases{\left\{\,\array{@{}lTl@{}}}
6086\def\endcases{\endarray\,\right.}
6087\def\smcases{\left\{\smarray{@{}lTl@{}}}
6088\def\endsmcases{\endsmarray\,\right.}
6089% \end{macrocode}
6090%
6091% \end{environment}
6092% \end{environment}
6093%
6094% \subsection{Error messages}
6095%
6096% Some token saving:
6097%
6098% \begin{macrocode}
6099\def\mth@error{\PackageError{mathenv}}
6100% \end{macrocode}
6101%
6102% Now for the error messages.
6103%
6104% \begin{macrocode}
6105\def\mth@err@number{%
6106 \mth@error{Too many `&' characters found}{%
6107 You've put too many `&' characters in an alignment^^J%
6108 environment (like `eqnarray' or `spliteqn') and wandered^^J%
6109 into trouble. I've gobbled the contents of that column^^J%
6110 and hopefully I can recover fairly easily.%
6111 }%
6112}
6113% \end{macrocode}
6114%
6115% \begin{macrocode}
6116\def\mth@err@mdsp{%
6117 \mth@error{Can't do displays in nondisplay maths mode}{%
6118 You're trying to start a display environment, but you're^^J%
6119 in nondisplay maths mode. The display will appear but^^J%
6120 don't blame me when it looks horrible.%
6121 }%
6122}
6123% \end{macrocode}
6124%
6125% \begin{macrocode}
6126\def\mth@err@hdsp{%
6127 \mth@error{Can't do displays in LR mode}{%
6128 You're trying to start a display environment, but you're^^J%
6129 in LR (restricted horizontal) mode. Everything will go^^J%
6130 totally wrong, so your best bet is to type `X', fix the^^J%
6131 mistake and start again.%
6132 }%
6133}
6134% \end{macrocode}
6135%
6136% \vskip\parskip\vbox{ ^^A The best way I could find of keeping this lot
6137% ^^A together, I'm afraid.
6138% That's all there is. Byebye.
6139%
6140% \begin{macrocode}
6141%</mathenv>
6142% \end{macrocode}
6143% \nopagebreak
6144%
6145% \hfill Mark Wooding, \today
6146% }
6147%
6148% \Finale
6149%
6150\endinput