%-*-tex-*-
% Copyright Michael J. Ferguson, INRS-Telecommunications (1985,1986)
% All rights reserved.
\catcode`\@=11
 
%======= Additional active Characters ===========
% ALL ACTIVE CHARACTERS MUST BE DECLARED AND DEFINED AT THE VERY
% BEGINNING OR THERE IS A DANGER OF INCORRECT TOKENIZATION
% ======== THESE ARE USED IN THE TABLE MACROS =========
\message{<< Additional Active Characters >>}
\catcode`\|= \active
\def|{\ifmmode \vert\else \char`\|\fi} % effectively undoes activeness
\def\q@m{\string"}
\catcode`\"=\active \def"{\char`\"}
% update \dospecials
\toksdef\toks@@=2
\toks@{\do \"}%
\toks@@ =\expandafter{\dospecials}\xdef\dospecials{\the\toks@@\the\toks@}
\toks@{\do \|}%
\toks@@ =\expandafter{\dospecials}\xdef\dospecials{\the\toks@@\the\toks@}
%=== these are redefined inside the table macros =====
 
 
%============= Table making macros ============
% the basic format is
% \begintable
% <special definitions for this table>
% \begintableformat
%           format as in halign except it needs ## instead of #
%           in tableformat, " means strut column
%           spacing controlled by \left, \center, \right
%           can use \math or \displaymath in conjuction with spacing
% \endtableformat
% \br{<struts>} .... | .... | .... " ..... | .... \er{<stuff>}  (rows)
% \-                                                (horizontal rules)
% ....etc
% \endtable
%
% each row has format
% \br{<..>}  <item> | <item} " .... | <item> \er{<..>}
%                   \br, \er mean beginning of row, end of row
%                   in table | means rule, " means no rule in strut column
%                   \: is standard strut, | is standard vrule
%
% \tablespread {to <dimen>}  width of table
% \tr=width of rules (default .4pt)
% \midtabglue sets glue in table (default 0pt plus 1fill)
% also can set explicit hrules and vrules
% tokens (e.g. \tablespread, \tr, \midtabglue, etc.) can be set in \everytable
 
% these macros require that | and " be active during the entire
% document to work correctly. ... although some definitions specifically
% set them active
\newcount\a@lignstate  \a@lignstate=0  %0 -- between lines after \cr
                                       %1 -- beginning of line in \bl
                                       %2 -- midline
                                       %3 -- endline in \el
\def\hssf{\hskip 0pt plus 1fill minus 1fill}
\def\n@ewaligndefs{\def\center##1{\hssf ##1\hssf\null}
          \def\left##1{##1\hssf\null}
          \def\right##1{\hssf ##1\null}}
\newdimen\trulesize
\let\tr=\trulesize
\trulesize = .4pt
\def\zerocenteredbox#1{\ifmmode \ifinner \setbox2 =\hbox{$#1$}\else
                           \setbox2 =\hbox{$\displaystyle#1$}\fi
                     \else \setbox2 =\hbox{#1}\fi
      \setbox0=\hbox{\lower.5ex\hbox{$\vcenter{\box2}$}}\ht0=0pt\dp0 =0pt\box0}
\let\zb=\zerocenteredbox
 
%this macro creates a strut with the that is higher by #2 and deeper than #3
% than the natural size of #1 ... the sizes may be negative
\def\modifystrut#1#2#3{\setbox4=\hbox{#1}\dimen0=\ht4
             \advance \dimen0 by #2 \dimen2 = \dp4
             \advance  \dimen2 by #3
            \vrule width 0pt height \dimen0 depth \dimen2}
\let\mst=\modifystrut
%general math form
\def\math#1{\relax $\relax#1\relax$}
\def\displaymath #1{\relax$\displaystyle #1\relax$}
 
 
%
\newskip\tcs
\newtoks\tablespread
\newskip\midtabglue \midtabglue = 0pt plus 1fill
\newtoks\everytable  \everytable = {\relax}
 
{\catcode`\|=\active   \catcode`\" = \active
  \gdef\begintable{\vbox\bgroup \tcs=.5em % uses font in force when entering
                  \catcode`\|=\active
                  \catcode`\"=\active
                   \def\:{\relax \vrule height 2.5ex depth .9ex width 0pt}
          \def\-{\ifcase\a@lignstate \fulltablerule{\tr}
                  \else
                  \thrule{\tr}\fi}
          \let\t@xx =\relax % for premature expansions
          \everycr={\noalign{\global\a@lignstate=0}}
          \def\fulltablerule##1{\noalign{\hrule height
                   ##1}}
          \def\thrule##1{\omit\leaders\hrule height ##1\hfill}
          \def\center{\hskip\tcs\hss ########\hss\hskip\tcs}
          \def\left{\hskip\tcs ########\hss\hskip\tcs}
          \def\right{\hskip\tcs\hss ########\hskip\tcs}
          \def\sprule{\tvrule{2.5\tr}}
          \def|{\ifcase\a@lignstate \def\t@xx{\tvrule{\tr}}\or
                            \def\t@xx{\tvrule{\tr}}\or
                             \def\t@xx{\unskip&\tvrule{\tr}&}\else
                             \def\t@xx{\tvrule{\tr}}\fi\t@xx}
          \def\|{\ifcase\a@lignstate \def\t@xx{\sprule}\or
                            \def\t@xx{\sprule}\or
                             \def\t@xx{\unskip&\sprule&}\else
                              \def\t@xx{\sprule}\fi\t@xx}
          \def"{&########&} % for table format
          \def\br##1{\global\a@lignstate=1 ##1\unskip\global\a@lignstate=2&}
          \def\er##1{\global\a@lignstate=3\unskip&##1\unskip
                      \global\a@lignstate=0\cr}
          \def\tvrule##1{\hss\vrule width ##1\hss}
          \def~{\penalty\@M \hphantom{0}}
%    % changes ~ to be phantom of width .5em
          \tablespread = {}
          \the\everytable
               }
  \gdef\begintableformat #1\endtableformat{\offinterlineskip \tabskip = 0pt
       \edef\t@blform{####\tabskip =\midtabglue &#1&####\tabskip=0pt\cr}
%      % adds rules front and back
                                  \n@ewaligndefs
                                 \def"{\ifcase\a@lignstate \def\t@xx{\relax}\or
                                          \def\t@xx{\relax}\or
                                        \def\t@xx{\unskip&&}\else
                                        \def\t@xx{\relax}\fi\t@xx}
                                \edef\h@align{\halign \the\tablespread}
                                  \h@align\bgroup\span\t@blform}
      }% " and | are always active in INRSTEX
 
\def\use#1{\omit\mscount=#1 \advance\mscount by -1\multiply\mscount by2
                \loop\ifnum\mscount>1 \sp@n\repeat
                \ifnum\mscount>0 \span \else \relax \fi}
\let\sp=\span
\let\om = \omit
\def\sa#1{\setbox0=\hbox{#1}\hbox to \wd0{}}
\def\endtable{\crcr\egroup\egroup}
 
\catcode`\@=12
 
******************************************
\def\uncatcodespecials{\catcode`@=12\def\do##1{\catcode`##1=12}\dospecials}
\def\setupverbatim{\tt\obeylines\uncatcodespecials\obeyspaces}
{\obeyspaces\global\let =\ }
\def\beginshowoff{\par\begingroup\setupverbatim\doverbatim}
{\catcode`\!=0 \catcode`\\=12
!obeylines!gdef!doverbatim^^M#1\endshowoff{#1!endgroup!medbreak!filbreak%
!smallskip}}
 
\input table
 
\centerline{\bf Table}
\bigskip
The following illustrate the procedure for using the {\it INRStable\/}
macros.  See {\it The TUGboat}, vol.~7, no.~2, June, 1986, pp.~106--109.
The characters {\tt "} and {\tt |} are active throughout the document,
but this does not affect their use, except that following spaces are
ignored.  From the macro:
\vskip.5truein
 
\beginshowoff
%============= Table making macros ============
% the basic format is
% \begintable
% <special definitions for this table>
% \begintableformat
%           format as in halign except it needs ## instead of #
%           in tableformat, " means strut column
%           spacing controlled by \left, \center, \right
%           can use \math or \displaymath in conjuction with spacing
% \endtableformat
% \br{<struts>} .... | .... | .... " ..... | .... \er{<stuff>}  (rows)
% \-                                                (horizontal rules)
% ....etc
% \endtable
%
% each row has format
% \br{<..>}  <item> | <item} " .... | <item> \er{<..>}
%                   \br, \er mean beginning of row, end of row
%                   in table | means rule, " means no rule in strut column
%                   \: is standard strut, | is standard vrule
%
% \tablespread {to <dimen>}  width of table
% \tr=width of rules (default .4pt)
% \midtabglue sets glue in table (default 0pt plus 1fill)
% also can set explicit hrules and vrules
% tokens (e.g. \tablespread, \tr, \midtabglue, etc.) can be set in \everytable
\endshowoff
 
{}\vskip.5truein
\centerline{           % see The TeXbook, p. 247
\begintable
\begintableformat
\center " \center " \center     % 3 centered columns, separated by struts
\endtableformat
\br{} \sa{Dividend} "\sa{Dividend} "\sa{Dividend} \er{}  %optional sample row
                           %\sa means sample column (for width)
\-    % horizontal rule
\br{\:|} \use{3}   AT\&T Common Stock    \er{|} %\use{3} means use 3 columns
\-
\br{\:|}     Year |  Price  |   Dividend       \er{|}
\-
\br{\:|}     1971 | 41--54  |    \$2.60        \er{|}
\-
\br{\:|}     ~~~2 | 41--54  |     ~2.70        \er{|}
\-
\br{\:|}     ~~~3 | 46--55  |     ~2.87        \er{|}
\-
\br{\:|}     ~~~4 | 40--53  |     ~3.24        \er{|}
\-
\br{\:|}     ~~~5 | 45--52  |     ~3.40        \er{|}
\-
\br{\:|}     ~~~6 | 41--54  |     ~~.95\rlap*  \er{|}
\-
\br{\:}\use{3} \left{* (first quarter only)}
\endtable }
 
\vskip.5truein
\beginshowoff
\input table
\centerline{           % see The TeXbook, p. 247
\begintable
\begintableformat
\center " \center " \center     % 3 centered columns, separated by struts
\endtableformat
\br{} \sa{Dividend} "\sa{Dividend} "\sa{Dividend} \er{}  %optional sample row
                           %\sa means sample column (for width)
\-    % horizontal rule
\br{\:|} \use{3}   AT\&T Common Stock    \er{|} %\use{3} means use 3 columns
\-
\br{\:|}     Year |  Price  |   Dividend       \er{|}
\-
\br{\:|}     1971 | 41--54  |    \$2.60        \er{|}
\-
\br{\:|}     ~~~2 | 41--54  |     ~2.70        \er{|}
\-
\br{\:|}     ~~~3 | 46--55  |     ~2.87        \er{|}
\-
\br{\:|}     ~~~4 | 40--53  |     ~3.24        \er{|}
\-
\br{\:|}     ~~~5 | 45--52  |     ~3.40        \er{|}
\-
\br{\:|}     ~~~6 | 41--54  |     ~~.95\rlap*  \er{|}
\-
\br{\:}\use{3} \left{* (first quarter only)}
\endtable }
\endshowoff
\vfill\eject
 
{}\vskip.5truein
\centerline{
\begintable
\def\sprule{\tvrule{5\tr}} % special rule defines \|
                           % \tr = .4pt
\begintableformat&\center\endtableformat   % repeating columns
\-
\br{\:|} \use{6}            XYZABC      \er{|\mst{\:}{3pt}{3pt}\rlap{\it ** i}}
   % \mst{<token>}{<dimen1>}{<dimen2>} means modified strut derived from token
   %    with dimen1 extra height and dimen2 extra depth
\-
\br{\:|} \use{3}   XYZ           | \use{3}     ABC    \er{|\mst{\:}{2pt}{2pt}}
\fulltablerule{5\tr}    %explicit horizontal rule
                        %can also use \use\tvrule{<dimen>} for partial rules
\br{\:|}      X   |  Y    |   Z \|   A  |   B  |  C   \er{|}
\-
\br{\:|} 372.466  | 493.7 "  45 \| 124  |  489 | 280  \er{|}
\-
\br{\:|} 372.40~  | 493.7 |  45 \| 124  |  489 | 280  \er{|}
\br{|}            "       "     \| \use{3}\-          \er{|}
\br{\:|} 372.~~~  | 493.7 |  45 \| 124  |  489 | 280  \er{|\rlap{ \it ** ii}}
\-
\br{\:|} \use{2}          | 832 \| abc  |  774 |$\int$\er{|\mst{$\int$}{0pt}
{3pt}}
\br{|} \use{2}\zb{XY/A}   |\use{4}\-                  \er{|}
                % \zb{<item>} means zero centered box (with no height or depth)
\br{\:|} \use{2}          |qrr  \| aaa  |  799 |      \er{|\rlap{ \it ** iii}}
\-
\endtable}
 
\vskip.5truein
\beginshowoff
\centerline{
\begintable
\def\sprule{\tvrule{5\tr}} % special rule defines \|
                           % \tr = .4pt
\begintableformat&\center\endtableformat   % repeating columns
\-
\br{\:|} \use{6}            XYZABC      \er{|\mst{\:}{3pt}{3pt}\rlap{\it ** i}}
   % \mst{<token>}{<dimen1>}{<dimen2>} means modified strut derived from token
   %    with dimen1 extra height and dimen2 extra depth
\-
\br{\:|} \use{3}   XYZ           | \use{3}     ABC    \er{|\mst{\:}{2pt}{2pt}}
\fulltablerule{5\tr}    %explicit horizontal rule
                        %can also use \use\tvrule{<dimen>} for partial rules
\br{\:|}      X   |  Y    |   Z \|   A  |   B  |  C   \er{|}
\-
\br{\:|} 372.466  | 493.7 "  45 \| 124  |  489 | 280  \er{|}
\-
\br{\:|} 372.40~  | 493.7 |  45 \| 124  |  489 | 280  \er{|}
\br{|}            "       "     \| \use{3}\-          \er{|}
\br{\:|} 372.~~~  | 493.7 |  45 \| 124  |  489 | 280  \er{|\rlap{ \it ** ii}}
\-
\br{\:|} \use{2}          | 832 \| abc  |  774 |$\int$\er{|\mst{$\int$}{0pt}
{3pt}}
\br{|} \use{2}\zb{XY/A}   |\use{4}\-                  \er{|}
                % \zb{<item>} means zero centered box (with no height or depth)
\br{\:|} \use{2}          |qrr  \| aaa  |  799 |      \er{|\rlap{ \it ** iii}}
\-
\endtable}
\endshowoff
\bye
