Fix dates and version numbers in the package files.
[mdwtools] / mdwmath.dtx
CommitLineData
86f6a31e 1% \begin{meta-comment} <general public licence>
2%%
3%% mdwmath package -- various nicer mathematical things
8bc5bdd2 4%% Copyright (c) 2003, 2020 Mark Wooding
86f6a31e 5%%
6%% This program is free software; you can redistribute it and/or modify
7%% it under the terms of the GNU General Public License as published by
8%% the Free Software Foundation; either version 2 of the License, or
9%% (at your option) any later version.
10%%
11%% This program is distributed in the hope that it will be useful,
12%% but WITHOUT ANY WARRANTY; without even the implied warranty of
13%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14%% GNU General Public License for more details.
15%%
16%% You should have received a copy of the GNU General Public License
17%% along with this program; if not, write to the Free Software
18%% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19%%
20% \end{meta-comment}
21%
22% \begin{meta-comment} <Package preamble>
23%<+package>\NeedsTeXFormat{LaTeX2e}
24%<+package>\ProvidesPackage{mdwmath}
af8af7eb 25%<+package> [2020/09/06 1.14.0 Nice mathematical things]
86f6a31e 26%<+oldeqnarray>\NeedsTeXFormat{LaTeX2e}
27%<+oldeqnarray>\ProvidesPackage{eqnarray}
af8af7eb 28%<+oldeqnarray> [2020/09/06 1.14.0 Old enhanced eqnarray]
86f6a31e 29% \end{meta-comment}
30%
3ba7380e 31% \CheckSum{740}
86f6a31e 32%% \CharacterTable
33%% {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
34%% 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
35%% Digits \0\1\2\3\4\5\6\7\8\9
36%% Exclamation \! Double quote \" Hash (number) \#
37%% Dollar \$ Percent \% Ampersand \&
38%% Acute accent \' Left paren \( Right paren \)
39%% Asterisk \* Plus \+ Comma \,
40%% Minus \- Point \. Solidus \/
41%% Colon \: Semicolon \; Less than \<
42%% Equals \= Greater than \> Question mark \?
43%% Commercial at \@ Left bracket \[ Backslash \\
44%% Right bracket \] Circumflex \^ Underscore \_
45%% Grave accent \` Left brace \{ Vertical bar \|
46%% Right brace \} Tilde \~}
47%%
48%
49% \begin{meta-comment}
50%
51%<*driver>
52\input{mdwtools}
53\let\opmod\pmod
54\usepackage{amssymb}
55\describespackage{mdwmath}
4a655c6f 56%\describespackage{eqnarray}
86f6a31e 57\ignoreenv{old-eqnarray}
4a655c6f 58%\unignoreenv{old-eqnarray}
86f6a31e 59\mdwdoc
60%</driver>
61%
62% \end{meta-comment}
63%
64% \section{User guide}
65%
66% \subsection{Square root typesetting}
67%
68% \DescribeMacro{\sqrt}
69% The package supplies a star variant of the |\sqrt| command which omits the
70% vinculum over the operand (the line over the top). While this is most
71% useful in simple cases like $\sqrt*{2}$ it works for any size of operand.
72% The package also re-implements the standard square root command so that it
73% positions the root number rather better.
74%
75% \begin{figure}
76% \begin{demo}[w]{Examples of the new square root command}
77%\[ \sqrt*{2} \quad \mbox{rather than} \quad \sqrt{2} \]
78%\[ \sqrt*[3]{2} \quad \mbox{ rather than } \quad \sqrt[3]{2} \]
79%\[ \sqrt{x^3 + \sqrt*[y]{\alpha}} - \sqrt*[n+1]{a} \]
80%\[ x = \sqrt*[3]{\frac{3y}{7}} \]
81%\[ q = \frac{2\sqrt*{2}}{5}+\sqrt[\frac{n+1}{2}]{2x^2+3xy-y^2} \]
82% \end{demo}
83% \end{figure}
84%
85% [Note that omission of the vinculum was originally a cost-cutting exercise
86% because the radical symbol can just fit in next to its operand and
87% everything ends up being laid out along a line. However, I find that the
88% square root without vinculum is less cluttered, so I tend to use it when
89% it doesn't cause ambiguity.]
90%
91% \subsection{Modular arithmetic}
92%
93% In standard maths mode, there's too much space before the parentheses in
94% the output of the |\pmod| command. Suppose that $x \equiv y^2 \opmod n$:
95% then the spacing looks awful. Go on, admit it.
96%
97% It looks OK in a display. For example, if
98% \[ c \equiv m^e \opmod n \]
99% then it's fine. The package redefines the |\pmod| command to do something
100% more sensible. So now $c^d \equiv m^{ed} \equiv m \pmod n$ and all looks
101% fine.
102%
103% \subsection{Some maths symbols you already have}
104%
105% \DescribeMacro\bitor
106% \DescribeMacro\bitand
107% \DescribeMacro\dblor
108% \DescribeMacro\dbland
109% Having just tried to do some simple things, I've found that there are maths
110% symbols missing. Here they are, in all their glory:
111% \begin{center} \unverb\| \begin{tabular}{cl|cl|cl}
eafdddad
MW
112% $\&$ & "\&" & $\bitor$ & "\bitor" & $\dbland$ & "\dbland" \\
113% $\bitand$ & "\bitand" & $\dblor$ & "\dblor" &
86f6a31e 114% \end{tabular} \end{center}
115%
116% \DescribeMacro\xor
117% \DescribeMacro\cat
118% I also set up the |\xor| command to typeset `$\xor$', which is commonly
119% used to represent the bitsize exclusive-or operation among cryptographers.
120% The command |\cat| typesets `$\cat$', which is a common operator indicating
121% concatenation of strings.
122%
123% \DescribeMacro\lsl
124% \DescribeMacro\lsr
125% \DescribeMacro\rol
126% \DescribeMacro\ror
127% The commands |\lsl| and |\lsr| typeset binary operators `$\lsl$' and
128% `$\lsr$' respectively, and |\rol| and |\ror| typeset `$\rol$' and `$\ror$'.
129% Note that these are spaced as binary operators, rather than relations.
130%
131% \DescribeMacro\compose
132% \DescribeMacro\implies
133% \DescribeMacro\vect
134% The |\compose| command typesets `$\compose$', which is usually used to
135% denote function composition. The |\implies| command is made to typeset
136% `$\implies$'. And \syntax{"\\vect{"<x>"}"} typesets `$\vect{x}$'.
137%
138% \DescribeMacro\statclose
139% \DescribeMacro\compind
140% The |\statclose| command typesets `$\statclose$', which indicates
141% `statistical closeness' of probability distributions; |\compind| typesets
142% `$\compind$', which indicates computational indistinguishability.
143%
4a655c6f 144% \subsection{Fractions}
145%
146% \DescribeMacro\fracdef
147% We provide a general fraction system, a little tiny bit like
148% \package{amsmath}'s |\genfrac|. Say
149% \syntax{"\\fracdef{"<name>"}{"<frac-params>"}"} to define a new
150% |\frac|-like operator. The \<frac-params> are a comma-separated list of
151% parameters:
152% \begin{description}
153% \item[\lit*{line}] Include a horizontal line between the top and bottom
154% (like |\frac|).
155% \item[\lit*{line=}\<length>] Include a horizontal line with width
156% \<length>.
157% \item[\lit*{noline}] Don't include a line (like |\binom|).
158% \item[\lit*{leftdelim=}\<delim>] Use \<delim> as the left-hand delimiter.
159% \item[\lit*{rightdelim=}\<delim>] Use \<delim> as the right-hand delimiter.
160% \item[\lit*{nodelims}] Don't include delimiters.
161% \item[\lit*{style=}\<style>] Typeset the fraction in \<style>, which is one
162% of |display|, |text|, |script| or |scriptscript|.
163% \item[\lit*{style}] Use the prevailing style for the fraction.
164% \item[\lit*{innerstyle=}\<style>] Typeset the \emph{components} of the
165% fraction in \<style>.
166% \item[\lit*{innerstyle}] Typeset the fraction components according to the
167% prevailing style.
168% \end{description}
169% The commands created by |\fracdef| have the following syntax:
170% \syntax{<name>"["<frac-params>"]{"<top>"}{"<bottom>"}"}. Thus, you can use
171% the optional argument to `tweak' the fraction if necessary. This isn't
172% such a good idea to do often.
173%
174% \DescribeMacro\frac
175% \DescribeMacro\binom
86f6a31e 176% \DescribeMacro\jacobi
4a655c6f 177% The macros |\frac|, |\binom| and |\jacobi| are defined using |\fracdef|.
178% They typset $\frac{x}{y}$, $\binom{n}{k}$ and $\jacobi{x}{n}$ respectively.
179% (The last may be of use to number theorists talking about Jacobi or
180% Lagrange symbols.)
181%
182% By way of example, these commands were defined using
183%\begin{verbatim}
184%\fracdef\frac{nodelims, line}
185%\fracdef\binom{leftdelim = (, rightdelim = ), noline}
186%\fracdef\jacobi{leftdelim = (, rightdelim = ), line}
187%\end{verbatim}
86f6a31e 188%
189% \subsection{Rant about derivatives}
190%
191% \DescribeMacro\d
192% There is a difference between UK and US typesetting of derivatives.
193% Americans typeset
194% \[ \frac{dy}{dx} \]
195% while the British want
196% \[ \frac{\d y}{\d x}. \]
4a655c6f 197% The command |\d| command is fixed to typeset a `$\d$'. (In text mode,
198% |\d{x}| still typesets `\d{x}'.)
86f6a31e 199%
200% \subsection{New operator names}
201%
202% \DescribeMacro\keys
203% \DescribeMacro\dom
204% \DescribeMacro\ran
205% \DescribeMacro\supp
206% \DescribeMacro\lcm
4a655c6f 207% \DescribeMacro\ord
208% \DescribeMacro\poly
209% \DescribeMacro\negl
86f6a31e 210% A few esoteric new operator names are supplied.
211% \begin{center} \unverb\| \begin{tabular}{cl|cl|cl}
eafdddad
MW
212% $\keys$ & "\keys" & $\dom$ & "\dom" & $\ran$ & "\ran" \\
213% $\supp$ & "\supp" & $\lcm$ & "\lcm" & $\ord$ & "\ord" \\
214% $\poly$ & "\poly" & $\negl$ & "\negl"
86f6a31e 215% \end{tabular} \end{center}
216% I think |\lcm| ought to be self-explanatory. The |\dom| and |\ran|
217% operators pick out the domain and range of a function, respectively; thus,
218% if $F\colon X \to Y$ is a function, then $\dom F = X$ and $\ran F = Y$.
219% The \emph{support} of a probability distribution $\mathcal{D}$ is the set
220% of objects with nonzero probability; i.e., $\supp{D} = \{\, x \in
4a655c6f 221% \dom\mathcal{D} \mid \mathcal{D}(x) > 0 \,\}$. If $g \in G$ is a group
222% element then $\ord g$ is the \emph{order} of $g$; i.e., the smallest
223% positive integer $i$ where $g^i$ is the identity element, or $0$ if there
224% is no such $i$. $\poly(n)$ is some polynomial function of $n$. A function
225% $\nu(\cdot)$ is \emph{negligible} if, for every polynomial function
226% $p(\cdot)$, there is an integer $N$ such that $\nu(n) < 1/p(n)$ for all $n
227% > N$; $\negl(n)$ is some negligible function of $n$.
86f6a31e 228%
229% \subsection{Standard set names}
230%
231% \DescribeMacro\Z
232% \DescribeMacro\Q
233% \DescribeMacro\R
234% \DescribeMacro\C
235% \DescribeMacro\N
236% \DescribeMacro\F
237% \DescribeMacro\powerset
4a655c6f 238% \DescribeMacro\gf
86f6a31e 239% If you have a |\mathbb| command defined, the following magic is revealed:
240% \begin{center} \unverb\| \begin{tabular}{cl|cl|cl}
241% $\Z$ & "\Z" & $\Q$ & "\Q" & $\R$ & "\R" \\
242% $\N$ & "\N" & $\F$ & "\F" & $\C$ & "\C"
243% \end{tabular} \end{center}
244% which are handy for various standard sets of things. Also the |\powerset|
4a655c6f 245% command typesets `$\powerset$', and \syntax{"\\gf{"<q>"}"}, which by default
246% typesets $\gf{\syntax{<q>}}$ but you might choose to have it set
247% $\mathrm{GF}(\syntax{<q>})$ intead.
248%
249% \subsection{Biggles}
250%
251% \DescribeMacro\bbigg
252% \DescribeMacro\bbiggl
253% \DescribeMacro\bbiggr
254% \DescribeMacro\bbiggm
255% The |\bbigg| commands generalizes the Plain \TeX\ |\bigg| family of
256% macros. |\bbigg| produces an `ordinary' symbol; |\bbiggl| and |\bbiggr|
257% produce left and right delimiters; and |\bbiggm| produces a relation. They
258% produce symbols whose size is related to the prevailing text size -- so
259% they adjust correctly in chapter headings, for example.
260%
261% The syntax is straightforward:
262% \syntax{"\\"<bigop>"["$a$"]{"$n$"}{"<delim>"}"}. Describing it is a bit
263% trickier. The size is based on the current |\strut| height. If |\strut|
264% has a height of $h$ and a depth of $d$, then the delimiter produced has a
265% height of $n \times (h + d + a)$.
266%
267% The old |\big| commands have been redefined in terms of |\bbigg|.
86f6a31e 268%
269% \subsection{The `QED' symbol}
270%
271% \DescribeMacro\qed
272% \DescribeMacro\qedrule
273% For use in proofs of theorems, we provide a `QED' symbol which behaves well
274% under bizarre line-splitting conditions. To use it, just say |\qed|. The
275% little `\qedrule' symbol is available on its own, by saying |\qedrule|.
276% This also sets |\qedsymbol| if it's not set already.
277% \qed
278%
3ba7380e
MW
279% \subsection{Punctuation in displays}
280%
281% It's conventional to follow displayed equations with the necessary
282% punctuation for them to fit into the surrounding prose. This isn't
283% universal: Ian Stewart says in the preface to the third edition of his
284% \emph{Galois Theory}:\footnote{^^A
285% Chapman \& Hall/CRC Mathematics, 2004; ISBN 1-58488-393-6.} ^^A
286% \begin{quote}
287% Along the way I made once change that may raise a few eyebrows. I have
288% spent much of my career telling students that written mathematics should
289% have punctuation as well as symbols. If a symbol or a formula would be
290% followed by a comma if it were replaced by a word or phrase, then it
291% should be followed by a comma; however strange the formula then looks.
292%
293% I still think that punctuation is essential for formulas in the main body
294% of the text. If the formula is $t^2 + 1$, say, then it should have its
295% terminating comma. But I have come to the conclusion that eliminating
296% visual junk from the printed page is more important than punctuatory
297% pedantry, so that when the same formula is \emph{displayed}, for example
298% \[ t^2 + 1 \]
299% then it looks silly if the comma is included, like this,
300% \[ t^2 + 1 \mpunct{,} \]
301% and everything is much cleaner and less ambiguous without punctuation.
302%
303% Purists will hate this, though many of them would not have noticed had I
304% not pointed it out here. Until recently, I would have agreed. But I
305% think it is time we accepted that the act of displaying a formula equips
306% it with \emph{implicit} (invisible) punctuation. This is the 21st
307% century, and typography has moved on.
308% \end{quote}%
309%
310% \DescribeMacro\mpunct
311% I tended to agree with Prof.\ Stewart, even before I read his preface; but
312% now I'm not so sure, and it's clear that we're in the minority. Therefore,
313% the command |\mpunct| sets its argument as text, a little distance from
314% the preceding mathematics.
315%
86f6a31e 316% \begin{ignore}
317% There used to be an eqnarray here, but that's migrated its way into the
318% \package{mdwtab} package. Maybe the original version, without dependency
319% on \package{mdwtab} ought to be releasable separately. I'll keep it around
320% just in case.
321%
322% The following is the documentation for the original version. There's an
323% updated edition in \package{mdwtab}.
324% \end{ignore}
325%
326% \begin{old-eqnarray}
327%
328% \subsection{A new \env{eqnarray} environment}
329%
330% \LaTeX's built-in \env{eqnarray} is horrible -- it puts far too much space
331% between the items in the array. This environment is rather nearer to the
332% \env{amsmath} \env{align} environments, although rather less capable.
333%
334% \bigskip
335% \DescribeEnv{eqnarray}
336% {\synshorts
337% \setbox0\hbox{"\\begin{eqnarray}["<preamble>"]" \dots "\\end{eqnarray}"}
338% \leavevmode \hskip-\parindent \fbox{\box0}
339% }
340% \smallskip
341%
342% The new version of \env{eqnarray} tries to do everything which you really
343% want it to. The \synt{preamble} string allows you to define the column
344% types in a vaguely similar way to the wonderful \env{tabular} environment.
345% The types provided (and it's easy-ish to add more) are:
346%
347% \def\ch{\char`}
78cdb9cc 348% \begin{description} \setdescriptionlabel{\normalfont\ttfamily#1}
86f6a31e 349% \item [r] Right aligned equation
350% \item [c] Centre-aligned equation
351% \item [l] Left aligned equation
352% \item [\textrm{\texttt{Tr}, \texttt{Tc} and \texttt{Tl}}] Right, centre and
353% left aligned text (not maths)
354% \item [L] Left aligned zero-width equation
355% \item [x] Centred entire equation
356% \item [:] Big gap separating sets of equations
357% \item [q] Quad space
358% \item [>\ch\{\synt{text}\ch\}] Insert text before column
359% \item [<\ch\{\synt{text}\ch\}] Insert text after column
360% \end{description}
361%
362% Some others are also defined: don't use them because they do complicated
363% things which are hard to explain and they aren't much use anyway.
364%
365% The default preamble, if you don't supply one of your own, is \lit{rcl}.
366% Most of the time, \lit{rl} is sufficient, although compatibility is more
367% important to me.
368%
369% By default, there is no space between columns, which makes formul\ae\ in an
370% \env{eqnarray} environment look just like formul\ae\ typeset on their own,
371% except that things get aligned in columns. This is where the default
372% \env{eqnarray} falls down: it leaves |\arraycolsep| space between each
373% column making the thing look horrible.
374%
375% An example would be good here, I think. This one's from exercise 22.9 of
376% the \textit{\TeX book}.
377%
378% \begin{demo}[w]{Simultaneous equations}
379%\begin{eqnarray}[rcrcrcrl]
380% 10w & + & 3x & + & 3y & + & 18z & = 1 \\
381% 6w & - & 17x & & & - & 5z & = 2
382%\end{eqnarray}
383% \end{demo}
384%
385% Choosing a more up-to-date example, here's one demonstrating the \lit{:}
386% column specifier from the \textit{\LaTeX\ Companion}.
387%
388% \begin{demo}[w]{Lots of equations}
389%\begin{eqnarray}[rl:rl:l]
eafdddad 390% V_i &= v_i - q_i v_j, & X_i &= x_i - q_i x_j, &
86f6a31e 391% U_i = u_i, \qquad \mbox{for $i \ne j$} \label{eq:A} \\
392% V_j &= v_j, & X_j &= x_j &
393% U_j u_j + \sum_{i \ne j} q_i u_i.
394%\end{eqnarray}
395% \end{demo}
396%
397% We can make things more interesting by adding a plain text column. Here we
398% go:
399%
400% \begin{demo}[w]{Plain text column}
401%\begin{eqnarray}[rlqqTl]
402% x &= y & by (\ref{eq:A}) \\
403% x' &= y' & by definition \\
404% x + x' &= y + y' & by Axiom~1
405%\end{eqnarray}
406% \end{demo}
407%
408% The new features also mean that you don't need to mess about with
409% |\lefteqn| any more. This is handled by the \lit{L} column type:
410%
411% \begin{demo}{Splitting example}
412%\begin{eqnarray*}[Ll]
413% w+x+y+z = \\
4a655c6f 414% & a+b+c+d+e+{} \\
86f6a31e 415% & f+g+h+i+j
416%\end{eqnarray*}
417% \end{demo}
418%
419% Finally, just to prove that the spacing's right at last, here's another one
420% from the \textit{Companion}.
421%
422% \begin{demo}{Spacing demonstration}
423%\begin{equation}
424% x^2 + y^2 = z^2
425%\end{equation}
426%\begin{eqnarray}[rl]
427% x^2 + y^2 &= z^2 \\
428% y^2 &< z^2
429%\end{eqnarray}
430% \end{demo}
431%
432% Well, that was easy enough. Now on to numbering. As you've noticed, the
433% equations above are numbered. You can use the \env{eqnarray$*$}
434% environment to turn off the numbering in the whole environment, or say
435% |\nonumber| on a line to suppress numbering of that one in particular.
436% More excitingly, you can say \syntax{"\\nonumber["<text>"]"} to choose
437% what text to display.
438%
439% A note for cheats: you can use the sparkly new \env{eqnarray} for simple
440% equations simply by specifying \lit{x} as the column description. Who
441% needs \AmSTeX? |;-)|
442%
443% \end{old-eqnarray}
444%
445% \implementation
446%
447% \section{Implementation}
448%
449% This isn't really complicated (honest) although it is a lot hairier than I
450% think it ought to be.
451%
452% \begin{macrocode}
453%<*package>
4a655c6f 454\RequirePackage{amssymb}
455\RequirePackage{mdwkey}
86f6a31e 456% \end{macrocode}
457%
458% \subsection{Square roots}
459%
460% \subsubsection{Where is the square root sign?}
461%
462% \LaTeX\ hides the square root sign away somewhere without telling anyone
463% where it is. I extract it forcibly by peeking inside the |\sqrtsign| macro
464% and scrutinising the contents. Here we go: prepare for yukkiness.
465%
466% \begin{macrocode}
467\newcount\sq@sqrt \begingroup \catcode`\|0 \catcode`\\12
468|def|sq@readrad#1"#2\#3|relax{|global|sq@sqrt"#2|relax}
469|expandafter|sq@readrad|meaning|sqrtsign|relax |endgroup
470\def\sq@delim{\delimiter\sq@sqrt\relax}
471% \end{macrocode}
472%
473% \subsubsection{Drawing fake square root signs}
474%
475% \TeX\ absolutely insists on drawing square root signs with a vinculum over
476% the top. In order to get the same effect, we have to attempt to emulate
477% \TeX's behaviour.
478%
479% \begin{macro}{\sqrtdel}
480%
481% This does the main job of typesetting a vinculum-free radical.\footnote{^^A
482% Note for chemists: this is nothing to do with short-lived things which
483% don't have their normal numbers of electrons. And it won't reduce the
484% appearance of wrinkles either.}
485% It's more or less a duplicate of what \TeX\ does internally, so it might be
486% a good plan to have a copy of Appendix~G open while you examine this.
487%
488% We start off by using |\mathpalette| to help decide how big things should
489% be.
490%
491% \begin{macrocode}
492\def\sqrtdel{\mathpalette\sqrtdel@i}
493% \end{macrocode}
494%
495% Read the contents of the radical into a box, so we can measure it.
496%
497% \begin{macrocode}
498\def\sqrtdel@i#1#2{%
499 \setbox\z@\hbox{$\m@th#1#2$}% %%% Bzzzt -- uncramps the mathstyle
500% \end{macrocode}
501%
502% Now try and sort out the values needed in this calculation. We'll assume
503% that $\xi_8$ is 0.6\,pt, the way it usually is. Next try to work out the
504% value of $\varphi$.
505%
506% \begin{macrocode}
507 \ifx#1\displaystyle%
508 \@tempdima1ex%
509 \else%
510 \@tempdima.6\p@%
511 \fi%
512% \end{macrocode}
513%
514% That was easy. Now for $\psi$.
515%
516% \begin{macrocode}
517 \@tempdimb.6\p@%
518 \advance\@tempdimb.25\@tempdima%
519% \end{macrocode}
520%
521% Build the `delimiter' in a box of height $h(x)+d(x)+\psi+\xi_8$, as
522% requested. Box~2 will do well for this purpose.
523%
524% \begin{macrocode}
525 \dimen@.6\p@%
526 \advance\dimen@\@tempdimb%
527 \advance\dimen@\ht\z@%
528 \advance\dimen@\dp\z@%
529 \setbox\tw@\hbox{%
530 $\left\sq@delim\vcenter to\dimen@{}\right.\n@space$%
531 }%
532% \end{macrocode}
533%
534% Now we need to do some more calculating (don't you hate it?). As far as
535% Appendix~G is concerned, $\theta=h(y)=0$, because we want no rule over the
e8e9e5d8 536% top.
86f6a31e 537%
538% \begin{macrocode}
539 \@tempdima\ht\tw@%
540 \advance\@tempdima\dp\tw@%
541 \advance\@tempdima-\ht\z@%
542 \advance\@tempdima-\dp\z@%
543 \ifdim\@tempdima>\@tempdimb%
544 \advance\@tempdima\@tempdimb%
545 \@tempdimb.5\@tempdima%
546 \fi%
547% \end{macrocode}
548%
549% Work out how high to raise the radical symbol. Remember that Appendix~G
550% thinks that the box has a very small height, although this is untrue here.
551%
552% \begin{macrocode}
553 \@tempdima\ht\z@%
554 \advance\@tempdima\@tempdimb%
555 \advance\@tempdima-\ht\tw@%
556% \end{macrocode}
557%
558% Build the output (finally). The brace group is there to turn the output
559% into a mathord, one of the few times that this is actually desirable.
560%
561% \begin{macrocode}
562 {\raise\@tempdima\box\tw@\vbox{\kern\@tempdimb\box\z@}}%
563}
564% \end{macrocode}
565%
566% \end{macro}
567%
568% \subsubsection{The new square root command}
569%
570% This is where we reimplement all the square root stuff. Most of this stuff
571% comes from the \PlainTeX\ macros, although some is influenced by \AmSTeX\
572% and \LaTeXe, and some is original. I've tried to make the spacing vaguely
573% automatic, so although it's not configurable like \AmSTeX's version, the
574% output should look nice more of the time. Maybe.
575%
576% \begin{macro}{\sqrt}
577%
578% \LaTeX\ says this must be robust, so we make it robust. The first thing to
579% do is to see if there's a star and pass the appropriate squareroot-drawing
580% command on to the rest of the code.
581%
582% \begin{macrocode}
583\DeclareRobustCommand\sqrt{\@ifstar{\sqrt@i\sqrtdel}{\sqrt@i\sqrtsign}}
584% \end{macrocode}
585%
586% Now we can sort out an optional argument to be displayed on the root.
587%
588% \begin{macrocode}
589\def\sqrt@i#1{\@ifnextchar[{\sqrt@ii{#1}}{\sqrt@iv{#1}}}
590% \end{macrocode}
591%
592% Stages~2 and~3 below are essentially equivalents of \PlainTeX's
593% |\root|\dots|\of| and |\r@@t|. Here we also find the first wrinkle: the
594% |\rootbox| used to store the number is spaced out on the left if necessary.
595% There's a backspace after the end so that the root can slip underneath, and
596% everything works out nicely. Unfortunately size is fixed here, although
597% doesn't actually seem to matter.
598%
599% \begin{macrocode}
600\def\sqrt@ii#1[#2]{%
601 \setbox\rootbox\hbox{$\m@th\scriptscriptstyle{#2}$}%
602 \ifdim\wd\rootbox<6\p@%
603 \setbox\rootbox\hb@xt@6\p@{\hfil\unhbox\rootbox}%
604 \fi%
605 \mathpalette{\sqrt@iii{#1}}%
606}
607% \end{macrocode}
608%
609% Now we can actually build everything. Note that the root is raised by its
610% depth -- this prevents a common problem with letters with descenders.
611%
612% \begin{macrocode}
613\def\sqrt@iii#1#2#3{%
614 \setbox\z@\hbox{$\m@th#2#1{#3}$}%
615 \dimen@\ht\z@%
616 \advance\dimen@-\dp\z@%
617 \dimen@.6\dimen@%
618 \advance\dimen@\dp\rootbox%
619 \mkern-3mu%
620 \raise\dimen@\copy\rootbox%
621 \mkern-10mu%
622 \box\z@%
623}
624% \end{macrocode}
625%
626% Finally handle a non-numbered root. We read the rooted text in as an
627% argument, to stop problems when people omit the braces. (\AmSTeX\ does
628% this too.)
629%
630% \begin{macrocode}
631\def\sqrt@iv#1#2{#1{#2}}
632% \end{macrocode}
633%
634% \end{macro}
635%
636% \begin{macro}{\root}
637%
638% We also re-implement \PlainTeX's |\root| command, just in case someone uses
639% it, and supply a star-variant. This is all very trivial.
640%
641% \begin{macrocode}
642\def\root{\@ifstar{\root@i\sqrtdel}{\root@i\sqrtsign}}
643\def\root@i#1#2\of{\sqrt@ii{#1}[#2]}
644% \end{macrocode}
645%
646% \end{macro}
647%
648% \subsection{Modular programming}
649%
650% \begin{macro}{\pmod}
651%
652% Do some hacking if not |\ifouter|.
653%
654% \begin{macrocode}
655\def\pmod#1{%
656 \ifinner\;\else\allowbreak\mkern18mu\fi%
657 ({\operator@font mod}\,\,#1)%
658}
659% \end{macrocode}
660%
661% \end{macro}
662%
663% \subsection{Some magic new maths characters}
664%
665% \begin{macro}{\bitor}
666% \begin{macro}{\bitand}
667% \begin{macro}{\dblor}
668% \begin{macro}{\dbland}
669% \begin{macro}{\xor}
670% \begin{macro}{\lor}
671% \begin{macro}{\ror}
672% \begin{macro}{\lsl}
673% \begin{macro}{\lsr}
674%
675% The new boolean operators.
676%
677% \begin{macrocode}
678\DeclareMathSymbol{&}{\mathbin}{operators}{`\&}
679\DeclareMathSymbol{\bitand}{\mathbin}{operators}{`\&}
680\def\bitor{\mathbin\mid}
681\def\dblor{\mathbin{\mid\mid}}
682\def\dbland{\mathbin{\mathrel\bitand\mathrel\bitand}}
683\let\xor\oplus
684\def\lsl{\mathbin{<\!\!<}}
685\def\lsr{\mathbin{>\!\!>}}
686\def\rol{\mathbin{<\!\!<\!\!<}}
687\def\ror{\mathbin{>\!\!>\!\!>}}
688\AtBeginDocument{\ifx\lll\@@undefined\else
689 \def\lsl{\mathbin{\ll}}
690 \def\lsr{\mathbin{\gg}}
691 \def\rol{\mathbin{\lll}}
692 \def\ror{\mathbin{\ggg}}
693\fi}
694% \end{macrocode}
695%
696% \end{macro}
697% \end{macro}
698% \end{macro}
699% \end{macro}
700% \end{macro}
701% \end{macro}
702% \end{macro}
703% \end{macro}
704% \end{macro}
705%
706% \begin{macro}{\cat}
707% \begin{macro}{\compose}
708% \begin{macro}{\implies}
709% \begin{macro}{\vect}
710% \begin{macro}{\d}
711% \begin{macro}{\jacobi}
712%
713% A mixed bag of stuff.
714%
715% \begin{macrocode}
716\def\cat{\mathbin{\|}}
717\let\compose\circ
718\def\implies{\Rightarrow}
719\def\vect#1{\mathord{\mathbf{#1}}}
4a655c6f 720\def\d{%
721 \ifmmode\mathord{\operator@font d}%
722 \else\expandafter\a\expandafter d\fi%
723}
86f6a31e 724\def\jacobi#1#2{{{#1}\overwithdelims()#2}}
725% \end{macrocode}
726%
727% \end{macro}
728% \end{macro}
729% \end{macro}
730% \end{macro}
731% \end{macro}
732% \end{macro}
733%
734% \begin{macro}{\statclose}
735% \begin{macro}{\compind}
736%
737% Fancy new relations for probability distributions.
738%
739% \begin{macrocode}
740\def\statclose{\mathrel{\mathop{=}\limits^{\scriptscriptstyle s}}}
741\def\compind{\mathrel{\mathop{\approx}\limits^{\scriptscriptstyle c}}}
742% \end{macrocode}
743%
744% \end{macro}
745% \end{macro}
746%
747% \begin{macro}{\keys}
748% \begin{macro}{\dom}
749% \begin{macro}{\ran}
750% \begin{macro}{\supp}
751% \begin{macro}{\lcm}
4a655c6f 752% \begin{macro}{\poly}
753% \begin{macro}{\negl}
754% \begin{macro}{\ord}
86f6a31e 755%
756% And the new operator names.
757%
758% \begin{macrocode}
759\def\keys{\mathop{\operator@font keys}\nolimits}
760\def\dom{\mathop{\operator@font dom}\nolimits}
761\def\ran{\mathop{\operator@font ran}\nolimits}
762\def\supp{\mathop{\operator@font supp}\nolimits}
763\def\lcm{\mathop{\operator@font lcm}\nolimits}
4a655c6f 764\def\poly{\mathop{\operator@font poly}\nolimits}
765\def\negl{\mathop{\operator@font negl}\nolimits}
766\def\ord{\mathop{\operator@font ord}\nolimits}
86f6a31e 767% \end{macrocode}
768%
769% \end{macro}
770% \end{macro}
771% \end{macro}
772% \end{macro}
773% \end{macro}
4a655c6f 774% \end{macro}
775% \end{macro}
776% \end{macro}
777%
778% \subsection{Fractions}
779%
780% \begin{macro}{\@frac@parse}
781%
782% \syntax{"\\@frac@parse{"<stuff>"}{"<frac-params>"}"} -- run \<stuff>
783% passing it three arguments: an infix fraction-making command, the `outer'
784% style, and the `inner' style.
785%
786% This is rather tricky. We clear a load of parameters, parse the parameter
787% list, and then build a token list containing the right stuff. Without the
788% token list fiddling, we end up expanding things at the wrong times -- for
789% example, |\{| expands to something terribly unpleasant in a document
790% preamble.
791%
792% All of the nastiness is contained in a group.
793%
794% \begin{macrocode}
795\def\@frac@parse#1#2{%
796 \begingroup%
797 \let\@wd\@empty\def\@ldel{.}\def\@rdel{.}%
798 \def\@op{over}\let\@dim\@empty\@tempswafalse%
799 \let\@is\@empty\let\@os\@empty%
800 \mkparse{mdwmath:frac}{#2}%
801 \toks\tw@{\endgroup#1}%
802 \toks@\expandafter{\csname @@\@op\@wd\endcsname}%
803 \if@tempswa%
804 \toks@\expandafter{\the\expandafter\toks@\@ldel}%
805 \toks@\expandafter{\the\expandafter\toks@\@rdel}%
806 \fi%
807 \expandafter\toks@\expandafter{\the\expandafter\toks@\@dim}%
808 \toks@\expandafter{\the\toks\expandafter\tw@\expandafter{\the\toks@}}
809 \toks@\expandafter{\the\expandafter\toks@\expandafter{\@os}}
810 \toks@\expandafter{\the\expandafter\toks@\expandafter{\@is}}
811 \the\toks@%
812}
813% \end{macrocode}
814%
815% The keyword definitions are relatively straightforward now. The error
816% handling for \textsf{style} and \textsf{innerstyle} could do with
817% improvement.
818%
819% \begin{macrocode}
820\def\@frac@del#1#2{\def\@wd{withdelims}\@tempswatrue\def#1{#2}}
821\mkdef{mdwmath:frac}{leftdelim}{\@frac@del\@ldel{#1}}
822\mkdef{mdwmath:frac}{rightdelim}{\@frac@del\@rdel{#1}}
823\mkdef{mdwmath:frac}{nodelims}*{\let\@wd\@empty\@tempswafalse}
824\mkdef{mdwmath:frac}{line}{%
825 \def\@op{above}\setlength\dimen@{#1}\edef\@dim{\the\dimen@\space}%
826}
827\mkdef{mdwmath:frac}{line}*{\def\@op{over}\let\@dim\@empty}
828\mkdef{mdwmath:frac}{noline}*{\def\@op{atop}\let\@dim\@empty}
829\def\@frac@style#1#2{%
830 \ifx\q@delim#2\q@delim\let#1\@empty%
831 \else%
832 \expandafter\ifx\csname #2style\endcsname\relax%
833 \PackageError{mdwmath}{Bad maths style `#2'}\@ehc%
834 \else%
835 \edef#1{\csname#2style\endcsname}%
836 \fi%
837 \fi%
838}
839\mkdef{mdwmath:frac}{style}[]{\@frac@style\@os{#1}}
840\mkdef{mdwmath:frac}{innerstyle}[]{\@frac@style\@is{#1}}
841% \end{macrocode}
842%
843% \end{macro}
844%
845% \begin{macro}{\fracdef}
846%
847% Here's where the rest of the pain is. We do a preliminary parse of the
848% parameters and `compile' the result into the output macro. If there's no
849% optional argument, then we don't need to do any really tedious formatting
850% at the point of use.
851%
852% \begin{macrocode}
853\def\fracdef#1#2{\@frac@parse{\fracdef@i{#1}{#2}}{#2}}
854\def\fracdef@i#1#2#3#4#5{\def#1{\@frac@do{#2}{#3}{#4}{#5}}}
855\def\@frac@do#1#2#3#4{%
856 \@ifnextchar[{\@frac@complex{#1}}{\@frac@simple{#2}{#3}{#4}}%
857}
858\def\@frac@complex#1[#2]{\@frac@parse\@frac@simple{#1,#2}}
859\def\@frac@simple#1#2#3#4#5{{#2{{#3#4}#1{#3#5}}}}
860% \end{macrocode}
861%
862% \end{macro}
863%
864% \begin{macro}{\frac@fix}
865% \begin{macro}{\@@over}
866% \begin{macro}{\@@atop}
867% \begin{macro}{\@@above}
868% \begin{macro}{\@@overwithdelims}
869% \begin{macro}{\@@atopwithdelims}
870% \begin{macro}{\@@abovewithdelims}
871%
872% Finally, we need to fix up |\@@over| and friends. Maybe \package{amsmath}
873% has hidden the commands away somewhere unhelpful. If not, we make the
874% requisite copies.
875%
876% \begin{macrocode}
877\def\q@delim{\q@delim}
878\def\frac@fix#1{\expandafter\frac@fix@i\string#1\q@delim}
879\def\frac@fix@i#1#2\q@delim{\frac@fix@ii{#2}\frac@fix@ii{#2withdelims}}
880\def\frac@fix@ii#1{%
881 \expandafter\ifx\csname @@#1\endcsname\relax%
882 \expandafter\let\csname @@#1\expandafter\endcsname\csname#1\endcsname%
883 \fi%
884}
885\frac@fix\over \frac@fix\atop \frac@fix\above
886% \end{macrocode}
887%
888% \end{macro}
889% \end{macro}
890% \end{macro}
891% \end{macro}
892% \end{macro}
893% \end{macro}
894% \end{macro}
895%
896% \begin{macro}{\frac}
897% \begin{macro}{\binom}
898% \begin{macro}{\jacobi}
899%
900% And finally, we define the fraction-making commands.
901%
902% \begin{macrocode}
903\fracdef\frac{nodelims, line}
904\fracdef\binom{leftdelim = (, rightdelim = ), noline}
905\fracdef\jacobi{leftdelim = (, rightdelim = ), line}
906% \end{macrocode}
907%
908% \end{macro}
909% \end{macro}
910% \end{macro}
86f6a31e 911%
912% \subsection{Blackboard bold stuff}
913%
914% \begin{macro}{\Z}
915% \begin{macro}{\Q}
916% \begin{macro}{\R}
917% \begin{macro}{\C}
918% \begin{macro}{\N}
919% \begin{macro}{\F}
920% \begin{macro}{\powerset}
4a655c6f 921% \begin{macro}{\gf}
86f6a31e 922%
923% First of all, the signs.
924%
925% \begin{macrocode}
926\def\Z{\mathbb{Z}}
927\def\Q{\mathbb{Q}}
928\def\R{\mathbb{R}}
929\def\C{\mathbb{C}}
930\def\N{\mathbb{N}}
931\def\F{\mathbb{F}}
932\def\powerset{\mathbb{P}}
4a655c6f 933\def\gf#1{\F_{#1}}
934%\def\gf#1{\mathrm{GF}({#1})}
86f6a31e 935% \end{macrocode}
936%
937% \end{macro}
938% \end{macro}
939% \end{macro}
940% \end{macro}
941% \end{macro}
942% \end{macro}
943% \end{macro}
4a655c6f 944% \end{macro}
86f6a31e 945%
946% And now, define |\mathbb| if it's not there already.
947%
948% \begin{macrocode}
949\AtBeginDocument{\ifx\mathbb\@@undefined\let\mathbb\mathbf\fi}
950% \end{macrocode}
951%
952% \subsection{Biggles}
953%
954% Now for some user-controlled delimiter sizing. The standard bigness of
955% plain \TeX's delimiters are all right, but it's a little limiting.
956%
957% The biggness of delimiters is based on the size of the current |\strut|,
958% which \LaTeX\ keeps up to date all the time. This will make the various
959% delimiters grow in proportion when the text gets bigger. Actually, I'm
960% not sure that this is exactly right -- maybe it should be nonlinear,
961%
962% \begin{macro}{\bbigg}
963% \begin{macro}{\bbiggl}
964% \begin{macro}{\bbiggr}
965% \begin{macro}{\bbiggm}
966%
967% This is where the bigness is done. This is more similar to the plain \TeX\
968% big delimiter stuff than to the \package{amsmath} stuff, although there's
969% not really a lot of difference.
970%
971% The two arguments are a multiplier for the delimiter size, and a small
972% increment applied \emph{before} the multiplication (which is optional).
973%
974% This is actually a front for a low-level interface which can be called
975% directly for efficiency.
976%
977% \begin{macrocode}
978\def\bbigg{\@bbigg\mathord} \def\bbiggl{\@bbigg\mathopen}
979\def\bbiggr{\@bbigg\mathclose} \def\bbiggm{\@bbigg\mathrel}
980% \end{macrocode}
981%
982% \end{macro}
983% \end{macro}
984% \end{macro}
985% \end{macro}
986%
987% \begin{macro}{\@bbigg}
988%
989% This is an optional argument parser providing a front end for the main
990% macro |\bbigg@|.
991%
992% \begin{macrocode}
993\def\@bbigg#1{\@ifnextchar[{\@bigg@i{#1}}{\@bigg@i{#1}[\z@]}}
994\def\@bigg@i#1[#2]#3#4{#1{\bbigg@{#2}{#3}{#4}}}
995% \end{macrocode}
996%
997% \end{macro}
998%
999% \begin{macro}{\bbigg@}
1000%
1001% This is it, at last. The arguments are as described above: an addition
1002% to be made to the strut height, and a multiplier. Oh, and the delimiter,
1003% of course.
1004%
1005% This is a bit messy. The smallest `big' delimiter, |\big|, is the same
1006% height as the current strut box. Other delimiters are~$1\frac12$, $2$
1007% and~$2\frac12$ times this height. I'll set the height of the delimiter by
1008% putting in a |\vcenter| of the appropriate size.
1009%
1010% Given an extra height~$x$, a multiplication factor~$f$ and a strut
1011% height~$h$ and depth~$d$, I'll create a vcenter with total height
1012% $f(h+d+x)$. Easy, isn't it?
1013%
1014% \begin{macrocode}
1015\def\bbigg@#1#2#3{%
1016 {\hbox{$%
1017 \dimen@\ht\strutbox\advance\dimen@\dp\strutbox%
1018 \advance\dimen@#1%
1019 \dimen@#2\dimen@%
1020 \left#3\vcenter to\dimen@{}\right.\n@space%
1021 $}}%
1022}
1023% \end{macrocode}
1024%
1025% \end{macro}
1026%
1027% \begin{macro}{\big}
1028% \begin{macro}{\Big}
1029% \begin{macro}{\bigg}
1030% \begin{macro}{\Bigg}
1031%
1032% Now for the easy macros.
1033%
1034% \begin{macrocode}
1035\def\big{\bbigg@\z@\@ne}
1036\def\Big{\bbigg@\z@{1.5}}
1037\def\bigg{\bbigg@\z@\tw@}
1038\def\Bigg{\bbigg@\z@{2.5}}
1039% \end{macrocode}
1040%
1041% \end{macro}
1042% \end{macro}
1043% \end{macro}
1044% \end{macro}
1045%
1046% \subsection{The `QED' symbol}
1047%
1048% \begin{macro}{\qed}
1049% \begin{macro}{\qedrule}
1050% \begin{macro}{\qedsymbol}
1051%
1052% This is fairly simple. Just be careful will the glue and penalties. The
1053% size of the little box is based on the current font size.
1054%
1055% The horizontal list constructed by the macro is like this:
1056%
1057% \begin{itemize}
1058% \item A |\quad| of space. This might get eaten if there's a break here or
1059% before. That's OK, though.
1060% \item An empty box, to break a run of discardable items.
1061% \item A |\penalty 10000| to ensure that the spacing glue isn't discarded.
1062% \item |\hfill| glue to push the little rule to the end of the line.
1063% \item A little square rule `\qedrule', with some small kerns around it.
1064% \item A glue item to counter the effect of glue added at the paragraph
e8e9e5d8 1065% boundary.
86f6a31e 1066% \end{itemize}
1067%
4a655c6f 1068% The vertical mode case is simpler, but less universal. It copes with
1069% relatively simple cases only.
1070%
86f6a31e 1071% A |\qed| commend ends the paragraph.
1072%
1073% \begin{macrocode}
4a655c6f 1074\def\qed{%
1075 \ifvmode%
1076 \unskip%
1077 \setbox\z@\hb@xt@\linewidth{\hfil\strut\qedsymbol}%
1078 \prevdepth-\@m\p@%
1079 \ifdim\prevdepth>\dp\strutbox%
1080 \dimen@\prevdepth\advance\dimen@-\dp\strutbox%
1081 \kern-\dimen@%
1082 \fi%
1083 \penalty\@M\vskip-\baselineskip\box\z@%
1084 \else%
1085 \unskip%
1086 \penalty\@M\hfill%
1087 \hbox{}\penalty200\quad%
1088 \hbox{}\penalty\@M\hfill\qedsymbol\hskip-\parfillskip\par%
1089 \fi%
86f6a31e 1090}
1091\def\qedrule{{%
1092 \dimen@\ht\strutbox%
4a655c6f 1093 \advance\dimen@\dp\strutbox%
86f6a31e 1094 \dimen@ii1ex%
1095 \advance\dimen@-\dimen@ii%
1096 \divide\dimen@\tw@%
1097 \advance\dimen@-\dp\strutbox%
1098 \advance\dimen@\dimen@ii%
1099 \advance\dimen@ii-\dimen@%
1100 \kern\p@%
1101 \vrule\@width1ex\@height\dimen@\@depth\dimen@ii%
1102 \kern\p@%
1103}}
1104\providecommand\qedsymbol{\qedrule}
1105% \end{macrocode}
1106%
1107% \end{macro}
1108% \end{macro}
1109% \end{macro}
1110%
3ba7380e
MW
1111% \subsection{Punctuation in displays}
1112%
1113% \begin{macro}{\mpunct}
1114%
1115% This is actually a little more subtle than you'd expect. If the
1116% \package{amstext} package is loaded, or something else has defined the
1117% |\text| command, then we should use that; otherwise, just drop a box in and
1118% hope for the best.
1119%
1120% \begin{macrocode}
1121\def\mpunct#1{%
1122 \,%
1123 \ifx\text\@@undefined\hbox%
1124 \else\expandafter\text\fi%
1125 {#1}%
1126}
1127% \end{macrocode}
1128%
1129%\end{macro}
1130%
86f6a31e 1131% \begin{ignore}
1132% The following is the original definition of the enhanced eqnarray
1133% environment. It's not supported, although if you can figure out how to
1134% extract it, it's all yours.
1135% \end{ignore}
1136%
1137% \begin{old-eqnarray}
1138%
1139% \subsection{The sparkly new \env{eqnarray}}
1140%
1141% Start off by writing a different package.
1142%
1143% \begin{macrocode}
1144%</package>
1145%<*oldeqnarray>
1146% \end{macrocode}
1147%
1148% \subsubsection{Options handling}
1149%
1150% We need to be able to cope with \textsf{fleqn} and \textsf{leqno} options.
1151% This will adjust our magic modified \env{eqnarray} environment
1152% appropriately.
1153%
1154% \begin{macrocode}
1155\newif\if@fleqn
1156\newif\if@leqno
1157\DeclareOption{fleqn}{\@fleqntrue}
1158\DeclareOption{leqno}{\@leqnotrue}
1159\ProcessOptions
1160% \end{macrocode}
1161%
1162% This is all really different to the \LaTeX\ version. I've looked at the
1163% various \env{tabular} implementations, the original \env{eqnarray} and the
1164% \textit{\TeX book} to see how best to do this, and then went my own way.
1165% If it doesn't work it's all my fault.
1166%
1167% \subsubsection{Some useful registers}
1168%
1169% The old \LaTeX\ version puts the equation numbers in by keeping a count of
1170% where it is in the alignment. Since I don't know how may columns there are
1171% going to be, I'll just use a switch in the preamble to tell me to stop
1172% tabbing.
1173%
1174% \begin{macrocode}
1175\newif\if@eqalast
1176% \end{macrocode}
1177%
1178% Now define some useful length parameters. First allocate them:
1179%
1180% \begin{macrocode}
1181\newskip\eqaopenskip
1182\newskip\eqacloseskip
1183\newskip\eqacolskip
1184\newskip\eqainskip
1185% \end{macrocode}
1186%
1187% Now assign some default values. Users can play with these if they really
1188% want although I can't see the point myself.
1189%
1190% \begin{macrocode}
1191\if@fleqn
1192 \AtBeginDocument{\eqaopenskip\leftmargini}
1193\else
1194 \eqaopenskip\@centering
1195\fi
1196\eqacloseskip\@centering
1197\eqacolskip\@centering
1198\eqainskip\z@
1199% \end{macrocode}
1200%
1201% We allow the user to play with the style if this is really wanted. I dunno
1202% why, really. Maybe someone wants very small alignments.
1203%
1204% \begin{macrocode}
1205\let\eqa@style\displaystyle
1206% \end{macrocode}
1207%
1208% \subsubsection{The main environments}
1209%
1210% We define the toplevel commands here. They just add in default arguments
1211% and then call |\@eqnarray| with a preamble string. The only difference is
1212% the last column they add in -- \env{eqnarray$*$} throws away the last
1213% column by sticking it in box~0. (I used to |\@gobble| it but that caused
1214% the |\cr| to be lost.)
1215%
1216% \begin{macrocode}
1217\def\eqnarray{\@ifnextchar[\eqnarray@i{\eqnarray@i[rcl]}}
1218\def\eqnarray@i[#1]{%
1219 \@eqnarray{#1!{\hb@xt@\z@{\hss##}\tabskip\z@}}
1220}
1221\@namedef{eqnarray*}{\@ifnextchar[\eqnarray@s@i{\eqnarray@s@i[rcl]}}
1222\def\eqnarray@s@i[#1]{%
1223 \@eqnarray{#1!{\nonumber\setbox\z@\hbox{##}\tabskip\z@}}%
1224}
1225% \end{macrocode}
1226%
1227% \subsubsection{Set up the initial display}
1228%
1229% \begin{macro}{\@eqnarray}
1230%
1231% The |\@eqnarray| command does most of the initial work. It sets up some
1232% flags and things, builds the |\halign| preamble, and returns.
1233%
1234% \begin{macrocode}
1235\def\@eqnarray#1{%
1236% \end{macrocode}
1237%
1238% Start playing with the counter here. The original does some icky internal
1239% playing, which isn't necessary. The |\if@eqnsw| switch is |true| if the
1240% user hasn't supplied an equation number. The |\if@eqalast| switch is
1241% |true| in the final equation-number column.
1242%
1243% \begin{macrocode}
1244 \refstepcounter{equation}%
1245 \@eqalastfalse%
1246 \global\@eqnswtrue%
1247 \m@th%
1248% \end{macrocode}
1249%
1250% Set things up for the |\halign| which is coming up.
1251%
1252% \begin{macrocode}
1253 \openup\jot%
1254 \tabskip\eqaopenskip%
1255 \let\\\@eqncr%
1256 \everycr{}%
1257 $$%
1258% \end{macrocode}
1259%
1260% We'll build the real |\halign| and preamble in a token register. All we
1261% need to do is stuff the header in the token register, clear a switch
1262% (that'll be explained later), parse the preamble and then expand the
1263% tokens we collected. Easy, no?
1264%
1265% \begin{macrocode}
1266 \toks@{\halign to\displaywidth\bgroup}%
1267 \@tempswafalse%
1268 \eqa@preamble#1\end%
1269 \the\toks@\cr%
1270}
1271% \end{macrocode}
1272%
1273% \end{macro}
1274%
1275% \subsubsection{Parsing the preamble}
1276%
1277% All this actually involves is reading the next character and building a
1278% command from it. That can pull off an argument if it needs it. Just make
1279% sure we don't fall off the end and we'll be OK.
1280%
1281% \begin{macrocode}
1282\def\eqa@preamble#1{%
1283 \ifx\end#1\else\csname eqa@char@#1\expandafter\endcsname\fi%
1284}
1285% \end{macrocode}
1286%
1287% Adding stuff to the preamble tokens is a simple matter of using
1288% |\expandafter| in the correct way.\footnote{^^A
1289% I have no idea why \LaTeX\ uses \cmd\edef\ for building its preamble. It
1290% seems utterly insane to me -- the amount of bodgery that \env{tabular}
1291% has to go through to make everything expand at the appropriate times is
1292% scary. Maybe Messrs~Lamport and Mittelbach just forgot about token
1293% registers when they were writing the code. Maybe I ought to rewrite the
1294% thing properly some time. Sigh.
1295%
1296% As a sort of postscript to the above, I \emph{have} rewritten the
1297% \env{tabular} environment, and made a damned fine job of it, in my
1298% oh-so-humble opinion. All this \env{eqnarray} stuff has been remoulded
1299% in terms of the generic column-defining things in \package{mdwtab}.
1300% You're reading the documentation of the old version, which isn't
1301% supported any more, so any bugs here are your own problem.}
1302%
1303% \begin{macrocode}
1304\def\eqa@addraw#1{\expandafter\toks@\expandafter{\the\toks@#1}}
1305% \end{macrocode}
1306%
1307% Now for some cleverness again. In order to put all the right bits of
1308% |\tabskip| glue in the right places we must \emph{not} terminate each
1309% column until we know what the next one is. We set |\if@tempswa| to be
1310% |true| if there's a column waiting to be closed (so it's initially
1311% |false|). The following macro adds a column correctly, assuming we're in
1312% a formula. Other column types make their own arrangements.
1313%
1314% \begin{macrocode}
1315\def\eqa@add#1{%
1316 \if@tempswa%
1317 \eqa@addraw{\tabskip\eqainskip&#1}%
1318 \else%
1319 \eqa@addraw{#1}%
1320 \fi%
1321 \@tempswatrue%
1322}
1323% \end{macrocode}
1324%
1325% Now to defining column types. Let's define a macro which allows us to
1326% define column types:
1327%
1328% \begin{macrocode}
1329\def\eqa@def#1{\expandafter\def\csname eqa@char@#1\endcsname}
1330% \end{macrocode}
1331%
1332% Now we can define the column types. Each column type must loop back to
1333% |\eqa@preamble| once it's finished, to read the rest of the preamble
1334% string. Note the positioning of ord atoms in the stuff below. This will
1335% space out relations and binops correctly when they occur at the edges of
1336% columns, and won't affect ord atoms at the edges, because ords pack
1337% closely.
1338%
1339% First the easy onces. Just stick |\hfil| in the right places and
1340% everything will be all right.
1341%
1342% \begin{macrocode}
1343\eqa@def r{\eqa@add{\hfil$\eqa@style##{}$}\eqa@preamble}
1344\eqa@def c{\eqa@add{\hfil$\eqa@style{}##{}$\hfil}\eqa@preamble}
1345\eqa@def l{\eqa@add{$\eqa@style{}##$\hfil}\eqa@preamble}
1346\eqa@def x{\eqa@add{\hfil$\eqa@style##$\hfil}\eqa@preamble}
1347% \end{macrocode}
1348%
1349% Now for the textual ones. This is also fairly easy.
1350%
1351% \begin{macrocode}
1352\eqa@def T#1{%
1353 \eqa@add{}%
1354 \if#1l\else\eqa@addraw{\hfil}\fi%
1355 \eqa@addraw{##}%
1356 \if#1r\else\eqa@addraw{\hfil}\fi%
1357 \eqa@preamble%
1358}
1359% \end{macrocode}
1360%
1361% Sort of split types of equations. I mustn't use |\rlap| here, or
1362% everything goes wrong -- |\\| doesn't get noticed by \TeX\ in the same way
1363% as |\cr| does.
1364%
1365% \begin{macrocode}
1366\eqa@def L{\eqa@add{\hb@xt@\z@{$\eqa@style##$\hss}\qquad}\eqa@preamble}
1367% \end{macrocode}
1368%
1369% The \lit{:} column type is fairly simple. We set |\tabskip| up to make
1370% lots of space and close the current column, because there must be one.^^A
1371% \footnote{This is an assumption.}
1372%
1373% \begin{macrocode}
1374\eqa@def :{%
1375 \eqa@addraw{\tabskip\eqacolskip&}\@tempswafalse\eqa@preamble%
1376}
1377\eqa@def q{\eqa@add{\quad}\@tempswafalse\eqa@preamble}
1378% \end{macrocode}
1379%
1380% The other column types just insert given text in an appropriate way.
1381%
1382% \begin{macrocode}
1383\eqa@def >#1{\eqa@add{#1}\@tempswafalse\eqa@preamble}
1384\eqa@def <#1{\eqa@addraw{#1}\eqa@preamble}
1385% \end{macrocode}
1386%
1387% Finally, the magical \lit{!} column type, which sets the equation number.
1388% We set up the |\tabskip| glue properly, tab on, and set the flag which
1389% marks the final column.
1390%
1391% \begin{macrocode}
1392\eqa@def !#1{%
1393 \eqa@addraw{\tabskip\eqacloseskip&\@eqalasttrue#1}\eqa@preamble%
1394}
1395% \end{macrocode}
1396%
1397% \subsubsection{Newline codes}
1398%
1399% Newline sequences (|\\|) get turned into calls of |\@eqncr|. The job is
1400% fairly simple, really. However, to avoid reading `|&|' characters
1401% prematurely, we set up a magic brace (from the \package{array} package --
1402% this avoids creating ord atoms and other nastyness).
1403%
1404% \begin{macrocode}
1405\def\@eqncr{%
1406 \iffalse{\fi\ifnum0=`}\fi%
1407 \@ifstar{\eqacr@i{\@M}}{\eqacr@i{\interdisplaylinepenalty}}%
1408}
1409\def\eqacr@i#1{\@ifnextchar[{\eqacr@ii{#1}}{\eqacr@ii{#1}[\z@]}}
1410\def\eqacr@ii#1[#2]{%
1411 \ifnum0=`{}\fi%
1412 \eqa@eqnum%
1413 \noalign{\penalty#1\vskip#2\relax}%
1414}
1415% \end{macrocode}
1416%
1417% \subsubsection{Setting equation numbers}
1418%
1419% Before we start, we need to generalise the flush-left number handling bits.
1420% The macro |\eqa@eqpos| will put its argument in the right place.
1421%
1422% \begin{macrocode}
1423\if@leqno
1424 \def\eqa@eqpos#1{%
1425 \hb@xt@.01\p@{}\rlap{\normalfont\normalcolor\hskip-\displaywidth#1}%
1426 }
1427\else
1428 \def\eqa@eqpos#1{\normalfont\normalcolor#1}
1429\fi
1430% \end{macrocode}
1431%
1432% First we need to move into the right column. Then we just set the equation
1433% number appropriately. There is some subtlety here, ish. The |\relax| is
1434% important, to delay expansion of the |\if|\dots\ until the new column has
1435% been started. The two helper macros are important too, to hide `|&|'s and
1436% `|\cr|'s from \TeX's scanner until the right time.
1437%
1438% \begin{macrocode}
1439\def\eqa@eqnum{%
1440 \relax%
1441 \if@eqalast\expandafter\eqa@eqnum@i\else\expandafter\eqa@eqnum@ii\fi%
1442}
1443\def\eqa@eqnum@i{%
1444 \if@eqnsw%
1445 \eqa@eqpos{(\theequation)}\stepcounter{equation}%
1446 \else%
1447 \eqa@eqpos\eqa@number%
1448 \fi%
1449 \global\@eqnswtrue%
1450 \cr%
1451}
1452\def\eqa@eqnum@ii{&\eqa@eqnum}
1453% \end{macrocode}
1454%
1455% \subsubsection{Numbering control}
1456%
1457% This is trivial. We set the |\if@eqnsw| flag to be |false| and store the
1458% text in a macro.
1459%
1460% \begin{macrocode}
1461\let\nonumber\relax
1462\newcommand\nonumber[1][]{\global\@eqnswfalse\global\def\eqa@number{#1}}
1463% \end{macrocode}
1464%
1465% \subsubsection{Closing the environments off}
1466%
1467% This is really easy. Set the final equation number, close the |\halign|,
1468% tidy up the equation counter (it's been stepped once too many times) and
1469% close the display.
1470%
1471% \begin{macrocode}
1472\def\endeqnarray{%
1473 \eqa@eqnum%
1474 \egroup%
1475 \global\advance\c@equation\m@ne%
1476 $$%
1477 \global\@ignoretrue%
1478}
1479\expandafter\let\csname endeqnarray*\endcsname\endeqnarray
1480% \end{macrocode}
1481%
1482% Now start up the other package again.
1483%
1484% \begin{macrocode}
1485%</oldeqnarray>
1486%<*package>
1487% \end{macrocode}
1488%
1489% \end{old-eqnarray}
1490%
1491% That's all there is. Byebye.
1492%
1493% \begin{macrocode}
1494%</package>
1495% \end{macrocode}
1496%
1497% \hfill Mark Wooding, \today
1498%
1499% \Finale
1500\endinput