X-Git-Url: https://git.distorted.org.uk/~mdw/mdwtools/blobdiff_plain/86f6a31e4b41acb71ba37f3838a9ce75f7743330..39bf771aebd339019d1f11c0c0f60d30aab4ddc4:/mdwmath.dtx

diff --git a/mdwmath.dtx b/mdwmath.dtx
index ee6ed1c..01be386 100644
--- a/mdwmath.dtx
+++ b/mdwmath.dtx
@@ -1,27 +1,17 @@
 % \begin{meta-comment}
 %
-% $Id: mdwmath.dtx,v 1.1 2002/02/03 20:49:03 mdw Exp $
+% $Id: mdwmath.dtx,v 1.2 2003/09/05 16:14:36 mdw Exp $
 %
 % Various nicer mathematical things
 %
-% (c) 1996 Mark Wooding
-%
-%----- Revision history -----------------------------------------------------
-%
-% $Log: mdwmath.dtx,v $
-% Revision 1.1  2002/02/03 20:49:03  mdw
-% Checkin for new build system.
-%
-% Revision 1.1  1996/11/19 20:53:21  mdw
-% Initial revision
-%
+% (c) 2003 Mark Wooding
 %
 % \end{meta-comment}
 %
 % \begin{meta-comment} <general public licence>
 %%
 %% mdwmath package -- various nicer mathematical things
-%% Copyright (c) 1996 Mark Wooding
+%% Copyright (c) 2003 Mark Wooding
 %%
 %% This program is free software; you can redistribute it and/or modify
 %% it under the terms of the GNU General Public License as published by
@@ -42,13 +32,13 @@
 % \begin{meta-comment} <Package preamble>
 %<+package>\NeedsTeXFormat{LaTeX2e}
 %<+package>\ProvidesPackage{mdwmath}
-%<+package>                [2000/07/02 1.2 Nice mathematical things]
+%<+package>                [2003/08/25 1.3 Nice mathematical things]
 %<+oldeqnarray>\NeedsTeXFormat{LaTeX2e}
 %<+oldeqnarray>\ProvidesPackage{eqnarray}
 %<+oldeqnarray>                [1996/04/11 1.1 Old enhanced eqnarray]
 % \end{meta-comment}
 %
-% \CheckSum{465}
+% \CheckSum{729}
 %% \CharacterTable
 %%  {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
 %%   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
@@ -73,9 +63,9 @@
 \let\opmod\pmod
 \usepackage{amssymb}
 \describespackage{mdwmath}
-% \describespackage{eqnarray}
+%\describespackage{eqnarray}
 \ignoreenv{old-eqnarray}
-% \unignoreenv{old-eqnarray}
+%\unignoreenv{old-eqnarray}
 \mdwdoc
 %</driver>
 %
@@ -161,9 +151,50 @@
 % `statistical closeness' of probability distributions; |\compind| typesets
 % `$\compind$', which indicates computational indistinguishability.
 %
+% \subsection{Fractions}
+%
+% \DescribeMacro\fracdef
+% We provide a general fraction system, a little tiny bit like
+% \package{amsmath}'s |\genfrac|.  Say
+% \syntax{"\\fracdef{"<name>"}{"<frac-params>"}"} to define a new
+% |\frac|-like operator.  The \<frac-params> are a comma-separated list of
+% parameters:
+% \begin{description}
+% \item[\lit*{line}] Include a horizontal line between the top and bottom
+%   (like |\frac|).
+% \item[\lit*{line=}\<length>] Include a horizontal line with width
+%   \<length>.
+% \item[\lit*{noline}] Don't include a line (like |\binom|).
+% \item[\lit*{leftdelim=}\<delim>] Use \<delim> as the left-hand delimiter.
+% \item[\lit*{rightdelim=}\<delim>] Use \<delim> as the right-hand delimiter.
+% \item[\lit*{nodelims}] Don't include delimiters.
+% \item[\lit*{style=}\<style>] Typeset the fraction in \<style>, which is one
+%   of |display|, |text|, |script| or |scriptscript|.
+% \item[\lit*{style}] Use the prevailing style for the fraction.
+% \item[\lit*{innerstyle=}\<style>] Typeset the \emph{components} of the
+%   fraction in \<style>.
+% \item[\lit*{innerstyle}] Typeset the fraction components according to the
+%   prevailing style.
+% \end{description}
+% The commands created by |\fracdef| have the following syntax:
+% \syntax{<name>"["<frac-params>"]{"<top>"}{"<bottom>"}"}.  Thus, you can use
+% the optional argument to `tweak' the fraction if necessary.  This isn't
+% such a good idea to do often.
+%
+% \DescribeMacro\frac
+% \DescribeMacro\binom
 % \DescribeMacro\jacobi
-% The |\jacobi{x}{n}| command typesets `$\jacobi{x}{n}$', which may be of use
-% to number theorists talking about Jacobi or Lagrange symbols.
+% The macros |\frac|, |\binom| and |\jacobi| are defined using |\fracdef|.
+% They typset $\frac{x}{y}$, $\binom{n}{k}$ and $\jacobi{x}{n}$ respectively.
+% (The last may be of use to number theorists talking about Jacobi or
+% Lagrange symbols.)
+%
+% By way of example, these commands were defined using
+%\begin{verbatim}
+%\fracdef\frac{nodelims, line}
+%\fracdef\binom{leftdelim = (, rightdelim = ), noline}
+%\fracdef\jacobi{leftdelim = (, rightdelim = ), line}
+%\end{verbatim}
 %
 % \subsection{Rant about derivatives}
 %
@@ -173,8 +204,8 @@
 % \[ \frac{dy}{dx} \]
 % while the British want
 % \[ \frac{\d y}{\d x}. \]
-% The command |\d| command is fixed to typeset a `$\d$'.  If you want the
-% accent, try |\a{d}{x}|, which typesets `\a{d}{x}'.
+% The command |\d| command is fixed to typeset a `$\d$'.  (In text mode,
+% |\d{x}| still typesets `\d{x}'.)
 %
 % \subsection{New operator names}
 %
@@ -183,17 +214,27 @@
 % \DescribeMacro\ran
 % \DescribeMacro\supp
 % \DescribeMacro\lcm
+% \DescribeMacro\ord
+% \DescribeMacro\poly
+% \DescribeMacro\negl
 % A few esoteric new operator names are supplied.
 % \begin{center} \unverb\| \begin{tabular}{cl|cl|cl}
 % $\keys$ & "\keys"	& $\dom$ & "\dom"	& $\ran$ & "\ran" \\
-% $\supp$ & "\supp"	& $\lcm$ & "\lcm"
+% $\supp$ & "\supp"	& $\lcm$ & "\lcm"       & $\ord$ & "\ord" \\
+% $\poly$ & "\poly"	& $\negl$ & "\negl"
 % \end{tabular} \end{center}
 % I think |\lcm| ought to be self-explanatory.  The |\dom| and |\ran|
 % operators pick out the domain and range of a function, respectively; thus,
 % if $F\colon X \to Y$ is a function, then $\dom F = X$ and $\ran F = Y$.
 % The \emph{support} of a probability distribution $\mathcal{D}$ is the set
 % of objects with nonzero probability; i.e., $\supp{D} = \{\, x \in
-% \dom\mathcal{D} \mid \mathcal{D}(x) > 0 \,\}$.
+% \dom\mathcal{D} \mid \mathcal{D}(x) > 0 \,\}$.  If $g \in G$ is a group
+% element then $\ord g$ is the \emph{order} of $g$; i.e., the smallest
+% positive integer $i$ where $g^i$ is the identity element, or $0$ if there
+% is no such $i$.  $\poly(n)$ is some polynomial function of $n$.  A function
+% $\nu(\cdot)$ is \emph{negligible} if, for every polynomial function
+% $p(\cdot)$, there is an integer $N$ such that $\nu(n) < 1/p(n)$ for all $n
+% > N$; $\negl(n)$ is some negligible function of $n$.
 %
 % \subsection{Standard set names}
 %
@@ -204,13 +245,36 @@
 % \DescribeMacro\N
 % \DescribeMacro\F
 % \DescribeMacro\powerset
+% \DescribeMacro\gf
 % If you have a |\mathbb| command defined, the following magic is revealed:
 % \begin{center} \unverb\| \begin{tabular}{cl|cl|cl}
 % $\Z$ & "\Z" & $\Q$ & "\Q" & $\R$ & "\R" \\
 % $\N$ & "\N" & $\F$ & "\F" & $\C$ & "\C"
 % \end{tabular} \end{center}
 % which are handy for various standard sets of things.  Also the |\powerset|
-% command typesets `$\powerset$'.
+% command typesets `$\powerset$', and \syntax{"\\gf{"<q>"}"}, which by default
+% typesets $\gf{\syntax{<q>}}$ but you might choose to have it set
+% $\mathrm{GF}(\syntax{<q>})$ intead.
+%
+% \subsection{Biggles}
+%
+% \DescribeMacro\bbigg
+% \DescribeMacro\bbiggl
+% \DescribeMacro\bbiggr
+% \DescribeMacro\bbiggm
+% The |\bbigg| commands generalizes the Plain \TeX\ |\bigg| family of
+% macros.  |\bbigg| produces an `ordinary' symbol; |\bbiggl| and |\bbiggr|
+% produce left and right delimiters; and |\bbiggm| produces a relation.  They
+% produce symbols whose size is related to the prevailing text size -- so
+% they adjust correctly in chapter headings, for example.
+%
+% The syntax is straightforward:
+% \syntax{"\\"<bigop>"["$a$"]{"$n$"}{"<delim>"}"}.  Describing it is a bit
+% trickier.  The size is based on the current |\strut| height.  If |\strut|
+% has a height of $h$ and a depth of $d$, then the delimiter produced has a
+% height of $n \times (h + d + a)$.
+%
+% The old |\big| commands have been redefined in terms of |\bbigg|.
 %
 % \subsection{The `QED' symbol}
 %
@@ -320,7 +384,7 @@
 % \begin{demo}{Splitting example}
 %\begin{eqnarray*}[Ll]
 %   w+x+y+z = \\
-%    & a+b+c+d+e+ \\
+%    & a+b+c+d+e+{} \\
 %    & f+g+h+i+j
 %\end{eqnarray*}
 % \end{demo}
@@ -360,6 +424,8 @@
 %
 %    \begin{macrocode}
 %<*package>
+\RequirePackage{amssymb}
+\RequirePackage{mdwkey}
 %    \end{macrocode}
 %
 % \subsection{Square roots}
@@ -624,7 +690,10 @@
 \let\compose\circ
 \def\implies{\Rightarrow}
 \def\vect#1{\mathord{\mathbf{#1}}}
-\def\d{\mathord{\operator@font d}}
+\def\d{%
+  \ifmmode\mathord{\operator@font d}%
+  \else\expandafter\a\expandafter d\fi%
+}
 \def\jacobi#1#2{{{#1}\overwithdelims()#2}}
 %    \end{macrocode}
 %
@@ -653,6 +722,9 @@
 % \begin{macro}{\ran}
 % \begin{macro}{\supp}
 % \begin{macro}{\lcm}
+% \begin{macro}{\poly}
+% \begin{macro}{\negl}
+% \begin{macro}{\ord}
 %
 % And the new operator names.
 %
@@ -662,6 +734,9 @@
 \def\ran{\mathop{\operator@font ran}\nolimits}
 \def\supp{\mathop{\operator@font supp}\nolimits}
 \def\lcm{\mathop{\operator@font lcm}\nolimits}
+\def\poly{\mathop{\operator@font poly}\nolimits}
+\def\negl{\mathop{\operator@font negl}\nolimits}
+\def\ord{\mathop{\operator@font ord}\nolimits}
 %    \end{macrocode}
 %
 % \end{macro}
@@ -669,6 +744,143 @@
 % \end{macro}
 % \end{macro}
 % \end{macro}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+%
+% \subsection{Fractions}
+%
+% \begin{macro}{\@frac@parse}
+%
+% \syntax{"\\@frac@parse{"<stuff>"}{"<frac-params>"}"} -- run \<stuff>
+% passing it three arguments: an infix fraction-making command, the `outer'
+% style, and the `inner' style.
+%
+% This is rather tricky.  We clear a load of parameters, parse the parameter
+% list, and then build a token list containing the right stuff.  Without the
+% token list fiddling, we end up expanding things at the wrong times -- for
+% example, |\{| expands to something terribly unpleasant in a document
+% preamble.
+%
+% All of the nastiness is contained in a group.
+%
+%    \begin{macrocode}
+\def\@frac@parse#1#2{%
+  \begingroup%
+  \let\@wd\@empty\def\@ldel{.}\def\@rdel{.}%
+  \def\@op{over}\let\@dim\@empty\@tempswafalse%
+  \let\@is\@empty\let\@os\@empty%
+  \mkparse{mdwmath:frac}{#2}%
+  \toks\tw@{\endgroup#1}%
+  \toks@\expandafter{\csname @@\@op\@wd\endcsname}%
+  \if@tempswa%
+    \toks@\expandafter{\the\expandafter\toks@\@ldel}%
+    \toks@\expandafter{\the\expandafter\toks@\@rdel}%
+  \fi%
+  \expandafter\toks@\expandafter{\the\expandafter\toks@\@dim}%
+  \toks@\expandafter{\the\toks\expandafter\tw@\expandafter{\the\toks@}}
+  \toks@\expandafter{\the\expandafter\toks@\expandafter{\@os}}
+  \toks@\expandafter{\the\expandafter\toks@\expandafter{\@is}}
+  \the\toks@%
+}
+%    \end{macrocode}
+%
+% The keyword definitions are relatively straightforward now.  The error
+% handling for \textsf{style} and \textsf{innerstyle} could do with
+% improvement.
+%
+%    \begin{macrocode}
+\def\@frac@del#1#2{\def\@wd{withdelims}\@tempswatrue\def#1{#2}}
+\mkdef{mdwmath:frac}{leftdelim}{\@frac@del\@ldel{#1}}
+\mkdef{mdwmath:frac}{rightdelim}{\@frac@del\@rdel{#1}}
+\mkdef{mdwmath:frac}{nodelims}*{\let\@wd\@empty\@tempswafalse}
+\mkdef{mdwmath:frac}{line}{%
+  \def\@op{above}\setlength\dimen@{#1}\edef\@dim{\the\dimen@\space}%
+}
+\mkdef{mdwmath:frac}{line}*{\def\@op{over}\let\@dim\@empty}
+\mkdef{mdwmath:frac}{noline}*{\def\@op{atop}\let\@dim\@empty}
+\def\@frac@style#1#2{%
+  \ifx\q@delim#2\q@delim\let#1\@empty%
+  \else%
+    \expandafter\ifx\csname #2style\endcsname\relax%
+      \PackageError{mdwmath}{Bad maths style `#2'}\@ehc%
+    \else%
+      \edef#1{\csname#2style\endcsname}%
+    \fi%
+  \fi%
+}
+\mkdef{mdwmath:frac}{style}[]{\@frac@style\@os{#1}}
+\mkdef{mdwmath:frac}{innerstyle}[]{\@frac@style\@is{#1}}
+%    \end{macrocode}
+%
+% \end{macro}
+%
+% \begin{macro}{\fracdef}
+%
+% Here's where the rest of the pain is.  We do a preliminary parse of the
+% parameters and `compile' the result into the output macro.  If there's no
+% optional argument, then we don't need to do any really tedious formatting
+% at the point of use.
+%
+%    \begin{macrocode}
+\def\fracdef#1#2{\@frac@parse{\fracdef@i{#1}{#2}}{#2}}
+\def\fracdef@i#1#2#3#4#5{\def#1{\@frac@do{#2}{#3}{#4}{#5}}}
+\def\@frac@do#1#2#3#4{%
+  \@ifnextchar[{\@frac@complex{#1}}{\@frac@simple{#2}{#3}{#4}}%
+}
+\def\@frac@complex#1[#2]{\@frac@parse\@frac@simple{#1,#2}}
+\def\@frac@simple#1#2#3#4#5{{#2{{#3#4}#1{#3#5}}}}
+%    \end{macrocode}
+%
+% \end{macro}
+%
+% \begin{macro}{\frac@fix}
+% \begin{macro}{\@@over}
+% \begin{macro}{\@@atop}
+% \begin{macro}{\@@above}
+% \begin{macro}{\@@overwithdelims}
+% \begin{macro}{\@@atopwithdelims}
+% \begin{macro}{\@@abovewithdelims}
+%
+% Finally, we need to fix up |\@@over| and friends.  Maybe \package{amsmath}
+% has hidden the commands away somewhere unhelpful.  If not, we make the
+% requisite copies.
+%
+%    \begin{macrocode}
+\def\q@delim{\q@delim}
+\def\frac@fix#1{\expandafter\frac@fix@i\string#1\q@delim}
+\def\frac@fix@i#1#2\q@delim{\frac@fix@ii{#2}\frac@fix@ii{#2withdelims}}
+\def\frac@fix@ii#1{%
+  \expandafter\ifx\csname @@#1\endcsname\relax%
+    \expandafter\let\csname @@#1\expandafter\endcsname\csname#1\endcsname%
+  \fi%
+}
+\frac@fix\over \frac@fix\atop \frac@fix\above
+%    \end{macrocode}
+%
+% \end{macro}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+%
+% \begin{macro}{\frac}
+% \begin{macro}{\binom}
+% \begin{macro}{\jacobi}
+%
+% And finally, we define the fraction-making commands.
+%
+%    \begin{macrocode}
+\fracdef\frac{nodelims, line}
+\fracdef\binom{leftdelim = (, rightdelim = ), noline}
+\fracdef\jacobi{leftdelim = (, rightdelim = ), line}
+%    \end{macrocode}
+%
+% \end{macro}
+% \end{macro}
+% \end{macro}
 %
 % \subsection{Blackboard bold stuff}
 %
@@ -679,6 +891,7 @@
 % \begin{macro}{\N}
 % \begin{macro}{\F}
 % \begin{macro}{\powerset}
+% \begin{macro}{\gf}
 %
 % First of all, the signs.
 %
@@ -690,6 +903,8 @@
 \def\N{\mathbb{N}}
 \def\F{\mathbb{F}}
 \def\powerset{\mathbb{P}}
+\def\gf#1{\F_{#1}}
+%\def\gf#1{\mathrm{GF}({#1})}
 %    \end{macrocode}
 %
 % \end{macro}
@@ -699,6 +914,7 @@
 % \end{macro}
 % \end{macro}
 % \end{macro}
+% \end{macro}
 %
 % And now, define |\mathbb| if it's not there already.
 %
@@ -822,19 +1038,32 @@
 %   boundary. 
 % \end{itemize}
 %
+% The vertical mode case is simpler, but less universal.  It copes with
+% relatively simple cases only.
+%
 % A |\qed| commend ends the paragraph.
 %
 %    \begin{macrocode}
-\providecommand\qed{%
-  \leavevmode%
-  \unskip%
-  \penalty\@M\hfill%
-  \hbox{}\penalty\z@\quad%
-  \hbox{}\penalty\@M\hfill\qedsymbol\hskip-\parfillskip\par%
+\def\qed{%
+  \ifvmode%
+    \unskip%
+    \setbox\z@\hb@xt@\linewidth{\hfil\strut\qedsymbol}%
+    \prevdepth-\@m\p@%
+    \ifdim\prevdepth>\dp\strutbox%
+      \dimen@\prevdepth\advance\dimen@-\dp\strutbox%
+      \kern-\dimen@%
+    \fi%
+    \penalty\@M\vskip-\baselineskip\box\z@%
+  \else%
+    \unskip%
+    \penalty\@M\hfill%
+    \hbox{}\penalty200\quad%
+    \hbox{}\penalty\@M\hfill\qedsymbol\hskip-\parfillskip\par%
+  \fi%
 }
 \def\qedrule{{%
   \dimen@\ht\strutbox%
-  \advance\dimen@\dp\strutbox
+  \advance\dimen@\dp\strutbox%
   \dimen@ii1ex%
   \advance\dimen@-\dimen@ii%
   \divide\dimen@\tw@%
