%
%   HowToGAMIT 1992  ----  This file Copyright (C) 1991, 1992 amgreene
%



%
%   Do we already exist?  If so, just clear certain variables.
%

  \let\next\relax
  \expandafter\ifx\csname include\endcsname\relax\else
  \def\Chapter{}
  \let\next\endinput\fi
  \next


%
%   Include all the neat frobs from amgreene's TeXlib directory.
%

  \def\include{\input /afs/athena/user/a/m/amgreene/TeXlib/}
  \include output
  \include tables
  \include fonts
  \include environments

%
%   Make missing \inputs non-fatal
%

  \let\Input\input
  \def\input{\nonstopmode\Input}

%
%   Dimensions  -- We should probably increse the vsize...
%

  \hsize=5.125in
  \vsize=7.4375in
  \voffset=-.5in
  \baselineskip=10.4pt
  \parindent=0pt
  \parskip=.5\baselineskip
  \tolerance=4000
  \emergencystretch=5pt
  \hfuzz=22in
  \hbadness=10000

%
%   Additional setup for the dimensions of the output page:
%

  \newbox\Page
  \newbox\TmpBox
  \newdimen\TmpDim
  \newdimen\WideMargin\WideMargin=.4in
  \newdimen\Margin\Margin=.2in
  \newdimen\TabPlace\TabPlace=\vsize
  \newdimen\TabSize\TabSize=.4in
  \newdimen\TabHeight\TabHeight=.25in

  \def\empty{}

  \def\Warning{\immediate\write16{WARNING: UNAPPROVED!}}
  \def\approved{\def\Warning{}}

%
%   If we're running under TeX 3.0 or later, utilize the inputlineno
%   primitive to help us track the input file.
%


  \expandafter\ifx\csname inputlineno\endcsname
    \def\flag#1{\immediate\write16{Page \folio: #1}}
    \def\lineno{\relax}
  \else
    \def\flag#1{\immediate\write16{\the\inputlineno: #1}}
    \def\lineno{\setbox200=%
                \hbox{\everypar{}\hskip6in\tenrm\the\inputlineno}%
                \ht200=0pt \dp200=0pt \wd200=0pt \vadjust{\box200}}
  \fi
  \everypar{\lineno}

\def\flag#1{} % wimp!



%
%   Set up the index stuff.  \index{foo} puts an appropriate line in
%   the index file.  \Index{foo} will be suppressed if immediately
%   preceded by \noindex.  (That is, \noindex suppresses the next
%   \Index entry.)
%


  \newwrite\idxfile\openout\idxfile=\jobname.idx
  \newif\ifnoindex\noindexfalse
  \def\noindex{\noindextrue}
  \def\index#1{\write\idxfile{#1 \folio}\ignorespaces}
  \def\Index#1{\ifnoindex\noindexfalse\else\index{#1}\fi}



%
%   Set up the table of contents (toc) stuff.  \toc{foo} puts an
%   appropriate line in the toc file.  This is handled automagically
%   by \chapter and \entry.
%


  \newwrite\tocfile\openout\tocfile=\jobname.toc
  \def\toc#1{\write\tocfile{\noexpand\content{#1}{\folio}}}



%
%   Establish the structural markup for the book.  Basically, we have
%   chapters which contain entries which contain subentries.  Chapters
%   and entries appear in the toc; chapters, entries and subentries
%   appear in the index unless preceded by \noindex.  The entries are
%   saved as marks for use in the page headers.


  \def\chapter#1{\Warning\supereject \def\Chapter{#1}
  \bigbreak\centerline{\huge\Chapter}\nobreak\medskip\nobreak
  \index{#1}\write\tocfile{\noexpand\chaptab{#1}}%
  \toc{\noexpand\largest\noexpand\bf #1}%
  \write\tocfile{\noexpand\smallskip}
  \advance\TabPlace by -\TabHeight
  \let\leftmark\empty}

  \def\entry #1{\ifhmode\immediate\write
  16{Warning: Entry #1 does not start paragraph!}\par\fi
  \entbreak\let\entbreak\defaultbreak\noindent\kern-1em{\bf #1\entdot}\kern .7em
  \let\entdot.\def\ThisEntry{#1}
  \lineno\Index{#1}\toc{\qquad #1}\mark{#1}\ignorespaces}

  \def\tuck{\let\entbreak\medbreak}
  \def\Tuck{\let\entbreak\smallbreak}
  \let\entbreak\defaultbreak

  \def\nodot{\let\entdot\relax}
  \let\entdot.
  \def\subentry#1{{\bf #1\entdot}\kern .7em\Index{#1}\let\entdot.}


  \let\defaultbreak\smallbreak % was bigbreak in 1991
  \let\entbreak\defaultbreak

%
%   Crossreferences and the glossary are special cases:
%


  \def\xref#1#2{\noindex\entry{#1} See #2.\par}


  \def\glossentry#1{\ifhmode\immediate\write
  16{Warning: Entry #1 does not start paragraph!}\par\fi
  \smallbreak\noindent\mark{#1}{\bf #1} --
  \ignorespaces}



%
%   Table frobs (I'm still not happy with these:)
%

  \let\Table=\table
  \let\EndTable=\endtable

  \def\table{\par\smallbreak\begingroup\smallest\fixstrut
  \hfil\vbox\bgroup\Table}

  \def\endtable{\EndTable\egroup\hfil\endgroup\smallbreak}


%
%   Phone numbers.   These used to be managed entirely within TeX, but
%   I now think I'm going to do this in perl.  :-)  First argument is
%   the name of the store; second argument is the phone number.  The
%   category is determined from the current entry.
%
%

  \newwrite\rolodex
  \def\Phones{\immediate\openout\rolodex=rolodex.data
	\immediate\write\rolodex{@\catcode64=12\noexpand\Table{lrl}}}
  \def\phone#1#2{{\bf #1}\ignorespaces
  \immediate\write\rolodex{#1&#2&\ThisEntry}\noexpand}
  \def\PhoneNumbers{\immediate\closeout\rolodex
     %\Table{lrl}
     \catcode64=9
     \input rolodex.sorted\EndTable}

%
%   Activities.   This needs a total overhaul.  Still no clue on how
%   to do this correctly.
%

  \def\empty{}
  \def\al#1{\def\tmp{#1}\ifx\tmp\empty\else, #1\fi}

  \long\def\act#1#2#3#4#5#6\par{% name/office/phone/contact/phone/descr
  \medskip
  \vtop{\smaller\fixstrut\parskip=0pt
  \hangindent=1em \hangafter=1
  {\smaller\baselineskip=7pt{\normalsize\bf #1.}
%  \al{#2}\al{#3}\al{#4}\al{#5}.  
   #6\par}\vskip -2pt plus 1pt}}


%
%   Ads?  Ha!
%
%

   \def\ad#1#2{\vbox to #1{\vfill\centerline{#2}\vfill}}
   \newinsert\Ads

%
%
%  Endgame
%

%  \let\oldend=\end
%  \def\end{\closeout\tocfile \closeout\idxfile \oldend}

%  \def\byebye{\ifhmode\par\fi\vfill\supereject
%  %\ifdim\ht\Ads>0pt\unvbox\Ads\vfill\eject\fi
%  \end}


%
%   Output routine support
%

  \let\RealHrule\hrule
%
%   Special pages
%

  \newif\ifnormalpage
  \def\SpecialPage{\normalpagefalse}
  \normalpagetrue

  \def\TwoColumn{\let\iftwocolumn\iftrue}
  \let\iftwocolumn\iffalse
  \newbox\LeftCol

%
%   Crop marks
%

  \newdimen\CropLen\CropLen=.2in\relax
  \newdimen\CropWid\CropWid=.2pt\relax

  \def\Left{\vrule width -\CropLen height .5\CropWid depth .5\CropWid
  \vrule width \CropLen height .5\CropWid depth .5\CropWid}

  \def\Right{\vrule width \CropLen height .5\CropWid depth .5\CropWid
  \vrule width -\CropLen height .5\CropWid depth .5\CropWid}

  \def\Up{\vrule width -\CropWid height 0pt depth 0pt
  \vrule width \CropWid height \CropLen depth 0pt
  \vrule width 0pt height -\CropLen depth 0pt}

  \def\Down{\lower\CropLen\hbox{\vrule width \CropWid height 
  \CropLen depth 0pt}}


%
%   Are ads allowed on the next page?
%

  \def\noads{\gdef\AdsAllowed{tf}}
  \def\yesads{\gdef\AdsAllowed{tt}}

  \yesads

%
%   The output routine is defined here.
%
\def\plainoutput{%
%
%  First of all, assemble the output box.  For now, that's just
%  \box255.  Later, we'll put together ads, quotes, and pictures.
%
%  Now, put that inside crop marks, headers, etc.
%
%
\def\Continue{tt}
%
  \iftwocolumn
     \ifdim\ht\LeftCol>0pt
	\setbox\Page=\hbox{\box\LeftCol\box255}
	\wd\Page=\hsize
     \else
	\def\Continue{tf}
	\global\setbox\LeftCol=\vbox{\box255}
	\wd\LeftCol=.5\hsize
     \fi
  \else
     \setbox\Page=\box255
  \fi
%
%
  \if\Continue
%
     \setbox255=\vbox{}
     \yesads
%
%  Prepare the actual page:
% 
    \advance\hsize by \WideMargin
    \advance\vsize by 1in

    \shipout\vbox{
%
%   UPPER CROP MARKS
%   ----- ---- -----
%
    \hbox{\hskip -\Margin\Left\Up\hskip\hsize
    \hskip2\Margin\hbox{\Right\Up}}
%
%   TOP MARGIN
%   --- ------
%
    \vskip\Margin
%
%   HEADER
%   ------
%
    \ifnormalpage

    \ifx\leftmark\empty\let\botmark\empty\fi

    \ifodd\count0
%
        \line{\hfil\sl\normalsize\vbox{\hbox{\leftmark}
                                       \hbox{\botmark }}
              \hugest\rm\ \folio}
%
    \else
%
        \line{\hugest\rm\folio\ \vbox{\hbox{\rm\largest\Chapter}}\hfil}
        \xdef\leftmark{\firstmark}
%
    \fi

%
%
%   HEADER RULE
%   ------ ----
%
    \vskip 2pt
    \RealHrule width \hsize
    \vskip .2in

    \else \line{\hugest\fixstrut\strut} \vskip 16.454pt

    \fi
%
%
%   THE PAGE (assembled above)
%   --- ----
%
    \ifodd\count0  \line{\hskip .1in\box\Page\hfill}
    \else          \line{\hfill\box\Page\hskip .1in}
    \fi            \vfil
%
%
   \iffalse
%   SIDE TABS
%   ---- ----
%
    \vskip -\TabPlace\hbox{
%
    \ifodd\count0  \vrule width    \hsize   height 0pt depth 0pt
                   \vrule width  .6\TabSize height 0pt depth 0pt
    \else          \vrule width   -\Margin  height 0pt depth 0pt
    \fi
%
                   \vrule width -.6\TabSize height 0pt depth 0pt
%
%   (that was placement.  Now for the actual tab.)
%                   
          \vrule width    \TabSize height\TabHeight depth 0pt}
%
%   (and now to restore our position at the bottom of the page)
%
    \vskip \TabPlace
   \fi
%
%
%   FOOTERS
%   -------
%
    \line{}

    \ifnormalpage

    \ifodd\count0  \line{\hfil\sl HowToGAMIT XXIII}
    \else          \line{\sl HowToGAMIT XXIII\hfil}
    \fi

    \else \line{} \fi
%
%
%   BOTTOM MARGIN AND CROP MARKS
%   ------ ------ --- ---- -----
%
    \vskip\Margin
    \hbox{\hskip -\Margin\Left\Down\hskip\hsize
          \hskip 2\Margin\hbox{\Right\Down}}}
%
%
%   HANDLE PAGE NUMBER INCREMENT
%   ------ ---- ------ ---------
%
    \global\advance\pageno by 1
    \global\normalpagetrue
%
%
%   END THE ``IF CONTINUE''
%   --- ---  --- --------- 
%
    \fi
%
%
%   AND CLEAR THE BOX 255
%   --- ----- --- --- ---
%
    \unvbox255}
%
%
%
\endinput
\def\0{{\oldstyle 0}}\catcode`0=\active\let0\0
\def\1{{\oldstyle 1}}\catcode`1=\active\let1\1
\def\2{{\oldstyle 2}}\catcode`2=\active\let2\2
\def\3{{\oldstyle 3}}\catcode`3=\active\let3\3
\def\4{{\oldstyle 4}}\catcode`4=\active\let4\4
\def\5{{\oldstyle 5}}\catcode`5=\active\let5\5
\def\6{{\oldstyle 6}}\catcode`6=\active\let6\6
\def\7{{\oldstyle 7}}\catcode`7=\active\let7\7
\def\8{{\oldstyle 8}}\catcode`8=\active\let8\8
\def\9{{\oldstyle 9}}\catcode`9=\active\let9\9
%----------------- WHAT THE HELL DOES THIS DO?
%
% Now set up for multiple files:
\endinput
\let\END\end
\let\end\endinput
\newwrite\pager
\def\EndAll #1{
\openout\pager=htg#1.pg
\write\pager{\the\count0}
\write\pager{\the\TabPlace}
\closeout\pager\let\end\END \bye}
\def\file #1
{\write\prog{Opening #1}\input #1 }
\newread\pagerd
\openin\pagerd=\jobname.pg
\read\pagerd to \tmp
\count0=\tmp
\read\pagerd to \tmp
\TabPlace=\tmp
\closein\pagerd
\advance\count0 by 1
%
\def\Chapter{}
