%                     FEYNMAN(34).TEX 
%  CALLING ROUTINE FOR DRAWING FEYNMAN DIAGRAMS IN LATEX.
%  DOCUMENTATION IN "FEYNMAN - A LaTeX Routine for Generating Feynman Diagrams"
%  Cavendish-HEP 88/11  (Cavendish Labs, Cambridge, UK).
%  See also: Levine, M.J.S., A LaTeX Graphics Routine for Drawing Feynman
%  Diagrams, Cavendish - HEP 89/4.
%  USES THE FOLLOWING TEX FILES:
%   GLUONSETUP(31), PHOTONSETUP(28), FERMIONSETUP(7), SCALARSETUP(9)
%   VERTEX(25), GLUONLINKS, LOOPS(1) 
%
%   THIS PROGRAM PACKAGE NOT TO BE ALTERED WITHOUT THE EXPRESS WRITTEN
%   PERMISSION OF THE AUTHOR.
%
%**************************************************************************
%
%                           SAMPLE USAGE
%  
%  \documentstyle[12pt]{article}
%  \begin{document}
%  \input feynman
%  \textheight 800pt \textwidth 450pt
%  \begin{picture}(10000,18000)
%  \drawline\gluon[\S\REG](0,16000)[8]
%  \drawline\fermion[\SW\REG](\gluonbackx,\gluonbacky)[2000]
%  \drawline\fermion[\SE\REG](\gluonbackx,\gluonbacky)[2000]
%  \end{picture}
%  \end{document}
%
%**************************************************************************
%
\message{FEYNMAN:  For generating Feynman Diagrams in LaTex}
\message{Mark 1.0 Last Altered by MJSL 2/89}
\textheight 650pt \textwidth 400pt  % Page size set.
\setlength{\unitlength}{0.01pt}
\gdef\Feynmanlength{\setlength{\unitlength}{0.01pt}}  % Say \Feynmanlength
\gdef\unlock{\catcode`\@=11}
%  Allows use of "@" in macro names, like PLAIN.TEX does.
\gdef\lock{\catcode`\@=12}%  Change @'s back to their normal category code.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ESTABLISH SOME COUNTERS TO INDICATE WHICH TYPE OF PARTICLE LINE IS
% BEING DRAWN.
%
% EACH TYPE OF  PARTICLE LINE HAS A CODE OF 3 1-DIGIT NUMBERS.
% THE FIRST DIGIT INDICATES THE BASIC PARTICLE TYPE (GLUON, PHOTON ETC.)
% THE SECOND INDICATES THE DIRECTION (HORIZONTAL, ANGLED, VERTICLE)
% THE THIRD INDICATES SUB-CASES (DIFFERENT SIZES ETC.)
%
%                             THE CODE
%                             ========
%
%  FIRST DIGIT:  \LINETYPE or \LTYPE
%
%    1   FERMION              (SOLID LINE)
%    2   SCALAR               (DASHED LINE)
%    3   PHOTON/VECTOR BOSON  (UNDULATING LINE) 
%    4   GLUON                (CURLY, LOOPING LINE)
%    5   ESPECIAL             (USER-DEFINED)
%
%  SECOND DIGIT:  \LINEDIRECTION or \LDIR
%
%    Eight directions are possible...the eight compass points.
%    North is at the top of the page
%
%    0   \N   North        (Vertical;  bottom to top)
%    1   \NE  North-East   (45 degrees; to the upper right)
%    2   \E   East         (Horizontal;  left to right)
%    3   \SE  South-East   (45 degrees; to the lower right)
%    4   \N   South        (Vertical;  top to bottom)
%    5   \SW  South-West   (45 degrees; to the lower left)
%    6   \W   West         (Horizontal;  right to left)
%    7   \NE  North-West   (45 degrees; to the upper left)
%
%  THIRD DIGIT:  \LINECONFIGURATION or \LCONFIG
%
%    0   STANDARD CONFIGURATION
%    1   FLIPPED (INVERTED; UPSIDE-DOWN) CONFIGURATION (PHOTONS & GLUONS)
%    2   CURLY
%    3   FLIPPEDCURLY
%    4   FLAT (PHOTONS AND GLUONS - CERTAIN DIRECTIONS ONLY)
%    5   FLIPPEDFLAT
%    6   CENTRAL (GLUONS ONLY);  LONGPHOTON (PHOTONS ONLY)
%    7   FLIPPEDCENTRAL; FLIPPEDLONG
%    8   SQUASHED (GLUONS ONLY - ONLY `EASTBOUND')
%    9   FLIPPEDSQUASHED
%
%.............................................................
%
\global\newcount\LINETYPE                     
\global\newcount\LINEDIRECTION
\global\newcount\LINECONFIGURATION
\newcommand{\LTYPE}{\LINETYPE}
\newcommand{\LDIR}{\LINEDIRECTION}
\newcommand{\LCONFIG}{\LINECONFIGURATION}
%DEFAULTS:  Horizontal fermion.   
\global\LINETYPE=1  \global\LINEDIRECTION=0  \global\LINECONFIGURATION=0
%  The parametric code names.  Don't change these.
\global\newcount\fermion    \fermion=1
\global\newcount\scalar     \scalar=2
\global\newcount\photon     \photon=3
\global\newcount\gluon      \gluon=4
\global\newcount\especial   \especial=5
\gdef\N{0}  \gdef\NE{1}  \gdef\E{2}   \gdef\SE{3}
\gdef\S{4}  \gdef\SW{5}  \gdef\W{6}   \gdef\NW{7}
\global\newcount\REG            \global\REG=0
\global\newcount\FLIPPED        \global\FLIPPED=1
\global\newcount\CURLY          \global\CURLY=2
\global\newcount\FLIPPEDCURLY   \global\FLIPPEDCURLY=3
\global\newcount\FLAT           \global\FLAT=4
\global\newcount\FLIPPEDFLAT    \global\FLIPPEDFLAT=5
\global\newcount\CENTRAL        \global\CENTRAL=6
\global\newcount\FLIPPEDCENTRAL \global\FLIPPEDCENTRAL=7
\gdef\LONGPHOTON{6}             \gdef\FLIPPEDLONG{7}
\global\newcount\SQUASHEDGLUON  \global\SQUASHEDGLUON=8
\gdef\SQUASHED{\SQUASHEDGLUON}
%\global\newcount\FLIPPEDSQUASHEDGLUON  \FLIPPEDSQUASHEDGLUON=9
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SOME COUNTERS AND DEFINITIONS FOR POSITIONS AND LENGTHS OF LINES & FEATURES %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcount\adjx \adjx=0
\newcount\adjy \adjy=0
\global\newdimen\BIGPHOTONS     \BIGPHOTONS=0pt  %  DEFAULT:  10 & 11-PT PHOTONS
\gdef\bigphotons{\global\BIGPHOTONS=12pt}%FOR 12-PT DOCS DRAWING E-W PHOTONS.
\global\newdimen\THICKPHOTONS     \THICKPHOTONS=0pt  %  FOR E-W PHOTONS 
\global\newdimen\THICKPHOTONSWITCH    \THICKPHOTONSWITCH=0pt
\gdef\THICKPHOTONTEST{
\THICKPHOTONSWITCH=0pt
\ifdim\THICKPHOTONS=0pt \relax
  \else \ifnum\LTYPE=3
           \ifnum\LDIR=2 \THICKPHOTONSWITCH=1pt \fi % THICK \E PHOTON
           \ifnum\LDIR=6 \THICKPHOTONSWITCH=1pt \fi % THICK \W PHOTON
        \fi
\fi
}  % end of THICKPHOTONTEST
\gdef\THICKLINES{\thicklines  \THICKPHOTONS=1pt}
\gdef\THINLINES{\thinlines  \THICKPHOTONS=0pt}
\global\newcount\phantomswitch   \global\phantomswitch=0
\global\newcount\stemlength   \global\stemlength=275   % Default STEM length.
\global\newcount\absstemlength        % A copy of STEM length.
\global\newcount\stemlengthx          % FOR STEMS on particle lines
\global\newcount\stemlengthy          % FOR STEMS on particle lines
\newdimen\FRONTSTEM  \FRONTSTEM=0pt   % FOR STEMS
\newdimen\BACKSTEM   \BACKSTEM=0pt    % FOR STEMS
\newdimen\EITHERSTEM \EITHERSTEM=0pt  % FOR STEMS
\gdef\frontstemmed{\FRONTSTEM=1pt}            % FOR STEMS
\gdef\backstemmed{\BACKSTEM=1pt}              % FOR STEMS
\gdef\stemmed{\FRONTSTEM=1pt  \BACKSTEM=1pt}    % FOR STEMS
\global\newcount\arrowlength                % FOR ARROWS
\global\newdimen\ATTIP   \global\ATTIP=0pt  % FOR ARROWS
\global\newdimen\ATBASE  \global\ATBASE=1pt % FOR ARROWS
\global\newcount\unitboxnumber  % SHOWS THE NUMBER OF `UNIT BOXES' IN LINE
\global\newcount\unitboxnumberpo  % One more than \unitboxnumber (in GLUONSETUP)
\global\newcount\particlelengthx  % THE X-LENGTH OF THE PARTICLE LINE
\gdef\plengthx{\particlelengthx}
\global\newcount\particlelengthy  % THE Y-LENGTH OF THE PARTICLE LINE
\gdef\plengthy{\particlelengthy}  
\global\newcount\boxlengthx  % THE X-LENGTH OF THE BOX:  abs(plengthx) usually
\global\newcount\boxlengthy  % THE y-LENGTH OF THE box:  abs(plengthy) usually
\global\newcount\particleadjustx  % Replaces \gluonadjustx, \scalaradjustx etc.
\global\newcount\particleadjusty  % Replaces \gluonadjusty, \scalaradjusty etc.
\global\newcount\particlelength   % The LENGTH of a particle line BOX (x)
\global\newcount\particlefrontx
\gdef\pfrontx{\particlefrontx}
\global\newcount\PFRONTx
\global\newcount\particlefronty
\gdef\pfronty{\particlefronty}
\global\newcount\PFRONTy
\global\newcount\particlebackx
\gdef\pbackx{\particlebackx}
\global\newcount\particlebacky
\gdef\pbacky{\particlebacky}
\global\newcount\particlemidx
\gdef\pmidx{\particlemidx}
\global\newcount\particlemidy
\gdef\pmidy{\particlemidy}
% SOME SPECIAL DEFS FOR \SCALARs:
\global\newcount\seglength  \global\newcount\gaplength
\global\gaplength=850  %default
\global\seglength=1416  % Length of each seg not including `ends' for attachment
% Now some storage locations for the user:
\global\newcount\Xone    \global\newcount\Yone    % user co-ords (\Xone,\Yone)
\global\newcount\Xtwo    \global\newcount\Ytwo    % user co-ords (\Xtwo,\Ytwo)
\global\newcount\Xthree  \global\newcount\Ythree  % user's (\Xthree,\Ythree)
\global\newcount\Xfour   \global\newcount\Yfour   % user co-ords (\Xfour,\Yfour)
\global\newcount\Xfive   \global\newcount\Yfive   % user co-ords (\Xfive,\Yfive)
\global\newcount\Xsix    \global\newcount\Ysix    % user co-ords (\Xsix,\Ysix)
\global\newcount\Xseven  \global\newcount\Yseven  % user's (\Xseven,\Yseven)
\global\newcount\Xeight  \global\newcount\Yeight  % user's (\Xeight,\Yeight)
%
%  SOME COUNTERS IDENTIFYING VARIOUS LINE PORTIONS AND DIMENSIONS:
%
\newsavebox{\lastline}  %  Default name for an unnamed particle line.
\global\newcount\numlineparts   % Num of pieces each unitbox of the line needs
\global\newcount\upperlineadjx  \upperlineadjx=0  %Default
\global\newcount\upperlineadjy  \upperlineadjy=0  %Default
\global\newcount\lowerlineadjx  \lowerlineadjx=0  %Default
\global\newcount\lowerlineadjy  \lowerlineadjy=0  %Default
\global\newcount\thirdlineadjx  \thirdlineadjx=0  %Default
\global\newcount\thirdlineadjy  \thirdlineadjy=0  %Default
\global\newcount\fourthlineadjx \fourthlineadjx=0  %Default
\global\newcount\fourthlineadjy \fourthlineadjy=0  %Default
\global\newcount\unitboxwidth   \unitboxwidth=1000%Default
\global\newcount\unitboxheight  \unitboxheight=0  %Default
\global\newcount\numupperunits  \numupperunits=8  %Default
\global\newcount\numlowerunits  \numlowerunits=8  %Default
\global\newcount\numthirdunits  \numthirdunits=8  %Default
\global\newcount\numfourthunits \numfourthunits=8  %Default
%  Some counters.  =0 until a line-type is drawn. Then=1.
\global\newcount\fermioncount   \global\fermioncount=0    
\global\newcount\scalarcount    \global\scalarcount=0    
\global\newcount\photoncount    \global\photoncount=0    
\global\newcount\gluoncount     \global\gluoncount=0    
\global\newcount\especialcount  \global\especialcount=0    
\global\newcount\vertexcount    \global\vertexcount=-1
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%     AUXILIARY ROUTINES FOR SETTING PARTICLE DIRECTIONS     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\global\newcount\XDIR
\global\newcount\YDIR
\gdef\SETDIR{  % SETS THE DIRECTIONS
\ifcase\LDIR 
     \global\XDIR=0  \global\YDIR=1   %\N  case.
\or  \global\XDIR=1  \global\YDIR=1   %\NE case.
\or  \global\XDIR=1  \global\YDIR=0   %\E  case.
\or  \global\XDIR=1  \global\YDIR=-1  %\SE case.
\or  \global\XDIR=0  \global\YDIR=-1  %\S  case.
\or  \global\XDIR=-1 \global\YDIR=-1  %\SW case.
\or  \global\XDIR=-1 \global\YDIR=0   %\W  case.
\or  \global\XDIR=-1 \global\YDIR=1   %\NW case.
\else\DIRECTERROR 
\fi}  % END OF \SETDIR
%
\gdef\moduloeight#1{
\ifnum#1>7 \global\advance #1 by -8 
\relax
\moduloeight#1 
\relax
\else \relax  
\fi}
\gdef\multroothalf#1{\global\multiply #1 by 7071 \global\divide #1 by 10000}
\gdef\negate#1{\global\multiply #1 by -1}
\gdef\double#1{\global\multiply #1 by 2}
\gdef\slanttest(#1,#2){ 
\ifodd\LDIR
\multiply #1 by 7071  \divide #1 by 10000
\multiply #2 by 7071  \divide #2 by 10000
\fi
}
\gdef\gslanttest(#1,#2){
\ifodd\LDIR
\multroothalf#1
\multroothalf#2
\fi
}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% AUXILIARY ROUTINES FOR SETTING PARTICLE LENGTHS & POSTIONS %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\gdef\setplength{ % calcs length of particle line
\global\particlelengthx=\unitboxwidth
\global\particlelengthy=\unitboxheight
\global\multiply \particlelengthx by \unitboxnumber
\global\multiply \particlelengthy by \unitboxnumber
\global\advance \particlelengthx by \particleadjustx
\global\advance \particlelengthy by \particleadjusty
}
\gdef\boxlengthdefault{  % DEFAULT FOR BOX SIZES IN \drawas
\global\boxlengthx=\plengthx
\global\boxlengthy=\plengthy
\ifnum\plengthx<0 \global\multiply\boxlengthx by -1 \fi
\ifnum\plengthy<0 \global\multiply\boxlengthy by -1 \fi
}
\gdef\rearcoords{  %  CALCULATES THE CO-ORDINATES OF THE BACK OF PARTICLE LINE
\global\particlebacky=\particlefronty 
\global\particlebackx=\particlefrontx 
\global\advance \particlebackx by \particlelengthx
\global\advance \particlebacky by \particlelengthy
}
\gdef\midcoords{  %  CALCULATES THE CO-ORDINATES OF THE MID OF PARTICLE LINE
\global\particlemidy=\particlefronty
\global\particlemidx=\particlefrontx
\global\stemlengthx=\particlelengthx  % Convenient variables not being used
\global\stemlengthy=\particlelengthy  
\global\divide\stemlengthx by 2
\global\divide\stemlengthy by 2
\global\advance \particlemidx by \stemlengthx
\global\advance \particlemidy by \stemlengthy
}
\gdef\setparticle{\setplength\rearcoords\midcoords\boxlengthdefault}  %sets line
%
\gdef\setcoords(#1,#2,#3)(#4,#5,#6)[#7,#8]{  
% Sets co-ords of first 3 line-parts of a line and the unitbox height and width
% Used by photons and gluons.
\global\upperlineadjx=#1
\global\lowerlineadjx=#2
\global\thirdlineadjx=#3
\global\upperlineadjy=#4
\global\lowerlineadjy=#5
\global\thirdlineadjy=#6
\global\unitboxwidth=#7
\global\unitboxheight=#8
}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                     ROUTINES FOR DRAWING LINES                              %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% **************   ROUTINE FOR DRAWING STORED LINES AND PICTURES   *************
%
\gdef\drawoldpic#1(#2,#3){  % DRAWS PRE-SAVED PICTURE
\global\particlefrontx=#2
\global\particlefronty=#3
\rearcoords  
\midcoords
\put(#2,#3){\usebox{#1}}
}
%
\gdef\drawsavedline`#1' as #2[#3#4](#5,#6)[#7]{
\global\LINETYPE=#2
\global\LINEDIRECTION=#3
\global\LINECONFIGURATION=#4
\global\particlefrontx=#5
\global\particlefronty=#6
\global\unitboxnumber=#7  
% Formerly called \numhalfwiggles,\numdashes, \numloops, \fermionlength
% #1 is saved linename;   #2 is \LINETYPE;    #3 is \LINEDIRECTION
% #4 is \LINECONFIGURATION (#5,#6)=(x,y) co-ords;  #7 is linelength (eg#wiggles)
\selectcase
\rearcoords% moved from before selectcase.
\midcoords
\ifnum\phantomswitch=0 \drawas{#1}\fi
% if \phantomswitch=1 then just set the line up and don't draw it.
}

\gdef\startphantom{\phantomswitch=1} % BEGIN PHANTOM MODE.
\gdef\stopphantom{\phantomswitch=0}  % END PHANTOM MODE.
% USE AS: 
% \startphantom...\drawline\gluon[...]...\drawvertex\photon...\stopphantom

\gdef\drawas#1{
\global\savebox{#1}(\boxlengthx,\boxlengthy){
\setlength{\unitlength}{0.01pt}
\begin{picture}(\boxlengthx,\boxlengthy)
\multiput(\upperlineadjx,\upperlineadjy)(\unitboxwidth,\unitboxheight)
{\numupperunits}{\upperunitbox}
\ifnum\numlineparts > 1  %  If the line needs 2 parts per unit or more
\multiput(\lowerlineadjx,\lowerlineadjy)(\unitboxwidth,\unitboxheight)
{\numlowerunits}{\lowerunitbox}  
\fi
\ifnum\numlineparts > 2  %  If the line needs 3 parts per unit or more
\multiput(\thirdlineadjx,\thirdlineadjy)(\unitboxwidth,\unitboxheight)
{\numthirdunits}{\thirdunitbox}  
\fi
\ifnum\numlineparts > 3  %  If the line needs 4 parts per unit or more
\multiput(\fourthlineadjx,\fourthlineadjy)(\unitboxwidth,\unitboxheight)
{\numfourthunits}{\lowerunitbox}  
\fi
\end{picture} }
% CHECK STEMS
\global\PFRONTx=\pfrontx  \global\PFRONTy=\pfronty   %save this value
\SETFRONTSTEM
% Now take into account the possibility of THICK E-W photons (drawn twice)
\THICKPHOTONTEST
\ifdim\THICKPHOTONSWITCH=1pt\global\advance\PFRONTy by 20  \fi
\put(\PFRONTx,\PFRONTy) {\usebox{#1}}   %\pfrontX,Y=\particlefrontx,y
%\put(\particlefrontx,\particlefronty) {\usebox{#1}}
\ifdim\THICKPHOTONSWITCH=1pt
\global\advance\PFRONTy by -40
\put(\PFRONTx,\PFRONTy) {\usebox{#1}}   % The second \E or \W photon ->thicker
\global\advance \PFRONTy by 20  %re-adjust:  advanced by -20 in total above.
\fi  %End of \ifdim\THICKPHOTONSWITCH=1
\SETBACKSTEM
\seglength=1416   \gaplength=850   % Re-set \SCALR defaults.
}
%
% *********   ROUTINES FOR STORING LINES  *******
%

\gdef\drawandsaveline`#1' as #2[#3#4](#5,#6)[#7]{
% #1 is saved linename;   #2 is \LINETYPE;    #3 is \LINEDIRECTION
% #4 is \LINECONFIGURATION (#5,#6)=(x,y) co-ords;  #7 is linelength (eg#wiggles)
\global\newsavebox{#1}
\drawsavedline`#1' as #2[#3#4](#5,#6)[#7]
}

\gdef\drawline#1[#2#3](#4,#5)[#6]{   % Draw line but don't name it.
\drawsavedline`\lastline' as #1[#2#3](#4,#5)[#6]}

\gdef\saveas#1{  %  For saving a line after the fact.
\global\newsavebox#1
\drawas#1}
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                      %
%                           C A S E S                                  %
%                           ---------                                  % 
%                                                                      %       
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ERROR MESSAGES FOR INCORRECT CASE SPECIFICATION:
\gdef\TYPEERROR{\message{*** ERROR IN PARTICLE TYPE SELECTION ***}
\message{+++ Try with line type \fermion,\scalar,\photon,\gluon 
(see manual) +++}\SETERR}
\gdef\DIRECTERROR{\SETERR\message{*** ERROR IN PARTICLE DIRECTION SELECTION ***}
\message{+++ Try again with direction N, NE, E, SE  etc. or see manual +++}}
\gdef\UNIMPERROR{\message{*** ERROR IN PARTICLE OPTIONS SELECTION ***}
\message{
+++ The requested options combination has not yet been implemented +++}\SETERR}
\gdef\SETERR{\gdef\upperunitbox{{\tiny Error}}  % PRINTS `error' in diagram.
\gdef\lowerunitbox{\relax}
\gdef\thirdunitbox{\relax}
}
\gdef\neglengthcheck{\ifnum\unitboxnumber < 1 
\message{   *** ERROR:  PARTICLE OF NEGATIVE OR ZERO LENGTH REQUESTED. ***   }
\message{   ***         TAKING ABSOLUTE VALUE. ***   }\negate\unitboxnumber \fi}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\gdef\selectcase{  
\neglengthcheck   %  check for particles of negative length.
% select PARTICLE alignment:
\SETDIR  
%  Select particle type
\ifcase\LINETYPE
\TYPEERROR  % \LINETYPE=0 case.
\or \selectfermion  % \LINETYPE=1 case.
\or \selectscalar   % \LINETYPE=2 case.
\or \selectphoton   % \LINETYPE=3 case.
\or \selectgluon    % \LINETYPE=4 case.
\or \selectespecial % \LINETYPE=5 case.
\else \TYPEERROR \fi  }
%%%%%%%%%%% (1) FERMIONS %%%%%%%%%%% 
\gdef\selectfermion{
% Input fermion-setup stuff ONLY IF HAVE NOT DONE SO YET.
% This avoids having to process a fermion if none are drawn.
\ifnum\fermioncount=0 \input fermionsetup \fi   
%                  CONTAINS fermion DEFINITIONS.
\global\advance\fermioncount by 1  % Counts number of fermions drawn. 
\ALLfermion   
}
%%%%%%%%%%% (2) SCALARS %%%%%%%%%%% 
\gdef\selectscalar{
% Input scalar-setup stuff ONLY IF HAVE NOT DONE SO YET.
% This avoids having to process a scalar if none are drawn.
\ifnum\scalarcount=0 \input scalarsetup \fi   
%                 CONTAINS scalar DEFINITIONS.
\global\advance\scalarcount by 1  % Counts number of scalars drawn. 
\ALLscalar
}
%%%%%%%%%%% (3) PHOTONS %%%%%%%%%%% 
\gdef\selectphoton{   % RECURSIVELY RE-DEFINED IN PHOTONSETUP(23+).TEX.
% Input photon-setup stuff ONLY IF HAVE NOT DONE SO YET.
% This avoids having to process a photon if none are drawn.
\ifnum\photoncount=0 \input photonsetup  \fi   
\selectphoton
%CONTAINS PHOTON DEFINITIONS. 
}
%%%%%%%%%%% (4) GLUONS %%%%%%%%%%% 
\gdef\selectgluon{   % RECURSIVELY RE-DEFINED IN GLUONSETUP(25+).TEX.
% Input gluon-setup stuff ONLY IF HAVE NOT DONE SO YET.
% This avoids having to process a gluon if none are drawn.
\ifnum\gluoncount=0 \input gluonsetup  \fi
\selectgluon
%                  CONTAINS gluon DEFINITIONS.
}
%%%%%%%%%%% (5) SPECIAL - USER DEFINED %%%%%%%%%%% 
\gdef\selectespecial{\UNIMPERROR}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                   ROUTINES FOR DRAWING VERTICES                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Input vertex-setup stuff ONLY IF HAVE NOT DONE SO YET.                      %
% This avoids having to process a vertex if none are drawn. 
\gdef\checkvertex{ %immediately re-defines \drawvertex,\vertexcap,\linkvertex...
\ifnum\vertexcount=-1   \input vertex  \fi}
% RECURSIVE DEFINITIONS:
\gdef\drawvertex#1[#2#3](#4,#5)[#6]{\checkvertex\drawvertex#1[#2#3](#4,#5)[#6]}
\gdef\vertexcap#1{\checkvertex\vertexcap#1}
\gdef\vertexcaps{\checkvertex\vertexcaps}
\gdef\vertexlink#1{\checkvertex\vertexlink#1}
\gdef\vertexlinks{\checkvertex\vertexlinks}
\gdef\stemvertex#1{\checkvertex\stemvertex#1}
\gdef\stemvertices{\checkvertex\stemvertices}
\gdef\flipvertex{\checkvertex\flipvertex}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                   ROUTINES FOR DRAWING ARROWS                               %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% SYNTAX:  \drawarrow[\NW\ATBASE](\pmidx,\pmidy)  etc.
\global\arrowlength=349  % Length of arrow
\gdef\drawarrow[#1#2](#3,#4){
\global\LDIR=#1
\SETDIR
\global\boxlengthx=#3  % Just a convenient variable name.  No significance.
\global\boxlengthy=#4  % The Arrow co-ordinates.
\ifdim#2=1pt  % CASE \ATBASE WHERE THE CO-ORDS ARE AT THE ARROWS BASE.
   %   #2 IS either \ATTIP or \ATBASE...Depending where it is to be positioned.
\adjx=\arrowlength      \adjy=\arrowlength
\multiply\adjx by \XDIR \multiply\adjy by \YDIR  % Set in \SETDIR
\slanttest(\adjx,\adjy)
\global\advance\boxlengthx by \adjx    \global\advance\boxlengthy by \adjy
\fi
\ifnum\phantomswitch=0\put(\boxlengthx,\boxlengthy){\vector(\XDIR,\YDIR){0}}\fi
}  % END OF \drawarrow.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                     ROUTINES FOR DRAWING STEMS                              %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\gdef\SETFRONTSTEM{
\EITHERSTEM=\FRONTSTEM   \advance\EITHERSTEM by \BACKSTEM
\ifdim\EITHERSTEM>0pt
\global\stemlengthx=\stemlength   \global\stemlengthy=\stemlength   
\global\absstemlength=\stemlength   
\SETDIR
\gslanttest(\stemlengthx,\stemlengthy)
\gslanttest(\absstemlength,\REG)  % the \REG is to use up the parameter space.
\ifnum\XDIR=0 \stemlengthx=0 \fi
\ifnum\YDIR=0 \stemlengthy=0 \fi
\global\multiply\stemlengthx by \XDIR
\global\multiply\stemlengthy by \YDIR
\ifdim\FRONTSTEM=1pt 
\ifnum\phantomswitch=0
          \put(\pfrontx,\pfronty){\line(\XDIR,\YDIR){\absstemlength}}\fi
\global\advance\plengthx by \stemlengthx
\global\advance\plengthy by \stemlengthy
\global\advance\PFRONTx by \stemlengthx   
\global\advance\PFRONTy by \stemlengthy
\global\advance\pmidx by \stemlengthx
\global\advance\pmidy by \stemlengthy
\global\advance\pbackx by \stemlengthx
\global\advance\pbacky by \stemlengthy
% FOR STEMMED PHOTONS AND GLUONS, \photonfront,back(x,y) are for the
% photon proper (no stem) while \pbackx,y include the stems.
\ifnum\LTYPE=3
\global\photonfrontx=\PFRONTx  \global\photonfronty=\PFRONTy
\global\photonbackx=\pbackx    \global\photonbacky=\pbacky
\fi  % END LTYPE
\ifnum\LTYPE=4
\global\gluonfrontx=\PFRONTx  \global\gluonfronty=\PFRONTy
\global\gluonbackx=\pbackx    \global\gluonbacky=\pbacky
\fi  % END LTYPE
\fi  % END FRONTSTEM
\fi  % END EITHERSTEM
}    % end of \SETFRONTSTEM
%
\gdef\SETBACKSTEM{
\ifdim\BACKSTEM=1pt 
\ifnum\phantomswitch=0
       \put(\pbackx,\pbacky){\line(\XDIR,\YDIR){\absstemlength}}\fi
\global\advance\plengthx by \stemlengthx
\global\advance\plengthy by \stemlengthy
\global\advance\pbackx by \stemlengthx
\global\advance\pbacky by \stemlengthy
\fi  % END BACKSTEM
\global\stemlength=275  \FRONTSTEM=0pt  \BACKSTEM=0pt % Reset default switches.
}    % END OF \SETBACKSTEM 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                              LOOPS                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\gdef\drawloop#1[#2#3](#4,#5){  %RECURSIVE.  
\input loops  % contains loops definitions
\drawloop#1[#2#3](#4,#5)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Feynmanlength  % Set length scale to centipoints.
