% !TEX useAlternatePath
% !TEX useConTeXtSyncParser

% Setting up the overlay for \hpos
\defineoverlay [graphics] [\positionoverlay{graphics}]
\setupbackgrounds
  [page]
  [background={foreground,graphics}]

% The MetaPost code for drawing the contractions.
\startMPpositiongraphic{mypos:contract}
     path pa, pb, pab ; numeric na, nb ; string ta, tb ;
     ta := mpvars("from") ;
     tb := mpvars("to") ;
     na := positionpage(ta) ; % page number of a
     nb := positionpage(tb) ; % page number of b
     pa := positionbox(ta) ;  % box of a
     pb := positionbox(tb) ;  % box of b
     z1 = 0.5[ulcorner pa,urcorner pa] ;
     z2 = 0.5[ulcorner pb,urcorner pb] ;
     y3 = max(y1,y2) + 3pt ; 
     if na = nb : % Check that positions are on the same page
       pab := z1 -- (x1,y3) -- (x2,y3) -- z2{down} ;
       pickup pencircle scaled 0.6pt ;
       draw pab yshifted 1pt ;
       positioninregion ;
     fi ;
\stopMPpositiongraphic

\startMPpositiongraphic{mypos:contractbelow}
     path pa, pb, pab ; numeric na, nb ; string ta, tb ;
     ta := mpvars("from") ;
     tb := mpvars("to") ;
     na := positionpage(ta) ; % page number of a
     nb := positionpage(tb) ; % page number of b
     pa := positionbox(ta) ;  % box of a
     pb := positionbox(tb) ;  % box of b
     z1 = 0.5[llcorner pa,lrcorner pa] ;
     z2 = 0.5[llcorner pb,lrcorner pb] ;
     y3 = min(y1,y2) - 3pt ; 
     if na = nb : % Check that positions are on the same page
       pab := z1 -- (x1,y3) -- (x2,y3) -- z2{down} ;
       pickup pencircle scaled 0.6pt ;
       draw pab yshifted -1pt ;
       positioninregion ;
     fi ;
\stopMPpositiongraphic

% Lua counts the spin formulas 
\ctxlua{sfnum = 0} % Count starts at zero
\def\sfID{\ctxlua{context(sfnum)}} % ConTeXt can call the count from Lua to use as an ID for each equation.

% Spin formulas increase the spin formula count and typeset the formula as usual.
\define\startspinformula{
  \ctxlua{sfnum = sfnum + 1}
  \startformula
}
\define\stopspinformula{\stopformula}

% My own \mpos uses the spin formula ID as part of the box label, then sets the contents as math.
\define[2]\mpos{\hpos{\sfID:#1}{$#2$}}

% Two commands for requesting contractions (the connections between spinors), using the spin formula ID.
\define[2]\contract{%
  \startpositionoverlay{graphics}%
    \setMPpositiongraphic{\sfID:#1}{mypos:contract}{to=\sfID:#2}%
  \stoppositionoverlay%
}
\define[2]\contractbelow{%
  \startpositionoverlay{graphics}%
    \setMPpositiongraphic{\sfID:#1}{mypos:contractbelow}{to=\sfID:#2}%
  \stoppositionoverlay%
}

% A textstyle fraction to make the normalization factor look nice.
\define[2]\textfrac{{\textstyle\frac{#1}{#2}}}


\starttext

\startspinformula
  \mpos{X}{e}_\alpha \mpos{Y}{e}^\mu \mpos{Z}{e}_{\dot\beta}
    = \textfrac{1}{\sqrt{2}}\sigma_{\alpha\dot\beta}^\mu
  \contract{X}{Y}
  \contractbelow{Y}{Z}
\stopformula

\startspinformula
  \mpos{X}{e}^{\dot\alpha} \mpos{Y}{e}_\mu \mpos{Z}{e}^{\beta}
    = \textfrac{1}{\sqrt{2}}\bar\sigma^{\dot\alpha\beta}_\mu
  \contractbelow{X}{Y}
  \contract{Y}{Z}
\stopformula

%\startformula
%  % Each \connectboxanchors works alone, but both together make trouble:
%    %\connectboxanchors[top][top]{X}{Y}
%    \connectboxanchors[bottom][bottom]{Y}{Z}
%  \mathboxanchored{X}{e}_\alpha \mathboxanchored{Y}{e}^\mu \mathboxanchored{Z}{e}_{\dot\beta}
%    = \textfrac{1}{\sqrt{2}}\sigma_{\alpha\dot\beta}^\mu
%\stopformula
%\blank[8mm]
%
%\startformula
%  % Each \connectboxanchors works alone, but both together make trouble:
%    \connectboxanchors[top][top]{X}{Y}
%    %\connectboxanchors[bottom][bottom]{Y}{Z}
%  \mathboxanchored{X}{e}_\alpha \mathboxanchored{Y}{e}^\mu \mathboxanchored{Z}{e}_{\dot\beta}
%    = \textfrac{1}{\sqrt{2}}\sigma_{\alpha\dot\beta}^\mu
%\stopformula

\stoptext