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