Debian Codegraph @

texmf-dist/source/latex/mdwtools/mdwmath.dtx

raw
1 % \begin{meta-comment}
2 %
3 % $Id: mdwmath.dtx,v 1.1 1996/11/19 20:53:21 mdw Exp $
4 %
5 % Various nicer mathematical things
6 %
7 % (c) 1996 Mark Wooding
8 %
9 %----- Revision history -----------------------------------------------------
10 %
11 % $Log: mdwmath.dtx,v $
12 % Revision 1.1 1996/11/19 20:53:21 mdw
13 % Initial revision
14 %
15 %
16 % \end{meta-comment}
17 %
18 % \begin{meta-comment} <general public licence>
19 %%
20 %% mdwmath package -- various nicer mathematical things
21 %% Copyright (c) 1996 Mark Wooding
22 %%
23 %% This program is free software; you can redistribute it and/or modify
24 %% it under the terms of the GNU General Public License as published by
25 %% the Free Software Foundation; either version 2 of the License, or
26 %% (at your option) any later version.
27 %%
28 %% This program is distributed in the hope that it will be useful,
29 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
30 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 %% GNU General Public License for more details.
32 %%
33 %% You should have received a copy of the GNU General Public License
34 %% along with this program; if not, write to the Free Software
35 %% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 %%
37 % \end{meta-comment}
38 %
39 % \begin{meta-comment} <Package preamble>
40 %<+package>\NeedsTeXFormat{LaTeX2e}
41 %<+package>\ProvidesPackage{mdwmath}
42 %<+package> [1996/04/11 1.1 Nice mathematical things]
43 %<+oldeqnarray>\NeedsTeXFormat{LaTeX2e}
44 %<+oldeqnarray>\ProvidesPackage{eqnarray}
45 %<+oldeqnarray> [1996/04/11 1.1 Old enhanced eqnarray]
46 % \end{meta-comment}
47 %
48 % \CheckSum{259}
49 % \begin{old-eqnarray}
50 % \CheckSum{484}
51 % \end{old-eqnarray}
52 %% \CharacterTable
53 %% {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
54 %% 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
55 %% Digits \0\1\2\3\4\5\6\7\8\9
56 %% Exclamation \! Double quote \" Hash (number) \#
57 %% Dollar \$ Percent \% Ampersand \&
58 %% Acute accent \' Left paren \( Right paren \)
59 %% Asterisk \* Plus \+ Comma \,
60 %% Minus \- Point \. Solidus \/
61 %% Colon \: Semicolon \; Less than \<
62 %% Equals \= Greater than \> Question mark \?
63 %% Commercial at \@ Left bracket \[ Backslash \\
64 %% Right bracket \] Circumflex \^ Underscore \_
65 %% Grave accent \` Left brace \{ Vertical bar \|
66 %% Right brace \} Tilde \~}
67 %%
68 %
69 % \begin{meta-comment}
70 %
71 %<*driver>
72 \input{mdwtools}
73 \describespackage{mdwmath}
74 % \describespackage{eqnarray}
75 \ignoreenv{old-eqnarray}
76 % \unignoreenv{old-eqnarray}
77 \mdwdoc
78 %</driver>
79 %
80 % \end{meta-comment}
81 %
82 % \section{User guide}
83 %
84 % \subsection{Square root typesetting}
85 %
86 % \DescribeMacro{\sqrt}
87 % The package supplies a star variant of the |\sqrt| command which omits the
88 % vinculum over the operand (the line over the top). While this is most
89 % useful in simple cases like $\sqrt*{2}$ it works for any size of operand.
90 % The package also re-implements the standard square root command so that it
91 % positions the root number rather better.
92 %
93 % \begin{figure}
94 % \begin{demo}[w]{Examples of the new square root command}
95 %\[ \sqrt*{2} \quad \mbox{rather than} \quad \sqrt{2} \]
96 %\[ \sqrt*[3]{2} \quad \mbox{ rather than } \quad \sqrt[3]{2} \]
97 %\[ \sqrt{x^3 + \sqrt*[y]{\alpha}} - \sqrt*[n+1]{a} \]
98 %\[ x = \sqrt*[3]{\frac{3y}{7}} \]
99 %\[ q = \frac{2\sqrt*{2}}{5}+\sqrt[\frac{n+1}{2}]{2x^2+3xy-y^2} \]
100 % \end{demo}
101 % \end{figure}
102 %
103 % [Note that omission of the vinculum was originally a cost-cutting exercise
104 % because the radical symbol can just fit in next to its operand and
105 % everything ends up being laid out along a line. However, I find that the
106 % square root without vinculum is less cluttered, so I tend to use it when
107 % it doesn't cause ambiguity.]
108 %
109 % \subsection{Some maths symbols you already have}
110 %
111 % Having just tried to do some simple things, I've found that there are maths
112 % symbols missing. Here they are, in all their glory:
113 %
114 % \begin{center} \unverb\| \begin{tabular}{cl|cl|cl}
115 % $\&$ & "\&" & $\bitor$ & "\bitor" & $\dbland$ & "\dbland" \\
116 % $\bitand$ & "\bitand" & $\dblor$ & "\dblor" &
117 % \end{tabular} \end{center}
118 %
119 % \begin{ignore}
120 % There used to be an eqnarray here, but that's migrated its way into the
121 % \package{mdwtab} package. Maybe the original version, without dependency
122 % on \package{mdwtab} ought to be releasable separately. I'll keep it around
123 % just in case.
124 %
125 % The following is the documentation for the original version. There's an
126 % updated edition in \package{mdwtab}.
127 % \end{ignore}
128 %
129 % \begin{old-eqnarray}
130 %
131 % \subsection{A new \env{eqnarray} environment}
132 %
133 % \LaTeX's built-in \env{eqnarray} is horrible -- it puts far too much space
134 % between the items in the array. This environment is rather nearer to the
135 % \env{amsmath} \env{align} environments, although rather less capable.
136 %
137 % \bigskip
138 % \DescribeEnv{eqnarray}
139 % {\synshorts
140 % \setbox0\hbox{"\\begin{eqnarray}["<preamble>"]" \dots "\\end{eqnarray}"}
141 % \leavevmode \hskip-\parindent \fbox{\box0}
142 % }
143 % \smallskip
144 %
145 % The new version of \env{eqnarray} tries to do everything which you really
146 % want it to. The \synt{preamble} string allows you to define the column
147 % types in a vaguely similar way to the wonderful \env{tabular} environment.
148 % The types provided (and it's easy-ish to add more) are:
149 %
150 % \def\ch{\char`}
151 % \begin{description} \def\makelabel{\hskip\labelsep\normalfont\ttfamily}
152 % \item [r] Right aligned equation
153 % \item [c] Centre-aligned equation
154 % \item [l] Left aligned equation
155 % \item [\textrm{\texttt{Tr}, \texttt{Tc} and \texttt{Tl}}] Right, centre and
156 % left aligned text (not maths)
157 % \item [L] Left aligned zero-width equation
158 % \item [x] Centred entire equation
159 % \item [:] Big gap separating sets of equations
160 % \item [q] Quad space
161 % \item [>\ch\{\synt{text}\ch\}] Insert text before column
162 % \item [<\ch\{\synt{text}\ch\}] Insert text after column
163 % \end{description}
164 %
165 % Some others are also defined: don't use them because they do complicated
166 % things which are hard to explain and they aren't much use anyway.
167 %
168 % The default preamble, if you don't supply one of your own, is \lit{rcl}.
169 % Most of the time, \lit{rl} is sufficient, although compatibility is more
170 % important to me.
171 %
172 % By default, there is no space between columns, which makes formul\ae\ in an
173 % \env{eqnarray} environment look just like formul\ae\ typeset on their own,
174 % except that things get aligned in columns. This is where the default
175 % \env{eqnarray} falls down: it leaves |\arraycolsep| space between each
176 % column making the thing look horrible.
177 %
178 % An example would be good here, I think. This one's from exercise 22.9 of
179 % the \textit{\TeX book}.
180 %
181 % \begin{demo}[w]{Simultaneous equations}
182 %\begin{eqnarray}[rcrcrcrl]
183 % 10w & + & 3x & + & 3y & + & 18z & = 1 \\
184 % 6w & - & 17x & & & - & 5z & = 2
185 %\end{eqnarray}
186 % \end{demo}
187 %
188 % Choosing a more up-to-date example, here's one demonstrating the \lit{:}
189 % column specifier from the \textit{\LaTeX\ Companion}.
190 %
191 % \begin{demo}[w]{Lots of equations}
192 %\begin{eqnarray}[rl:rl:l]
193 % V_i &= v_i - q_i v_j, & X_i &= x_i - q_i x_j, &
194 % U_i = u_i, \qquad \mbox{for $i \ne j$} \label{eq:A} \\
195 % V_j &= v_j, & X_j &= x_j &
196 % U_j u_j + \sum_{i \ne j} q_i u_i.
197 %\end{eqnarray}
198 % \end{demo}
199 %
200 % We can make things more interesting by adding a plain text column. Here we
201 % go:
202 %
203 % \begin{demo}[w]{Plain text column}
204 %\begin{eqnarray}[rlqqTl]
205 % x &= y & by (\ref{eq:A}) \\
206 % x' &= y' & by definition \\
207 % x + x' &= y + y' & by Axiom~1
208 %\end{eqnarray}
209 % \end{demo}
210 %
211 % The new features also mean that you don't need to mess about with
212 % |\lefteqn| any more. This is handled by the \lit{L} column type:
213 %
214 % \begin{demo}{Splitting example}
215 %\begin{eqnarray*}[Ll]
216 % w+x+y+z = \\
217 % & a+b+c+d+e+ \\
218 % & f+g+h+i+j
219 %\end{eqnarray*}
220 % \end{demo}
221 %
222 % Finally, just to prove that the spacing's right at last, here's another one
223 % from the \textit{Companion}.
224 %
225 % \begin{demo}{Spacing demonstration}
226 %\begin{equation}
227 % x^2 + y^2 = z^2
228 %\end{equation}
229 %\begin{eqnarray}[rl]
230 % x^2 + y^2 &= z^2 \\
231 % y^2 &< z^2
232 %\end{eqnarray}
233 % \end{demo}
234 %
235 % Well, that was easy enough. Now on to numbering. As you've noticed, the
236 % equations above are numbered. You can use the \env{eqnarray$*$}
237 % environment to turn off the numbering in the whole environment, or say
238 % |\nonumber| on a line to suppress numbering of that one in particular.
239 % More excitingly, you can say \syntax{"\\nonumber["<text>"]"} to choose
240 % what text to display.
241 %
242 % A note for cheats: you can use the sparkly new \env{eqnarray} for simple
243 % equations simply by specifying \lit{x} as the column description. Who
244 % needs \AmSTeX? |;-)|
245 %
246 % \end{old-eqnarray}
247 %
248 % \implementation
249 %
250 % \section{Implementation}
251 %
252 % This isn't really complicated (honest) although it is a lot hairier than I
253 % think it ought to be.
254 %
255 % \begin{macrocode}
256 %<*package>
257 % \end{macrocode}
258 %
259 % \subsection{Square roots}
260 %
261 % \subsubsection{Where is the square root sign?}
262 %
263 % \LaTeX\ hides the square root sign away somewhere without telling anyone
264 % where it is. I extract it forcibly by peeking inside the |\sqrtsign| macro
265 % and scrutinising the contents. Here we go: prepare for yukkiness.
266 %
267 % \begin{macrocode}
268 \newcount\sq@sqrt
269 \begingroup
270 \catcode`\|0 \catcode`\\12
271 |def|sq@readrad#1"#2\#3|relax{|global|sq@sqrt"#2|relax}
272 |expandafter|sq@readrad|meaning|sqrtsign|relax
273 |endgroup
274 \def\sq@delim{\delimiter\sq@sqrt\relax}
275 % \end{macrocode}
276 %
277 % \subsubsection{Drawing fake square root signs}
278 %
279 % \TeX\ absolutely insists on drawing square root signs with a vinculum over
280 % the top. In order to get the same effect, we have to attempt to emulate
281 % \TeX's behaviour.
282 %
283 % \begin{macro}{\sqrtdel}
284 %
285 % This does the main job of typesetting a vinculum-free radical.\footnote{^^A
286 % Note for chemists: this is nothing to do with short-lived things which
287 % don't have their normal numbers of electrons. And it won't reduce the
288 % appearance of wrinkles either.}
289 % It's more or less a duplicate of what \TeX\ does internally, so it might be
290 % a good plan to have a copy of Appendix~G open while you examine this.
291 %
292 % We start off by using |\mathpalette| to help decide how big things should
293 % be.
294 %
295 % \begin{macrocode}
296 \def\sqrtdel{\mathpalette\sqrtdel@i}
297 % \end{macrocode}
298 %
299 % Read the contents of the radical into a box, so we can measure it.
300 %
301 % \begin{macrocode}
302 \def\sqrtdel@i#1#2{%
303 \setbox\z@\hbox{$\m@th#1#2$}% %%% Bzzzt -- uncramps the mathstyle
304 % \end{macrocode}
305 %
306 % Now try and sort out the values needed in this calculation. We'll assume
307 % that $\xi_8$ is 0.6\,pt, the way it usually is. Next try to work out the
308 % value of $\varphi$.
309 %
310 % \begin{macrocode}
311 \ifx#1\displaystyle%
312 \@tempdima1ex%
313 \else%
314 \@tempdima.6\p@%
315 \fi%
316 % \end{macrocode}
317 %
318 % That was easy. Now for $\psi$.
319 %
320 % \begin{macrocode}
321 \@tempdimb.6\p@%
322 \advance\@tempdimb.25\@tempdima%
323 % \end{macrocode}
324 %
325 % Build the `delimiter' in a box of height $h(x)+d(x)+\psi+\xi_8$, as
326 % requested. Box~2 will do well for this purpose.
327 %
328 % \begin{macrocode}
329 \[email protected]\p@%
330 \advance\dimen@\@tempdimb%
331 \advance\dimen@\ht\z@%
332 \advance\dimen@\dp\z@%
333 \setbox\tw@\hbox{%
334 $\left\sq@delim\vcenter to\dimen@{}\right.\n@space$%
335 }%
336 % \end{macrocode}
337 %
338 % Now we need to do some more calculating (don't you hate it?). As far as
339 % Appendix~G is concerned, $\theta=h(y)=0$, because we want no rule over the
340 % top.
341 %
342 % \begin{macrocode}
343 \@tempdima\ht\tw@%
344 \advance\@tempdima\dp\tw@%
345 \advance\@tempdima-\ht\z@%
346 \advance\@tempdima-\dp\z@%
347 \ifdim\@tempdima>\@tempdimb%
348 \advance\@tempdima\@tempdimb%
349 \@tempdimb.5\@tempdima%
350 \fi%
351 % \end{macrocode}
352 %
353 % Work out how high to raise the radical symbol. Remember that Appendix~G
354 % thinks that the box has a very small height, although this is untrue here.
355 %
356 % \begin{macrocode}
357 \@tempdima\ht\z@%
358 \advance\@tempdima\@tempdimb%
359 \advance\@tempdima-\ht\tw@%
360 % \end{macrocode}
361 %
362 % Build the output (finally). The brace group is there to turn the output
363 % into a mathord, one of the few times that this is actually desirable.
364 %
365 % \begin{macrocode}
366 {\raise\@tempdima\box\tw@\vbox{\kern\@tempdimb\box\z@}}%
367 }
368 % \end{macrocode}
369 %
370 % \end{macro}
371 %
372 % \subsubsection{The new square root command}
373 %
374 % This is where we reimplement all the square root stuff. Most of this stuff
375 % comes from the \PlainTeX\ macros, although some is influenced by \AmSTeX\
376 % and \LaTeXe, and some is original. I've tried to make the spacing vaguely
377 % automatic, so although it's not configurable like \AmSTeX's version, the
378 % output should look nice more of the time. Maybe.
379 %
380 % \begin{macro}{\sqrt}
381 %
382 % \LaTeX\ says this must be robust, so we make it robust. The first thing to
383 % do is to see if there's a star and pass the appropriate squareroot-drawing
384 % command on to the rest of the code.
385 %
386 % \begin{macrocode}
387 \DeclareRobustCommand\sqrt{\@ifstar{\sqrt@i\sqrtdel}{\sqrt@i\sqrtsign}}
388 % \end{macrocode}
389 %
390 % Now we can sort out an optional argument to be displayed on the root.
391 %
392 % \begin{macrocode}
393 \def\sqrt@i#1{\@ifnextchar[{\sqrt@ii{#1}}{\sqrt@iv{#1}}}
394 % \end{macrocode}
395 %
396 % Stages~2 and~3 below are essentially equivalents of \PlainTeX's
397 % |\root|\dots|\of| and |\r@@t|. Here we also find the first wrinkle: the
398 % |\rootbox| used to store the number is spaced out on the left if necessary.
399 % There's a backspace after the end so that the root can slip underneath, and
400 % everything works out nicely. Unfortunately size is fixed here, although
401 % doesn't actually seem to matter.
402 %
403 % \begin{macrocode}
404 \def\sqrt@ii#1[#2]{%
405 \setbox\rootbox\hbox{$\m@th\scriptscriptstyle{#2}$}%
406 \ifdim\wd\rootbox<6\p@%
407 \setbox\rootbox\hb@xt@6\p@{\hfil\unhbox\rootbox}%
408 \fi%
409 \mathpalette{\sqrt@iii{#1}}%
410 }
411 % \end{macrocode}
412 %
413 % Now we can actually build everything. Note that the root is raised by its
414 % depth -- this prevents a common problem with letters with descenders.
415 %
416 % \begin{macrocode}
417 \def\sqrt@iii#1#2#3{%
418 \setbox\z@\hbox{$\m@th#2#1{#3}$}%
419 \dimen@\ht\z@%
420 \advance\dimen@-\dp\z@%
421 \[email protected]\dimen@%
422 \advance\dimen@\dp\rootbox%
423 \mkern-3mu%
424 \raise\dimen@\copy\rootbox%
425 \mkern-10mu%
426 \box\z@%
427 }
428 % \end{macrocode}
429 %
430 % Finally handle a non-numbered root. We read the rooted text in as an
431 % argument, to stop problems when people omit the braces. (\AmSTeX\ does
432 % this too.)
433 %
434 % \begin{macrocode}
435 \def\sqrt@iv#1#2{#1{#2}}
436 % \end{macrocode}
437 %
438 % \end{macro}
439 %
440 % \begin{macro}{\root}
441 %
442 % We also re-implement \PlainTeX's |\root| command, just in case someone uses
443 % it, and supply a star-variant. This is all very trivial.
444 %
445 % \begin{macrocode}
446 \def\root{\@ifstar{\root@i\sqrtdel}{\root@i\sqrtsign}}
447 \def\root@i#1#2\of{\sqrt@ii{#1}[#2]}
448 % \end{macrocode}
449 %
450 % \end{macro}
451 %
452 % \subsection{Some magic new maths characters}
453 %
454 % This is all really easy.
455 %
456 % \begin{macrocode}
457 \DeclareMathSymbol{&}{\mathbin}{operators}{`\&}
458 \DeclareMathSymbol{\bitand}{\mathbin}{operators}{`\&}
459 \def\bitor{\mathbin\mid}
460 \def\dblor{\mathbin{\mid\mid}}
461 \def\dbland{\mathbin{\mathrel\bitand\mathrel\bitand}}
462 % \end{macrocode}
463 %
464 % \subsection{Biggles}
465 %
466 % Now for some user-controlled delimiter sizing. The standard bigness of
467 % plain \TeX's delimiters are all right, but it's a little limiting.
468 %
469 % The biggness of delimiters is based on the size of the current |\strut|,
470 % which \LaTeX\ keeps up to date all the time. This will make the various
471 % delimiters grow in proportion when the text gets bigger. Actually, I'm
472 % not sure that this is exactly right -- maybe it should be nonlinear,
473 %
474 % \begin{macro}{\bbigg}
475 % \begin{macro}{\bbiggl}
476 % \begin{macro}{\bbiggr}
477 % \begin{macro}{\bbiggm}
478 %
479 % This is where the bigness is done. This is more similar to the plain \TeX\
480 % big delimiter stuff than to the \package{amsmath} stuff, although there's
481 % not really a lot of difference.
482 %
483 % The two arguments are a multiplier for the delimiter size, and a small
484 % increment applied \emph{before} the multiplication (which is optional).
485 %
486 % This is actually a front for a low-level interface which can be called
487 % directly for efficiency.
488 %
489 % \begin{macrocode}
490 \def\bbigg{\@bbigg\mathord}
491 \def\bbiggl{\@bbigg\mathopen}
492 \def\bbiggr{\@bbigg\mathclose}
493 \def\bbiggm{\@bbigg\mathrel}
494 % \end{macrocode}
495 %
496 % \end{macro}
497 % \end{macro}
498 % \end{macro}
499 % \end{macro}
500 %
501 % \begin{macro}{\@bbigg}
502 %
503 % This is an optional argument parser providing a front end for the main
504 % macro |\bbigg@|.
505 %
506 % \begin{macrocode}
507 \def\@bbigg#1{\@ifnextchar[{\@bigg@i{#1}}{\@bigg@i{#1}[\z@]}}
508 \def\@bigg@i#1[#2]#3#4{#1{\bbigg@{#2}{#3}{#4}}}
509 % \end{macrocode}
510 %
511 % \end{macro}
512 %
513 % \begin{macro}{\bbigg@}
514 %
515 % This is it, at last. The arguments are as described above: an addition
516 % to be made to the strut height, and a multiplier. Oh, and the delimiter,
517 % of course.
518 %
519 % This is a bit messy. The smallest `big' delimiter, |\big|, is the same
520 % height as the current strut box. Other delimiters are~$1\frac12$, $2$
521 % and~$2\frac12$ times this height. I'll set the height of the delimiter by
522 % putting in a |\vcenter| of the appropriate size.
523 %
524 % Given an extra height~$x$, a multiplication factor~$f$ and a strut
525 % height~$h$ and depth~$d$, I'll create a vcenter with total height
526 % $f(h+d+x)$. Easy, isn't it?
527 %
528 % \begin{macrocode}
529 \def\bbigg@#1#2#3{%
530 \hbox{$%
531 \dimen@\ht\strutbox\advance\dimen@\dp\strutbox%
532 \advance\dimen@#1%
533 \dimen@#2\dimen@%
534 \left#3\vcenter to\dimen@{}\right.\n@space%
535 $}%
536 }
537 % \end{macrocode}
538 %
539 % \end{macro}
540 %
541 % \begin{macro}{\big}
542 % \begin{macro}{\Big}
543 % \begin{macro}{\bigg}
544 % \begin{macro}{\Bigg}
545 %
546 % Now for the easy macros.
547 %
548 % \begin{macrocode}
549 \def\big{\bbigg@\z@\@ne}
550 \def\Big{\bbigg@\z@{1.5}}
551 \def\bigg{\bbigg@\z@\tw@}
552 \def\Bigg{\bbigg@\z@{2.5}}
553 % \end{macrocode}
554 %
555 % \end{macro}
556 % \end{macro}
557 % \end{macro}
558 % \end{macro}
559 %
560 %
561 % \begin{ignore}
562 % The following is the original definition of the enhanced eqnarray
563 % environment. It's not supported, although if you can figure out how to
564 % extract it, it's all yours.
565 % \end{ignore}
566 %
567 % \begin{old-eqnarray}
568 %
569 % \subsection{The sparkly new \env{eqnarray}}
570 %
571 % Start off by writing a different package.
572 %
573 % \begin{macrocode}
574 %</package>
575 %<*oldeqnarray>
576 % \end{macrocode}
577 %
578 % \subsubsection{Options handling}
579 %
580 % We need to be able to cope with \textsf{fleqn} and \textsf{leqno} options.
581 % This will adjust our magic modified \env{eqnarray} environment
582 % appropriately.
583 %
584 % \begin{macrocode}
585 \newif\if@fleqn
586 \newif\if@leqno
587 \DeclareOption{fleqn}{\@fleqntrue}
588 \DeclareOption{leqno}{\@leqnotrue}
589 \ProcessOptions
590 % \end{macrocode}
591 %
592 % This is all really different to the \LaTeX\ version. I've looked at the
593 % various \env{tabular} implementations, the original \env{eqnarray} and the
594 % \textit{\TeX book} to see how best to do this, and then went my own way.
595 % If it doesn't work it's all my fault.
596 %
597 % \subsubsection{Some useful registers}
598 %
599 % The old \LaTeX\ version puts the equation numbers in by keeping a count of
600 % where it is in the alignment. Since I don't know how may columns there are
601 % going to be, I'll just use a switch in the preamble to tell me to stop
602 % tabbing.
603 %
604 % \begin{macrocode}
605 \newif\if@eqalast
606 % \end{macrocode}
607 %
608 % Now define some useful length parameters. First allocate them:
609 %
610 % \begin{macrocode}
611 \newskip\eqaopenskip
612 \newskip\eqacloseskip
613 \newskip\eqacolskip
614 \newskip\eqainskip
615 % \end{macrocode}
616 %
617 % Now assign some default values. Users can play with these if they really
618 % want although I can't see the point myself.
619 %
620 % \begin{macrocode}
621 \if@fleqn
622 \AtBeginDocument{\eqaopenskip\leftmargini}
623 \else
624 \eqaopenskip\@centering
625 \fi
626 \eqacloseskip\@centering
627 \eqacolskip\@centering
628 \eqainskip\z@
629 % \end{macrocode}
630 %
631 % We allow the user to play with the style if this is really wanted. I dunno
632 % why, really. Maybe someone wants very small alignments.
633 %
634 % \begin{macrocode}
635 \let\eqa@style\displaystyle
636 % \end{macrocode}
637 %
638 % \subsubsection{The main environments}
639 %
640 % We define the toplevel commands here. They just add in default arguments
641 % and then call |\@eqnarray| with a preamble string. The only difference is
642 % the last column they add in -- \env{eqnarray$*$} throws away the last
643 % column by sticking it in box~0. (I used to |\@gobble| it but that caused
644 % the |\cr| to be lost.)
645 %
646 % \begin{macrocode}
647 \def\eqnarray{\@ifnextchar[\eqnarray@i{\eqnarray@i[rcl]}}
648 \def\eqnarray@i[#1]{%
649 \@eqnarray{#1!{\hb@xt@\z@{\hss##}\tabskip\z@}}
650 }
651 \@namedef{eqnarray*}{\@ifnextchar[\eqnarray@s@i{\eqnarray@s@i[rcl]}}
652 \def\eqnarray@s@i[#1]{%
653 \@eqnarray{#1!{\nonumber\setbox\z@\hbox{##}\tabskip\z@}}%
654 }
655 % \end{macrocode}
656 %
657 % \subsubsection{Set up the initial display}
658 %
659 % \begin{macro}{\@eqnarray}
660 %
661 % The |\@eqnarray| command does most of the initial work. It sets up some
662 % flags and things, builds the |\halign| preamble, and returns.
663 %
664 % \begin{macrocode}
665 \def\@eqnarray#1{%
666 % \end{macrocode}
667 %
668 % Start playing with the counter here. The original does some icky internal
669 % playing, which isn't necessary. The |\if@eqnsw| switch is |true| if the
670 % user hasn't supplied an equation number. The |\if@eqalast| switch is
671 % |true| in the final equation-number column.
672 %
673 % \begin{macrocode}
674 \refstepcounter{equation}%
675 \@eqalastfalse%
676 \global\@eqnswtrue%
677 \m@th%
678 % \end{macrocode}
679 %
680 % Set things up for the |\halign| which is coming up.
681 %
682 % \begin{macrocode}
683 \openup\jot%
684 \tabskip\eqaopenskip%
685 \let\\\@eqncr%
686 \everycr{}%
687 $$%
688 % \end{macrocode}
689 %
690 % We'll build the real |\halign| and preamble in a token register. All we
691 % need to do is stuff the header in the token register, clear a switch
692 % (that'll be explained later), parse the preamble and then expand the
693 % tokens we collected. Easy, no?
694 %
695 % \begin{macrocode}
696 \toks@{\halign to\displaywidth\bgroup}%
697 \@tempswafalse%
698 \eqa@preamble#1\end%
699 \the\toks@\cr%
700 }
701 % \end{macrocode}
702 %
703 % \end{macro}
704 %
705 % \subsubsection{Parsing the preamble}
706 %
707 % All this actually involves is reading the next character and building a
708 % command from it. That can pull off an argument if it needs it. Just make
709 % sure we don't fall off the end and we'll be OK.
710 %
711 % \begin{macrocode}
712 \def\eqa@preamble#1{%
713 \ifx\end#1\else\csname eqa@char@#1\expandafter\endcsname\fi%
714 }
715 % \end{macrocode}
716 %
717 % Adding stuff to the preamble tokens is a simple matter of using
718 % |\expandafter| in the correct way.\footnote{^^A
719 % I have no idea why \LaTeX\ uses \cmd\edef\ for building its preamble. It
720 % seems utterly insane to me -- the amount of bodgery that \env{tabular}
721 % has to go through to make everything expand at the appropriate times is
722 % scary. Maybe Messrs~Lamport and Mittelbach just forgot about token
723 % registers when they were writing the code. Maybe I ought to rewrite the
724 % thing properly some time. Sigh.
725 %
726 % As a sort of postscript to the above, I \emph{have} rewritten the
727 % \env{tabular} environment, and made a damned fine job of it, in my
728 % oh-so-humble opinion. All this \env{eqnarray} stuff has been remoulded
729 % in terms of the generic column-defining things in \package{mdwtab}.
730 % You're reading the documentation of the old version, which isn't
731 % supported any more, so any bugs here are your own problem.}
732 %
733 % \begin{macrocode}
734 \def\eqa@addraw#1{\expandafter\toks@\expandafter{\the\toks@#1}}
735 % \end{macrocode}
736 %
737 % Now for some cleverness again. In order to put all the right bits of
738 % |\tabskip| glue in the right places we must \emph{not} terminate each
739 % column until we know what the next one is. We set |\if@tempswa| to be
740 % |true| if there's a column waiting to be closed (so it's initially
741 % |false|). The following macro adds a column correctly, assuming we're in
742 % a formula. Other column types make their own arrangements.
743 %
744 % \begin{macrocode}
745 \def\eqa@add#1{%
746 \if@tempswa%
747 \eqa@addraw{\tabskip\eqainskip&#1}%
748 \else%
749 \eqa@addraw{#1}%
750 \fi%
751 \@tempswatrue%
752 }
753 % \end{macrocode}
754 %
755 % Now to defining column types. Let's define a macro which allows us to
756 % define column types:
757 %
758 % \begin{macrocode}
759 \def\eqa@def#1{\expandafter\def\csname eqa@char@#1\endcsname}
760 % \end{macrocode}
761 %
762 % Now we can define the column types. Each column type must loop back to
763 % |\eqa@preamble| once it's finished, to read the rest of the preamble
764 % string. Note the positioning of ord atoms in the stuff below. This will
765 % space out relations and binops correctly when they occur at the edges of
766 % columns, and won't affect ord atoms at the edges, because ords pack
767 % closely.
768 %
769 % First the easy onces. Just stick |\hfil| in the right places and
770 % everything will be all right.
771 %
772 % \begin{macrocode}
773 \eqa@def r{\eqa@add{\hfil$\eqa@style##{}$}\eqa@preamble}
774 \eqa@def c{\eqa@add{\hfil$\eqa@style{}##{}$\hfil}\eqa@preamble}
775 \eqa@def l{\eqa@add{$\eqa@style{}##$\hfil}\eqa@preamble}
776 \eqa@def x{\eqa@add{\hfil$\eqa@style##$\hfil}\eqa@preamble}
777 % \end{macrocode}
778 %
779 % Now for the textual ones. This is also fairly easy.
780 %
781 % \begin{macrocode}
782 \eqa@def T#1{%
783 \eqa@add{}%
784 \if#1l\else\eqa@addraw{\hfil}\fi%
785 \eqa@addraw{##}%
786 \if#1r\else\eqa@addraw{\hfil}\fi%
787 \eqa@preamble%
788 }
789 % \end{macrocode}
790 %
791 % Sort of split types of equations. I mustn't use |\rlap| here, or
792 % everything goes wrong -- |\\| doesn't get noticed by \TeX\ in the same way
793 % as |\cr| does.
794 %
795 % \begin{macrocode}
796 \eqa@def L{\eqa@add{\hb@xt@\z@{$\eqa@style##$\hss}\qquad}\eqa@preamble}
797 % \end{macrocode}
798 %
799 % The \lit{:} column type is fairly simple. We set |\tabskip| up to make
800 % lots of space and close the current column, because there must be one.^^A
801 % \footnote{This is an assumption.}
802 %
803 % \begin{macrocode}
804 \eqa@def :{%
805 \eqa@addraw{\tabskip\eqacolskip&}\@tempswafalse\eqa@preamble%
806 }
807 \eqa@def q{\eqa@add{\quad}\@tempswafalse\eqa@preamble}
808 % \end{macrocode}
809 %
810 % The other column types just insert given text in an appropriate way.
811 %
812 % \begin{macrocode}
813 \eqa@def >#1{\eqa@add{#1}\@tempswafalse\eqa@preamble}
814 \eqa@def <#1{\eqa@addraw{#1}\eqa@preamble}
815 % \end{macrocode}
816 %
817 % Finally, the magical \lit{!} column type, which sets the equation number.
818 % We set up the |\tabskip| glue properly, tab on, and set the flag which
819 % marks the final column.
820 %
821 % \begin{macrocode}
822 \eqa@def !#1{%
823 \eqa@addraw{\tabskip\eqacloseskip&\@eqalasttrue#1}\eqa@preamble%
824 }
825 % \end{macrocode}
826 %
827 % \subsubsection{Newline codes}
828 %
829 % Newline sequences (|\\|) get turned into calls of |\@eqncr|. The job is
830 % fairly simple, really. However, to avoid reading `|&|' characters
831 % prematurely, we set up a magic brace (from the \package{array} package --
832 % this avoids creating ord atoms and other nastyness).
833 %
834 % \begin{macrocode}
835 \def\@eqncr{%
836 \iffalse{\fi\ifnum0=`}\fi%
837 \@ifstar{\eqacr@i{\@M}}{\eqacr@i{\interdisplaylinepenalty}}%
838 }
839 \def\eqacr@i#1{\@ifnextchar[{\eqacr@ii{#1}}{\eqacr@ii{#1}[\z@]}}
840 \def\eqacr@ii#1[#2]{%
841 \ifnum0=`{}\fi%
842 \eqa@eqnum%
843 \noalign{\penalty#1\vskip#2\relax}%
844 }
845 % \end{macrocode}
846 %
847 % \subsubsection{Setting equation numbers}
848 %
849 % Before we start, we need to generalise the flush-left number handling bits.
850 % The macro |\eqa@eqpos| will put its argument in the right place.
851 %
852 % \begin{macrocode}
853 \if@leqno
854 \def\eqa@eqpos#1{%
855 \hb@[email protected]\p@{}\rlap{\normalfont\normalcolor\hskip-\displaywidth#1}%
856 }
857 \else
858 \def\eqa@eqpos#1{\normalfont\normalcolor#1}
859 \fi
860 % \end{macrocode}
861 %
862 % First we need to move into the right column. Then we just set the equation
863 % number appropriately. There is some subtlety here, ish. The |\relax| is
864 % important, to delay expansion of the |\if|\dots\ until the new column has
865 % been started. The two helper macros are important too, to hide `|&|'s and
866 % `|\cr|'s from \TeX's scanner until the right time.
867 %
868 % \begin{macrocode}
869 \def\eqa@eqnum{%
870 \relax%
871 \if@eqalast\expandafter\eqa@eqnum@i\else\expandafter\eqa@eqnum@ii\fi%
872 }
873 \def\eqa@eqnum@i{%
874 \if@eqnsw%
875 \eqa@eqpos{(\theequation)}\stepcounter{equation}%
876 \else%
877 \eqa@eqpos\eqa@number%
878 \fi%
879 \global\@eqnswtrue%
880 \cr%
881 }
882 \def\eqa@eqnum@ii{&\eqa@eqnum}
883 % \end{macrocode}
884 %
885 % \subsubsection{Numbering control}
886 %
887 % This is trivial. We set the |\if@eqnsw| flag to be |false| and store the
888 % text in a macro.
889 %
890 % \begin{macrocode}
891 \let\nonumber\relax
892 \newcommand\nonumber[1][]{\global\@eqnswfalse\global\def\eqa@number{#1}}
893 % \end{macrocode}
894 %
895 % \subsubsection{Closing the environments off}
896 %
897 % This is really easy. Set the final equation number, close the |\halign|,
898 % tidy up the equation counter (it's been stepped once too many times) and
899 % close the display.
900 %
901 % \begin{macrocode}
902 \def\endeqnarray{%
903 \eqa@eqnum%
904 \egroup%
905 \global\advance\c@equation\m@ne%
906 $$%
907 \global\@ignoretrue%
908 }
909 \expandafter\let\csname endeqnarray*\endcsname\endeqnarray
910 % \end{macrocode}
911 %
912 % Now start up the other package again.
913 %
914 % \begin{macrocode}
915 %</oldeqnarray>
916 %<*package>
917 % \end{macrocode}
918 %
919 % \end{old-eqnarray}
920 %
921 % That's all there is. Byebye.
922 %
923 % \begin{macrocode}
924 %</package>
925 % \end{macrocode}
926 %
927 % \hfill Mark Wooding, \today
928 %
929 % \Finale
930 \endinput