+% \section{Implementation of \package{mtcolour}}
+%
+%
+% This is in a separate package to avoid dragging in the \package{color}
+% package if it's unwanted.
+%
+% I prefer English spellings. Here's a trivial redirection for Americans.
+%
+% \begin{macrocode}
+%<*color>
+\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{mtcolour}}
+\ProcessOptions
+\RequirePackage{mtcolour}
+%</color>
+% \end{macrocode}
+%
+% And now we can start the thing properly.
+%
+% \begin{macrocode}
+%<*colour>
+\RequirePackage{color}
+% \end{macrocode}
+%
+%
+% \subsection{Cell background colours}
+%
+% First, some simple preliminaries. The |\iftab@colour| switch is set if the
+% current cell is meant to have a colour.
+%
+% \begin{macrocode}
+\newif\iftab@colour
+\tab@colourfalse
+% \end{macrocode}
+%
+% We shall store the cell colour information in |\tab@cellcolour|, and the
+% row colour information as |\tab@rowcolour|. Because of the structure of
+% tables, we need to make global assignments; so we must copy the current
+% value away at the start of a table and put the value back at the end. In
+% order to transfer the overhang information reliably, we use a separate
+% control sequence |\tab@colouroverhangs| for that -- otherwise |\color| can
+% corrupt it.
+%
+% \begin{macrocode}
+\tab@addhookbefore\tab@beginhook{%
+ \let\tab@saverowcolour\tab@rowcolour%
+ \let\tab@savecolouroverhangs\tab@colouroverhangs%
+ \let\tab@savecellcolour\tab@cellcolour%
+}
+\tab@addhookafter\tab@endhook{%
+ \global\let\tab@rowcolour\tab@saverowcolour%
+ \global\let\tab@colouroverhangs\tab@savecolouroverhangs%
+ \global\let\tab@cellcolour\tab@savecellcolour%
+}
+% \end{macrocode}
+%
+% Initially, there are no colours.
+%
+% \begin{macrocode}
+\let\tab@rowcolour\@empty%
+\let\tab@cellcolour\@empty%
+\let\tab@colouroverhangs\@empty%
+% \end{macrocode}
+%
+% \begin{macro}{\@snarfcolour}
+%
+% Reading a colour specification is something we'll need to do a few times,
+% so an abstraction is useful. Its single argument is a continuation to
+% which we pass a colour-spec acceptable to the |\color| command. (This is
+% the same code as found in the \package{sverb} package. Remember to keep
+% them in step.)
+%
+% \begin{macrocode}
+\def\@snarfcolour#1{%
+ \@ifnextchar[{\@snarfcolour@i{#1}}{\@snarfcolour@ii{#1}{}}%
+}
+\def\@snarfcolour@i#1[#2]{\@snarfcolour@ii{#1}{[#2]}}
+\def\@snarfcolour@ii#1#2#3{#1{#2{#3}}}
+% \end{macrocode}
+%
+% \end{macro}
+%
+% \begin{macro}{\cellcolour}
+%
+% Setting a cell colour is a matter of stashing the right declarations in
+% |\tab@cellcolour| and |\tab@colouroverhangs|. Note that the overhangs end
+% up in |\dimen0| and |\dimen2|.
+%
+% \begin{macrocode}
+\def\cellcolour{%
+ \@ifstar{\tab@ccol@i{\let\tab@rowcolour\@empty}}{\tab@ccol@i{}}%
+}
+\def\tab@ccol@i#1{\@snarfcolour{\tab@ccol@ii{#1}}}
+\def\tab@ccol@ii#1#2{\@testopt{\tab@ccol@iii{#2#1}}\z@}
+\def\tab@ccol@iii#1[#2]{\@testopt{\tab@ccol@iv{#1}{#2}}{#2}}
+\def\tab@ccol@iv#1#2[#3]{%
+ \gdef\tab@cellcolour{\color#1\tab@colourtrue}%
+ \gdef\tab@colouroverhangs{%
+ \setlength\dimen@{#2}%
+ \setlength{\dimen\tw@}{#3}%
+ }%
+}
+% \end{macrocode}
+%
+% \end{macro}
+%
+% \begin{macro}{\rowcolour}
+%
+% Setting the global row colour is simpler, because we don't mess with
+% overhangs.
+%
+% \begin{macrocode}
+\def\rowcolour{\@snarfcolour\tab@setrowcolour}
+\let\rowcolor\rowcolour
+\def\tab@setrowcolour#1{%
+ \gdef\tab@rowcolour{\color#1\tab@colourtrue}%
+}
+% \end{macrocode}
+%
+% \end{macro}
+%
+% \begin{macro}{\rowcolouroff}
+%
+% And turning the global colouring off is easy.
+%
+% \begin{macrocode}
+\def\rowcolouroff{\global\let\tab@rowcolour\@empty}
+\let\rowcoloroff\rowcolouroff
+% \end{macrocode}
+%
+% \end{macro}
+%
+% \begin{macro}{\tab@colourleft}
+%
+% Now we start on the table-cell hooks. The left hook starts a box which
+% will capture the cell's text and natural width. We add the hook to the
+% rule list as well, so that we can colour the bits in |\vgap|s correctly.
+%
+% \begin{macrocode}
+\tab@addhookbefore\tab@lefttexthook\tab@colourleft
+\tab@addhookbefore\tab@leftruletexthook\tab@colourleft
+\def\tab@colourleft{%
+ \global\let\tab@cellcolour\@empty%
+ \global\let\tab@colouroverhangs\@empty%
+ \setbox\z@\hbox\bgroup\color@begingroup%
+}
+% \end{macrocode}
+%
+% \end{macro}
+%
+% \begin{macro}{\tab@colourright}
+%
+% The right hook will insert an appropriate rule behind the cell and
+% retypeset the cell contents over the top. Note that the stretch in a table
+% cell is exactly 1\,fil. Because we add (leaders) and subtract (negative
+% |\hskip|) 1\,fil, we retain this stretch exactly. Don't bother unless
+% there's actually some colouring.
+%
+% \begin{macrocode}
+\tab@addhookafter\tab@righttexthook\tab@colourright
+\tab@addhookafter\tab@rightruletexthook\tab@colourright
+\def\tab@colourright{%
+ \color@endgroup\egroup%
+ \color@begingroup%
+ \global\tab@colourfalse%
+ \tab@cellcolour\tab@rowcolour%
+ \dimen@\z@\dimen\tw@\z@\tab@colouroverhangs%
+ \iftab@colour%
+ \skip@\wd\z@\advance\skip@\z@\@plus1fil%
+ \skip\tw@\skip@%
+ \kern-\dimen@%
+ \advance\skip\tw@\dimen@%
+ \advance\skip\tw@\dimen\tw@%
+ \leaders\vrule\hskip\skip\tw@%
+ \kern-\dimen\tw@%
+ \hskip-\skip@%
+ \fi%
+ \color@endgroup%
+ \unhbox\z@%
+}
+% \end{macrocode}
+%
+% \end{macro}
+%
+%
+% \subsection{Coloured rules}
+%
+% We hook ourselves onto the rule-parameters edifice. This is rather
+% straightforward.
+%
+% \begin{macrocode}
+\tab@addhookafter\tab@rp@inithook{%
+ \let\tab@rulecolour\@empty%
+ \let\tab@rulecolourmodel\@empty%
+}
+\mkdef{mdwtab:rule}{colour}{\tab@setrulecolour{#1}}
+\mkdef{mdwtab:rule}{colourmodel}{\tab@setrulecolourmodel{#1}}
+\mkdef{mdwtab:rule}{color}{\tab@setrulecolour{#1}}
+\mkdef{mdwtab:rule}{colormodel}{\tab@setrulecolourmodel{#1}}
+\mkdef{mdwtab:rule}{nocolour}*{\let\tab@rulecolour\@empty}
+\mkdef{mdwtab:rule}{nocolor}*{\let\tab@rulecolour\@empty}
+\mkdef{mdwtab:rule}{nocolourmodel}*{\let\tab@rulecolourmodel\@empty}
+\mkdef{mdwtab:rule}{nocolormodel}*{\let\tab@rulecolourmodel\@empty}
+\def\tab@setrulecolour#1{%
+ \def\tab@rulecolour{\color\tab@rulecolourmodel{#1}}%
+}
+\def\tab@setrulecolourmodel#1{\def\tab@rulecolourmodel{[#1]}}
+\tab@addhookafter\tab@rp@sethook{\tab@rulecolour}
+% \end{macrocode}
+%
+%
+% \subsection{Other stuff}
+%
+% \begin{macro}{\ifmod}
+%
+% \syntax{"\\ifmod{"$x$"}{"$m$"}{"y"}{"<yes>"}{"<no>"}"} -- if $x \bmod m =
+% y$ then do \<yes>; otherwise do \<no>.
+%
+% \begin{macrocode}
+\def\ifmod#1#2#3{%
+ \begingroup%
+ \@tempcnta#1%
+ \@tempcntb#2%
+ \count@\@tempcnta%
+ \divide\count@\@tempcntb%
+ \multiply\count@\@tempcntb%
+ \advance\@tempcnta-\count@%
+ \count@#3\relax%
+ \ifnum\@tempcnta=\count@\endgroup\expandafter\@firstoftwo%
+ \else\endgroup\expandafter\@secondoftwo\fi%
+}
+% \end{macrocode}
+%
+% \end{macro}
+%
+% Done.
+%
+% \begin{macrocode}
+%</colour>
+% \end{macrocode}
+%
+%^^A-------------------------------------------------------------------------