% latex-squares.sty  29 May 2013

% Macros for inserting pictures of square dance formations into LaTeX documents.
%
% This style option is designed to work with the article document
% style of LaTeX 2e.  Use
% \documentclass{article}
% \usepackage{latex-squares}

% Author: Guy Steele
%  Inspired by, and using parts of, the tex-squares.sty package
%  by Larry Denenberg and Stephen Gildea.
%  Comments and suggestions welcome.



\NeedsTeXFormat{LaTeX2e}[1994/12/01]	%for \textbullet
\ProvidesPackage{latex-squares}[2009/01/18 Square Dance Formations]
\RequirePackage{color,pict2e,graphicx}
\RequirePackage[geometry]{ifsym}
%\RequirePackage{MnSymbol}

\AtBeginDocument{\font\dancerfont=\fontname\font}

\newcount\atsignsymmetry
\newcount\possibleatsignsymmetry
\newcount\atsignnose


% User-settable parameters

% The thickness of lines used to draw dancer symbols.
\newdimen\dancerrulethickness \dancerrulethickness=0.6pt

% \dancersize is the nominal size (width and height) of a dancer symbol; the entire
% symbol, including stroke width, should fit within a square that is
% (\dancersize plus \dancerrulethickness) times (\dancersize plus \dancerrulethickness).
\newdimen\dancersize  \dancersize=2.8ex

% \dancerspotsep is a decimal number that, when multiplied by \dancersize,
% specifies the distance between dancer spots in a standard dancer matrix.
% This is the distance that "Press Ahead" would move a dancer.
\newcommand\dancerspotsep{2.2}

% \dancerspotdenom should be a small integer, typically 1, 2, 3, or 4.
% It is the number of spots in the notation matrix required to span one dancer spot.
\newcount\dancerspotdenom  \dancerspotdenom=2

\newdimen\dancerseprulethickness \dancerseprulethickness=1.1pt
\newcount\dancerseprulecount

% \dancernosesize is a decimal number that, when multiplied by \dancersize,
% specifies the diameter of a dancer's nose.  For a full-circle nose,
% 0.4 seems to be a good value; for a half-circle nose, 0.45 is better.
\newcommand\dancernosesize{0.45}

% \dancernosefraction is a decimal number that indicates what fraction of
% a nose's diameter should protrude form the dancer symbol.
% For a full-circle nose, use 1.0; for a half-circle nose, use 0.5 .
% I think 0.625 is a good value for many purposes.
\newcommand\dancernosefraction{0.625}

\newif\ifhalfcirclenoses \halfcirclenosesfalse
\newcommand\halfcirclenoses{\halfcirclenosestrue}
\newcommand\fullcirclenoses{\halfcirclenosesfalse}

\newif\iffontdancers         \fontdancerstrue
\newif\ifdancerhollowsquare  \dancerhollowsquarefalse
\newif\ifdancersquareoval    \dancersquareovalfalse
\newcommand\dancercornerradius{0.5}
\newif\ifquantizedancersize  \quantizedancersizefalse
\newif\ifquantizedancersep   \quantizedancersepfalse

\newcommand\fulldancerspots{\dancerspotdenom=1 }
\newcommand\halfdancerspots{\dancerspotdenom=2 }
\newcommand\quarterdancerspots{\dancerspotdenom=4 }

\newcommand\MainstreamStyle{\renewcommand\dancerspotsep{1.5}\renewcommand\dancernosesize{0.45}\renewcommand\dancernosefraction{0.625}\fulldancerspots}
\newcommand\PlusStyle{\renewcommand\dancerspotsep{1.7}\renewcommand\dancernosesize{0.4}\renewcommand\dancernosefraction{0.75}\halfdancerspots}
\newcommand\AdvancedStyle{\renewcommand\dancerspotsep{2.2}\renewcommand\dancernosesize{0.4}\renewcommand\dancernosefraction{0.75}\halfdancerspots}
\newcommand\ChallengeStyle{\renewcommand\dancerspotsep{2.2}\renewcommand\dancernosesize{0.4}\renewcommand\dancernosefraction{0.75}\halfdancerspots}

\newdimen\dancermatrixunit
\newdimen\dancertempdimen
\newcount\dancerphantomdashes\dancerphantomdashes=2    %Eric's value.  Larry used 4.
\newskip\FormationSep \FormationSep=12pt plus 6pt minus 2pt
\newskip\SquareDanceDiagramSep \SquareDanceDiagramSep=10pt plus 4pt minus 2pt
\newskip\SquareDanceBigCaptionSep \SquareDanceBigCaptionSep=8pt plus 4pt minus 2pt
\newskip\SquareDanceCaptionSep \SquareDanceCaptionSep=6pt plus 4pt minus 2pt

\newskip\SquareDanceOuterSkipii \SquareDanceOuterSkipii=36pt plus 1fil minus 36pt
\newskip\SquareDanceInnerSkipii \SquareDanceInnerSkipii=72pt minus 48pt
\newskip\SquareDanceOuterSkipiii \SquareDanceOuterSkipiii=18pt plus 1fil minus 9pt
\newskip\SquareDanceInnerSkipiii \SquareDanceInnerSkipiii=36pt minus 18pt
\newskip\SquareDanceOuterSkipiv \SquareDanceOuterSkipiv=12pt plus 1fil minus 8pt
\newskip\SquareDanceInnerSkipiv \SquareDanceInnerSkipiv=24pt minus 16pt
\newskip\SquareDanceOuterSkipv \SquareDanceOuterSkipv=12pt plus 1fil minus 4pt
\newskip\SquareDanceInnerSkipv \SquareDanceInnerSkipv=24pt minus 8pt
\newskip\SquareDanceOuterSkipvi \SquareDanceOuterSkipvi=12pt plus 1fil minus 4pt
\newskip\SquareDanceInnerSkipvi \SquareDanceInnerSkipvi=24pt minus 8pt
\newskip\SquareDanceOuterSkipvii \SquareDanceOuterSkipvii=12pt plus 1fil minus 4pt
\newskip\SquareDanceInnerSkipvii \SquareDanceInnerSkipvii=24pt minus 8pt
\newskip\SquareDanceOuterSkipviii \SquareDanceOuterSkipviii=12pt plus 1fil minus 4pt
\newskip\SquareDanceInnerSkipviii \SquareDanceInnerSkipviii=24pt minus 8pt


\newcount\dancertempcounta
\newcount\dancertempcountb
\newcount\dancertempcountc
\newcount\dancertempcountd
\newcount\dancertempcounte
\newcount\dancertempcountf
\newcount\dancertempcountz

% \newcommand\calculateunitsfromdimen[2]{\dancertempcounta=#2\dancertempcountb=\dancermatrixunit
%   % Now need to multiply by 1000 and divide by \dancermatrixunit, avoiding overflow.
%   \ifnum \dancertempcounta>65536000
%     \divide\dancertempcounta by \dancertempcountb
%     \multiply\dancertempcounta by 1000
%   \else\ifnum \dancertempcounta>6553600
%     \multiply\dancertempcounta by 20
%     \divide\dancertempcounta by \dancertempcountb
%     \multiply\dancertempcounta by 50
%   \else\ifnum \dancertempcounta>1310720
%     \multiply\dancertempcounta by 100
%     \divide\dancertempcounta by \dancertempcountb
%     \multiply\dancertempcounta by 10
%   \else
%     \multiply\dancertempcounta by 1000
%     \divide\dancertempcounta by \dancertempcountb
%   \fi\fi\fi
%   % Now render this as a decimal number with three places after the decimal point.
%   \dancertempcountd=\dancertempcounta \divide\dancertempcounta by 10 \dancertempcountz=\dancertempcounta
%   \multiply\dancertempcountz by 10 \advance\dancertempcountd by -\dancertempcountz
%   \dancertempcountc=\dancertempcounta \divide\dancertempcounta by 10 \dancertempcountz=\dancertempcounta
%   \multiply\dancertempcountz by 10 \advance\dancertempcountc by -\dancertempcountz
%   \dancertempcountb=\dancertempcounta \divide\dancertempcounta by 10 \dancertempcountz=\dancertempcounta
%   \multiply\dancertempcountz by 10 \advance\dancertempcountb by -\dancertempcountz
%   \edef#1{\the\dancertempcounta.\the\dancertempcountb\the\dancertempcountc\the\dancertempcountd}}

\newcount\formationx
\newcount\formationy
\newcount\formationxsize
\newcount\formationcenterx
\newcount\formationcentery
\newcount\diagramsinrow
\newcount\flagpolesymmetry  %0 for no symmetry, or 2 (bigon), 4 (square), 6 (hexagon), or 8 (octagon)
\newcount\flagpolenose      %bit-encoded: 1=South, 2=East, 4=North, 8=West
\newcount\numberofcolumns
\newbox\formationbox
\newbox\diagrambox
\newbox\rowdiagramsbox
\newbox\rowcaptionsbox
\newbox\formationtempboxa
\newbox\formationtempboxb
\newbox\dancerlabelbox

\newdimen\formationxdimen
\newdimen\formationydimen
\newdimen\formationminx
\newdimen\formationmaxx
\newdimen\formationminy
\newdimen\formationmaxy
\newdimen\formationtempdimen

\newif\ifdancertilted
\newif\ifdancerspot
\newif\ifdancercounts
\newif\ifdancerseprule
\newif\ifformationrotated
\newif\ifdancerlabelisX
\newif\ifdancerlabelisslash
\newif\ifdancerlabelisbox
\newif\ifdancerwarp
\newif\ifdancerflagpole

% Multiplication routine for multiplying a "dimen" by a decimal fraction.
% Example usage: \multiplydimen\mydimenreg(1.41421}
% Not accurate to the very last bit, but does avoid undeserved overflow.

\newdimen\multiplytempdimen
\newdimen\multiplytempresult
\newcount\multiplytempcount

\newcommand\multiplydimen[2]{\multiplytempdimen=#2pt\multiplytempcount=\multiplytempdimen
  \multiplytempresult=0pt
  \multiplytempdimen=#1\divide\multiplytempdimen by 2
  \multiplydimenstep \multiplydimenstep \multiplydimenstep \multiplydimenstep
  \multiplydimenstep \multiplydimenstep \multiplydimenstep \multiplydimenstep
  \multiplydimenstep \multiplydimenstep \multiplydimenstep \multiplydimenstep
  \multiplydimenstep \multiplydimenstep \multiplydimenstep \multiplydimenlaststep
  \multiply #1 by \multiplytempcount \advance #1 by \multiplytempresult}

\newcommand\multiplydimenstep{\ifodd\multiplytempcount\advance\multiplytempresult by \multiplytempdimen\fi
                              \divide\multiplytempcount by 2 \divide\multiplytempresult by 2}

\newcommand\multiplydimenlaststep{\ifodd\multiplytempcount\advance\multiplytempresult by \multiplytempdimen\fi
                              \divide\multiplytempcount by 2}

\newcommand\dancertiltasneeded[1]{\ifdancertilted
   \expandafter\rotatebox\expandafter{\dancertiltangle}{#1}\else #1\fi}

\newcommand\dancerboxbasic{\iffontdancers \fontdancerbox \else \ifdancerhollowsquare \dancerboxbasicNEW \else \ifdancersquareoval \dancerboxsquareoval \else \dancerboxbasicOLD \fi\fi\fi}

% \newcommand\fontdancerbox{\hbox{\vbox to 0pt{\vss\hbox to 0pt{\hss\color{white}\huge$\blacksquare$\hss}\vss}\relax
%   \vbox to 0pt{\vss\hbox to 0pt{\hss\color{black}\Huge$\square$\hss}\vss}}}
\newcommand\fontdancerbox{\hbox{\vbox to 0pt{\vss\hbox to 0pt{\hss\color{white}\Large\FilledSquare\hss}\vss}\relax
  \vbox to 0pt{\vss\hbox to 0pt{\hss\color{black}\Large\Square\hss}\vss}}}

\newcommand\dancerboxbasicOLD{\hbox to 0pt{\hss
  \vbox to 0pt{\vss
               % Intentionally reduce \dancersize a bit to help optically relative to circle
               \advance\dancersize by -\dancerrulethickness
               \dancertempdimen=\dancersize  \advance\dancertempdimen by \dancerrulethickness
               \hrule height \dancerrulethickness width \dancertempdimen
              \hbox{\vrule height \dancersize width \dancerrulethickness
                     {\color{white}\vrule height \dancersize width \dancersize}%
                     \hskip -\dancerrulethickness
                     \vrule height \dancersize width \dancerrulethickness}
               \vskip -\dancerrulethickness
               \hrule height \dancerrulethickness width \dancertempdimen
               \vss}\hss}\relax
  \ifdancerlabelisX \global\setbox\dancerlabelbox=\hbox{\packagedancerlabel{\dancerboxlabelX}}\fi
  \ifdancerlabelisslash \global\setbox\dancerlabelbox=\hbox{\packagedancerlabel{\dancerboxlabelslash}}\fi
  \ifdancerlabelisbox \global\setbox\dancerlabelbox=\hbox{\packagedancerlabel{\dancerboxlabelbox}}\fi}

\newcommand\dancerboxsquareoval{{\relax
  % Intentionally reduce \dancersize a bit to help optically relative to circle
  \advance\dancersize by -\dancerrulethickness
  \unitlength=\dancersize
  \begin{picture}(0,0)\linethickness{\dancerrulethickness}%
  \put(0,0){\vbox to 0pt{\vss\hbox to 0pt{\hss\color{white}\vrule height \dancersize width \dancersize\hss}\vss}}
  \renewcommand\maxovalrad{\dancercornerradius\dancerrulethickness}\put(0,0){\oval(1,1)}
  \end{picture}\relax
  \ifdancerlabelisX \global\setbox\dancerlabelbox=\hbox{\packagedancerlabel{\dancerboxlabelX}}\fi
  \ifdancerlabelisslash \global\setbox\dancerlabelbox=\hbox{\packagedancerlabel{\dancerboxlabelslash}}\fi
  \ifdancerlabelisbox \global\setbox\dancerlabelbox=\hbox{\packagedancerlabel{\dancerboxlabelbox}}\fi}}


\newcommand\dancerboxbasicNEW{%
  \hbox to 0pt{\hss\vbox to 0pt{\vss\hrule height \dancersize width \dancersize\vss}\hss}%
  \hbox to 0pt{\hss\vbox to 0pt{\vss\advance\dancersize by -2\dancerrulethickness
               \color{white}\hrule height \dancersize width \dancersize\vss}\hss}}

\newcommand\dancerbox{\ifdancertilted \rotatebox{45}{\dancerboxbasic}\else\dancerboxbasic\fi}

\newcommand\dancerblackboxbasic[1]{\hbox to 0pt{\hss
               % Intentionally reduce \dancersize a bit to help optically relative to circle
               \advance\dancersize by -\dancerrulethickness
   \dancertempdimen=\dancersize  \advance\dancertempdimen by \dancerrulethickness
  \vbox to 0pt{#1\vss\hrule height \dancertempdimen width \dancertempdimen\vss}\hss}}

\newcommand\dancerblackbox{\ifdancertilted \rotatebox{45}{\dancerblackboxbasic{}}\else\dancerblackboxbasic{}\fi}
\newcommand\dancergraybox{\ifdancertilted \rotatebox{45}{\dancerblackboxbasic{\color[rgb]{0.8,0.8,0.8}}}\else\dancerblackboxbasic{\color[rgb]{0.8,0.8,0.8}}\fi}

\newcommand\dancercircle{\iffontdancers \fontdancercircle \else \dancercircleOLD \fi}

% \newcommand\fontdancercircle{\hbox{\vbox to 0pt{\vss\hbox to 0pt{\hss\color{white}\huge$\medcircle$\hss}\vss}\relax
%   \vbox to 0pt{\vss\hbox to 0pt{\hss\color{white}\huge$\blacksquare$\hss}\vss}%yes, a square
%   \vbox to 0pt{\vss\hbox to 0pt{\hss\Huge$\medcircle$\hss}\vss}}}
\newcommand\fontdancercircle{\hbox{\vbox to 0pt{\vss\hbox to 0pt{\hss\color{white}\Large\FilledCircle\hss}\vss}\relax
  \vbox to 0pt{\vss\hbox to 0pt{\hss\Large\Circle\hss}\vss}}}

\newcommand\dancercircleOLD{{\unitlength=\dancersize
  \begin{picture}(0,0)\linethickness{\dancerrulethickness}%
  \put(0,0){\color{white}\circle*{1}}\put(0,0){\circle{1}}\end{picture}}\relax
  \ifdancerlabelisX \global\setbox\dancerlabelbox=\hbox{\packagedancerlabel{\dancercirclelabelX}}\fi
  \ifdancerlabelisslash \global\setbox\dancerlabelbox=\hbox{\packagedancerlabel{\dancercirclelabelslash}}\fi
  \ifdancerlabelisbox \global\setbox\dancerlabelbox=\hbox{\packagedancerlabel{\dancercirclelabelbox}}\fi}

\newcommand\dancerblackcircle{{\unitlength=\dancersize
  \begin{picture}(0,0)\linethickness{\dancerrulethickness}%
  \put(0,0){\circle*{1}}\put(0,0){\circle{1}}\end{picture}}}

\newcommand\dancergraycircle{{\unitlength=\dancersize\color[rgb]{0.8,0.8,0.8}
  \begin{picture}(0,0)\linethickness{\dancerrulethickness}%
  \put(0,0){\circle*{1}}\put(0,0){\circle{1}}\end{picture}}}


\newcommand\dancerphantomboxbasic{\hbox to 0pt{\hss
  \vbox to 0pt{\vss\dancerphantomhbar
                   \vskip -\dancerrulethickness
                   \hrule height 0pt
                   \hbox{\dancerphantomvbar
                         \hskip -\dancerrulethickness
                         \hskip \dancersize
                         \hskip -\dancerrulethickness
                         \dancerphantomvbar}
                   \hrule height 0pt
                   \vskip -\dancerrulethickness
                   \dancerphantomhbar
                   \vss}\hss}}

\newcommand\dancerphantombox{\ifdancertilted \rotatebox{45}{\dancerphantomboxbasic}\else\dancerphantomboxbasic\fi}

\newcommand\dancerphantomhbar{\dimen0=\dancersize
  \advance\dimen0 by -2\dancerrulethickness
  \divide\dimen0 by \dancerphantomdashes
  \setbox0=\hbox to \dimen0{\xleaders\hrule height\dancerrulethickness\hskip 0pt plus 3fil
                            \hskip 0pt plus 4fil
                            \xleaders\hrule height\dancerrulethickness\hskip 0pt plus 3fil}\relax
  \hbox to \dancersize{\vrule height \dancerrulethickness width \dancerrulethickness
                       \xleaders\box0\hfil
                       \vrule height \dancerrulethickness width \dancerrulethickness}}

\newcommand\dancerphantomvbar{\dimen0=\dancersize
  \advance\dimen0 by -2\dancerrulethickness
  \divide\dimen0 by \dancerphantomdashes
  \setbox0=\vbox to \dimen0{\xleaders\vrule width\dancerrulethickness\vskip 0pt plus 3fil
                            \vskip 0pt plus 4fil
                            \xleaders\vrule width\dancerrulethickness\vskip 0pt plus 3fil}\relax
  \vbox to \dancersize{\hrule height \dancerrulethickness width \dancerrulethickness
                       \xleaders\box0\vfil
                       \hrule height \dancerrulethickness width \dancerrulethickness}}

\newcommand\dancerstar{%
  \hbox to 0pt{\hss\vbox to 0pt{\vss\hrule width \dancersize height \dancerrulethickness\vss}\hss}%
  \hbox to 0pt{\hss\vbox to 0pt{\vss\hrule width \dancerrulethickness height \dancersize\vss}\hss}}


\newcommand\dancerbigstar{%
  \hbox to 0pt{\hss\vbox to 0pt{\vss\hrule width 2.2\dancersize height \dancerrulethickness\vss}\hss}%
  \hbox to 0pt{\hss\vbox to 0pt{\vss\hrule width \dancerrulethickness height 2.2\dancersize\vss}\hss}}

\newcommand\dancerflagpoledot{{\unitlength=0.3\dancersize
  \begin{picture}(0,0)\linethickness{\dancerrulethickness}%
  \put(0,0){\circle*{1}}\end{picture}}\dancerflagpolecenter}

\newcommand\dancerflagpolecenter{%
  \formationcenterx=\formationx
  \formationcentery=\formationy
  \flagpolesymmetry=-1
  \def\dancertempmacro{}\ifx\dancernumericlabel\dancertempmacro \flagpolesymmetry=0\fi
  \def\dancertempmacro{2}\ifx\dancernumericlabel\dancertempmacro \flagpolesymmetry=2\fi
  \def\dancertempmacro{4}\ifx\dancernumericlabel\dancertempmacro \flagpolesymmetry=4\fi
  \def\dancertempmacro{6}\ifx\dancernumericlabel\dancertempmacro \flagpolesymmetry=6\fi
  \def\dancertempmacro{8}\ifx\dancernumericlabel\dancertempmacro \flagpolesymmetry=8\fi
  \ifnum \flagpolesymmetry < 0 \errmessage{Bad numeric label on flagpole marker}\fi}

\newcommand\dancerbullet{{\unitlength=0.6\dancersize
  \begin{picture}(0,0)\linethickness{\dancerrulethickness}%
  \put(0,0){\circle*{1}}\end{picture}}}

\newcommand\dancerboxlabelX{{\unitlength=\dancersize
   \begin{picture}(0,0)\dancertempdimen=0.5pt
     \advance\dancertempdimen by \dancerrulethickness
     \advance\dancertempdimen by \dancerrulethickness
     \divide\dancertempdimen by 3
     \linethickness{\dancertempdimen}%
   \ifdancertilted
     \put(-0.678,0){\line(1,0){1.357}}   %  1.357 = 1.414 * 0.96
     \put(0,-0.678){\line(0,1){1.357}}
   \else
     \put(-0.48,-0.48){\line(1,1){0.96}}
     \put(-0.48,0.48){\line(1,-1){0.96}}
   \fi\end{picture}}}

\newcommand\dancerboxlabelslash{{\unitlength=\dancersize
   \begin{picture}(0,0)\dancertempdimen=0.5pt
     \advance\dancertempdimen by \dancerrulethickness
     \advance\dancertempdimen by \dancerrulethickness
     \divide\dancertempdimen by 3
     \linethickness{\dancertempdimen}%
   \ifdancertilted
     \put(-0.678,0){\line(1,0){1.357}}   %  1.357 = 1.414 * 0.96
   \else
     \put(-0.48,-0.48){\line(1,1){0.96}}
   \fi\end{picture}}}

\newcommand\dancerboxlabelbox{{\unitlength=\dancersize
   \begin{picture}(0,0)\dancertempdimen=0.5pt
     \advance\dancertempdimen by \dancerrulethickness
     \advance\dancertempdimen by \dancerrulethickness
     \divide\dancertempdimen by 3
     \linethickness{\dancertempdimen}%
   \ifdancertilted
     \put(-0.281,0){\line(1,1){0.281}}
     \put(-0.281,0){\line(1,-1){0.281}}
     \put(0.281,0){\line(-1,1){0.281}}
     \put(0.281,0){\line(-1,-1){0.281}}
   \else
     \put(-0.22,-0.2){\line(1,0){0.44}}
     \put(-0.2,-0.22){\line(0,1){0.44}}
     \put(0.22,0.2){\line(-1,0){0.44}}
     \put(0.2,0.22){\line(0,-1){0.44}}
   \fi\end{picture}}}

\newcommand\dancercirclelabelX{{\unitlength=\dancersize
   \begin{picture}(0,0)\dancertempdimen=0.5pt
     \advance\dancertempdimen by \dancerrulethickness
     \advance\dancertempdimen by \dancerrulethickness
     \divide\dancertempdimen by 3
     \linethickness{\dancertempdimen}%
   \ifdancertilted
     \put(-0.5,0){\line(1,0){1}}
     \put(0,-0.5){\line(0,1){1}}
   \else
     \put(-0.3535,-0.3535){\line(1,1){0.707}}
     \put(-0.3535,0.3535){\line(1,-1){0.707}}
   \fi\end{picture}}}

\newcommand\dancercirclelabelslash{{\unitlength=\dancersize
   \begin{picture}(0,0)\dancertempdimen=0.5pt
     \advance\dancertempdimen by \dancerrulethickness
     \advance\dancertempdimen by \dancerrulethickness
     \divide\dancertempdimen by 3
     \linethickness{\dancertempdimen}%
   \ifdancertilted
     \put(-0.5,0){\line(1,0){1}}
   \else
     \put(-0.3535,-0.3535){\line(1,1){0.707}}
   \fi\end{picture}}}

\newcommand\dancercirclelabelbox{\dancerboxlabelbox}

\newcommand\LTSQ@dancerspot{{\multiply\dancersize by 3\divide\dancersize by 7
  \hbox to 0pt{\hss\vbox to 0pt{\vss\hrule width \dancersize height \dancerrulethickness\vss}\hss}%
  \hbox to 0pt{\hss\vbox to 0pt{\vss\hrule width \dancerrulethickness height \dancersize\vss}\hss}}}

% In the most general case, the \begin{Formation} ... \end{Formation}
% construction expands into a vertical sequence of alignments.
% Each alignment is a sequence of rows; each row may be followed
% followed by a big (full-width) caption, or may be automatically
% ended by the \columns counter.
% Each row is a sequence of diagrams (and their associated captions).
% Each diagram is a sequence of overlays followed by a caption.
% Each overlay is a sequence of lines separated by \overlay.
% Each line is a sequence of dancers ended by \\.
% Each dancer is a sequence of dancer specifiers ended by a space.
%
% However, a wrinkle is that we don't actually start off the whole
% alignment business.  Instead, we start a single diagram.  If, and
% only if, a caption is seen, or the \columns feature is used,
% then the rest of hierarchy is started after the fact, so to speak.
%
% When \numberofcolumns is positive, the alignment is left open
% so as to span multiple rows; otherwise, the alignment is closed
% off at the end of each row and a new alignment started for the next.

\newenvironment{Formation}{%
  \ifquantizedancersize
    \advance\dancersize by 65535sp
    \divide\dancersize by 65536    \multiply\dancersize by 65536
  \fi
  \dancermatrixunit=\dancerspotsep\dancersize
  \ifquantizedancersep
    \advance\dancermatrixunit by 65535sp
    \divide\dancermatrixunit by 65536    \multiply\dancermatrixunit by 65536
  \fi
  \divide\dancermatrixunit by \dancerspotdenom
  \numberofcolumns=0\startrow
  \startdiagram\startoverlay\startline}{\finishline\finishoverlay\finishdiagram{}\ifaligning
  \ifnum \numberofcolumns>0
    \def\tempc{\finishrow{}\finishalignment}%
  \else
    \ifnonemptyrow
      \def\tempc{\finishrow{}\finishalignment}%
    \else
      \def\tempc{}%
    \fi
  \fi
  \tempc
  \addvspace{\FormationSep}\else
  \box\rowdiagramsbox\fi}

\newcommand\processformation[1]{\ifcat\noexpand#1 \def\tempc{\processformation}\else
  \ifcat\noexpand#1\noexpand\par \def\tempc{\processcontrol#1}\else\def\tempc{\glomdancer#1}\fi\fi\tempc}

\newcommand\processcontrol[1]{\def\tempa{#1}\relax
   \def\tempb{\\}\ifx\tempa\tempb
     \def\tempc{\processnewline}\else
   \def\tempb{\overlay}\ifx\tempa\tempb
     \def\tempc{\processoverlay}\else
   \def\tempb{\caption}\ifx\tempa\tempb
     \def\tempc{\processcaption}\else
   \def\tempb{\bigcaption}\ifx\tempa\tempb
     \def\tempc{\processbigcaption}\else
   \def\tempb{\columns}\ifx\tempa\tempb
     \def\tempc{\processcolumns}\else
   \def\tempb{\end}\ifx\tempa\tempb
     \def\tempc{\end}\else
   \errmessage{Unrecognized control sequence \noexpand#1 in square dance formation}\relax
   \def\tempc{\processformation}\fi\fi\fi\fi\fi\fi\tempc}

\newcommand\glomdancer{}\def\glomdancer#1 {\startdancer\processdancer#1\;}  %Note space after "#1"

\newcommand\processdancer[1]{\def\tempa{#1}\def\tempb{\;}\ifx\tempa\tempb
    \def\tempc{\finishdancer\processformation}\else
  \ifcat\noexpand#1\noexpand\par \def\tempc{\controlfixup{#1}}\else
  \def\tempc{\csname subprocessformation#1\endcsname}\fi\fi\tempc}

\newcommand\controlfixup{}\def\controlfixup#1#2\;{\processcontrol#1#2 }  % Remove the \; and put the space back

\newif\ifdanceradjustup
\newif\ifdanceradjustdown
\newif\ifdanceradjustleft
\newif\ifdanceradjustright

\newcommand\startdancer{\maybestartnewrow
  \let\dancerstyle=\dancerbox \dancerseprulecount=0 \let\dancerseprulestyle=\relax \let\dancersepruledefaultshape=\relax
  \setbox\dancerlabelbox=\hbox{}\def\dancernumericlabel{}\relax
  \def\dancernose{}\def\dancerlabelcolor{}\def\dancerseprulecolorid{}\dancertiltedfalse
  \danceradjustupfalse\danceradjustdownfalse\danceradjustleftfalse\danceradjustrightfalse
  \dancerspotfalse\dancercountstrue\dancerseprulefalse
  \dancerwarpfalse\dancerflagpolefalse\flagpolenose=0
  \possibleatsignsymmetry=0
  \dancerlabelisXfalse\dancerlabelisslashfalse\dancerlabelisboxfalse}

\newcommand\finishdancer{\relax
  \nonemptylinetrue
  \nonemptyoverlaytrue
  \ifdancerspot
   \edef\tempx{\the\formationx}\dancertempcounta=\formationx
    \ifdanceradjustleft
      \ifnum \formationx > 0
          \advance\dancertempcounta by -1 \edef\tempx{\the\dancertempcounta.5}\else
          \ifnum \formationx = 0 \edef\tempx{-0.5}\else
          \edef\tempx{\the\formationx.5}\fi\fi
    \fi
    \ifdanceradjustright
      \ifnum \formationx < -1
          \advance\dancertempcounta by 1 \edef\tempx{\the\dancertempcounta.5}\else
          \ifnum \formationx = -1 \edef\tempx{-0.5}\else
          \edef\tempx{\the\formationx.5}\fi\fi
    \fi
    \edef\tempy{\the\formationy}\dancertempcounta=\formationy
    \ifdanceradjustdown
      \ifnum \formationy > 0
          \advance\dancertempcounta by -1 \edef\tempy{\the\dancertempcounta.5}\else
          \ifnum \formationy = 0 \edef\tempy{-0.5}\else
          \edef\tempy{\the\formationy.5}\fi\fi
    \fi
    \ifdanceradjustup
      \ifnum \formationy < -1
          \advance\dancertempcounta by 1 \edef\tempy{\the\dancertempcounta.5}\else
          \ifnum \formationy = -1 \edef\tempy{-0.5}\else
          \edef\tempy{\the\formationy.5}\fi\fi
    \fi
    \edef\tempa{(\tempx,\tempy)}\relax
    \ifdancerseprule
      {\csname dancerseprulecolor\dancerseprulecolorid\endcsname
       \expandafter\put\tempa{\hbox to 0pt{\csname dancer\dancerNose Seprule\endcsname\hskip -0.5\dancermatrixunit\hss}}}\relax
    \fi
    \ifdancerseprule\else\expandafter\put\tempa{\csname dancer\dancernose Nose\endcsname}\fi
    \expandafter\put\tempa{\dancertiltedfalse\csname dancer\dancernose Tilt\endcsname\dancerstyle}
    \expandafter\put\tempa{\vbox to 0pt{\vss\hbox to 0pt{\hss
        \dancerlabelcolor\box\dancerlabelbox\packagedancerlabel{\hbox{\dancernumericlabel}}\hss}\vss}}
    \ifdancercounts \dancercalculatebounds \fi
    \advance\formationx by 1
  \fi}

\newcommand\maybestartnewrow{\def\tempc{}\ifaligning\ifnonemptyrow
    \ifnum \numberofcolumns>0
      \ifnum \diagramsinrow>\numberofcolumns
        \def\tempc{\finishrow{}\restorealignstate\startrow}\fi\fi\fi\fi
  \tempc}

\newcommand\processnewline{\finishline\advance\formationy by -1\startline}

\newcommand\startline{\formationx=0\nonemptylinefalse\processformation}

\newcommand\finishline{\ifnum \formationxsize<\formationx \formationxsize=\formationx \fi}

\newcommand\processoverlay{\finishline\finishoverlay\startoverlay\startline}

\newcommand\startoverlay{\nonemptyoverlayfalse
  \formationy=0
  \formationcenterx=-1 \formationcentery=-1      %These get assigned by @ or F
  \formationxsize=1  % Yes: 1 is the minimum xsize, not 0
  \def\dancerformationrotation{0}\def\dancersymmetryrotation{0}%
  \def\dancerwarprotation{0}\formationrotatedfalse
  % We use -7200pt as a "negative infinity" and 7200pt as a "positive infinity"
  % so that their difference (that is, 14400) will be less than 2^16 and therefore
  % will be representable (7200pt = 100 inches, which should be plenty big).
  \formationmaxx=-7200pt \formationminx=7200pt
  \formationmaxy=-7200pt \formationminy=7200pt
  \atsignsymmetry=0        % This can be updated by the label of @ or F
  \atsignnose=0            % This can be updated by the nose of @ or F
  \setbox\formationbox=\vbox\bgroup\unitlength=\dancermatrixunit\noindent
     \begin{picture}(0,0)}

\newcommand\finishoverlay{\ifnum \formationx=0 \advance\formationy by 1 \fi
       \formationy=-\formationy
       \advance\formationmaxx by -\formationminx
       \advance\formationmaxy by -\formationminy
       % Lift a bunch of registers out of this group ...
       \global\dancertempcounta=\formationxsize
       \global\dancertempcountb=\formationy
       \global\dancertempcountc=\formationmaxx
       \global\dancertempcountd=\formationmaxy
       \global\let\dancertempmacro=\dancerformationrotation
       \global\dancertempcounte=0 \ifformationrotated\global\dancertempcounte=1\fi
       \global\dancertempcountf=0 \ifnonemptyoverlay\global\dancertempcountf=1\fi
     \end{picture}\egroup  %(This \egroup ends the \vbox around the picture.)
  % .. and rebind the registers in this group.
  \formationxsize=\dancertempcounta
  \formationy=\dancertempcountb
  \formationmaxx=\dancertempcountc sp
  \formationmaxy=\dancertempcountd sp
  \let\dancerformationrotation=\dancertempmacro
  \formationrotatedfalse \ifnum \dancertempcounte>0 \formationrotatedtrue \fi
  \nonemptyoverlayfalse \ifnum \dancertempcountf>0 \nonemptyoverlaytrue \fi
  % End of rebinding
  \ifnonemptyoverlay
    \nonemptydiagramtrue
    % Add necessary outside margin.  Using \sqrt{2} \dancersize rather than
    % just \dancersize allows for corners of tilted dancers and for noses.
    \advance\formationmaxx by 1.41421\dancersize
    \advance\formationmaxy by 1.41421\dancersize
    \advance\formationxsize by -1
    \setbox\formationbox=\vbox to 0pt{\formationtempdimen=\formationy\dancermatrixunit
      \vskip -0.5\formationtempdimen
      \hbox to 0pt{\formationtempdimen=\formationxsize\dancermatrixunit
                   \hskip -0.5\formationtempdimen
                   \box\formationbox\hss}\vss}\relax
    \setbox\formationbox=\vbox to \formationmaxy{\vss\hbox to \formationmaxx{\hss
      \ifformationrotated
        \rotatebox{\dancerformationrotation}{\box\formationbox}\relax
      \else
        \box\formationbox
      \fi
      \hss}\vss}\relax
    % Now \box\formationbox has the new overlay, and we need to add it
    % to \box\diagrambox, aligning their centers and taking the max over
    % their width and height.
    \ifdim \formationmaxx < \wd\diagrambox \formationmaxx=\wd\diagrambox \fi
    \ifdim \formationmaxy < \ht\diagrambox \formationmaxy=\ht\diagrambox \fi
    \setbox\diagrambox=\vbox to \formationmaxy{\vss\hbox to \formationmaxx{\hss
        \vbox to 0pt{\vss\hbox to 0pt{\hss\box\diagrambox\hss}\vss}\relax
        \vbox to 0pt{\vss\hbox to 0pt{\hss\box\formationbox\hss}\vss}\hss}\vss}\relax
    % The following is a temporary hack until we get @-processing working properly.
    \formationtempdimen=\ht\diagrambox
    \divide\formationtempdimen by 2
    \ht\diagrambox=\formationtempdimen
    \dp\diagrambox=\formationtempdimen
  \fi}

\newtoks\preamblematerial
\newtoks\dancertemptoks
\newif\ifaligning
\newif\ifnonemptycaption
\newif\ifnonemptyline
\newif\ifnonemptyoverlay
\newif\ifnonemptydiagram
\newif\ifnonemptyrow

\newcommand\processcaption[1]{\diagrambreak{#1}{}}

% First argument is caption material;
% second argument is commands to put between diagrams.
\newcommand\diagrambreak[2]{\finishline\finishoverlay
  \finishdiagram{#1}\ifaligning\else\addvspace{\FormationSep}\fi\aligningtrue
  #2\startdiagram\startoverlay\startline}

\newcommand\startdiagram{\setbox\diagrambox=\hbox{}\nonemptydiagramfalse}

% The argument to \finishdiagram is the caption text;
% the matching diagram is in \box\diagrambox.
% Note that the \rowdiagramsbox and \rowcaptionsbox are kept in
% reverse order so that, later on, \lastbox can be used to extract
% the subboxes in the correct order.

\newcommand\finishdiagram[1]{\relax
  \def\tempa{#1}\def\tempb{}\ifx\tempa\tempb \else \nonemptycaptiontrue \nonemptydiagramtrue \fi
  \ifnonemptyrow
    \ifnonemptydiagram
      \advance\diagramsinrow by 1
      \expandafter\preamblematerial\expandafter=\expandafter{\the\preamblematerial&\hfil##\hfil}\relax
      \setbox\rowdiagramsbox=\hbox{\box\diagrambox\unhbox\rowdiagramsbox}\relax
      \setbox\rowcaptionsbox=\hbox{\hbox{\formatcaption{#1}}\unhbox\rowcaptionsbox}\relax
    \fi
  \else
    \ifnonemptydiagram \nonemptyrowtrue \fi
    \ifnonemptycaption \nonemptyrowtrue \fi
    \diagramsinrow=1
    \preamblematerial={\hfil##\hfil}\relax
    \setbox\rowdiagramsbox=\hbox{\box\diagrambox}\relax
    \setbox\rowcaptionsbox=\hbox{\hbox{\formatcaption{#1}}}\relax
  \fi}

\newcommand\formatcaption[1]{\def\tempa{#1}\def\tempb{}\ifx\tempa\tempb \else
  \mbox{\begin{tabular}[c]{@{}c@{}}#1\end{tabular}}\fi}

\newcommand\processcolumns[1]{\diagrambreak{}{%
  \ifnonemptyrow
    \def\tempc{\finishrow{}\finishalignment\restorealignstate}%
  \else
    \def\tempc{}%
  \fi
  \tempc
  \numberofcolumns=#1\relax
  \startalignment{\numberofcolumns}\startrow}}

\newcommand\startrow{\nonemptycaptionfalse\nonemptyrowfalse}

% The caller of \finishrow may need to call \restorealignstate.
\newcommand\finishrow[1]{%
  \ifnonemptyrow
    \ifnum \numberofcolumns>0 \def\tempk{}\else
      \def\tempk{\startalignment{\diagramsinrow}}\fi
    \ifnonemptycaption
      \def\tempc{\tempk
                 \dumpalignboxes{\rowdiagramsbox}\noalign{\nobreak\vskip\SquareDanceCaptionSep}\restorealignstate
                 \dumpalignboxes{\rowcaptionsbox}%
                 \noalign{\formatbigcaption{#1}\vskip\SquareDanceDiagramSep}}%
    \else
      \def\tempc{\tempk
                 \dumpalignboxes{\rowdiagramsbox}%
                 \noalign{\formatbigcaption{#1}\vskip\SquareDanceDiagramSep}}%
    \fi
  \else
    \def\tempc{}%
  \fi
  \tempc}

\newcommand\formatbigcaption[1]{%
  \def\tempa{#1}\def\tempb{}\ifx\tempa\tempb \else
    \vskip\SquareDanceBigCaptionSep
    \hbox to \hsize{\hskip\@totalleftmargin\hss\begin{tabular}[c]{@{}c@{}}#1\end{tabular}\hss}\relax
  \fi}

% The argument is the box of boxes to dump out.
% The last thing this does is the \cr to end the alignment row.
% Just before this \cr, the state to be preserved is saved;
% it is up to the caller to do the restore at the right place.
\newcommand\dumpalignboxes[1]{\dancertempcountz=0
  \dumponebox{#1}\dumpmoreboxes{#1}\savealignstate\cr}

\newcommand\dumponebox[1]{\advance\dancertempcountz by 1
  \setbox#1=\hbox{\unhbox#1\global\setbox\formationtempboxa=\lastbox}\relax
  \box\formationtempboxa}

\newcommand\dumpmoreboxes[1]{\ifnum \dancertempcountz=\diagramsinrow
  \def\tempc{}\else
  \def\tempc{\savealignstate&\restorealignstate\dumponebox{#1}\dumpmoreboxes{#1}}\fi\tempc}

% Save and restore state that must be preserved across an alignment
% tab or \cr, for example, despite the fact that alignment entries
% are TeX groups:
%    \numberofcolumns
%    \diagramsinrow
%    \dancertempcountz
%    \box\rowdiagramsbox
%    \box\rowcaptionsbox
% Note that \ifaligning must be true, so just set it true again.
% Normally, good style would be to code this as a single macro taking
% an argument that says what to do between the save and restore, so as
% to guarantee that saves and restores are properly balanced.
% However, TeX is EXTREMELY fussy about what appears immediately
% after a \cr, especially between the last \cr and the end of the
% alignment.  Breaking it up into two macros is the easiest way out.

\newcommand\savealignstate{\relax
  \global\dancertempcountc=\numberofcolumns
  \global\dancertempcountd=\diagramsinrow
  \global\dancertempcounte=\dancertempcountz
  %\expandafter\global\expandafter\dancertemptoks\expandafter=\expandafter{\the\preamblematerial}%
  \global\setbox\formationtempboxa=\box\rowdiagramsbox
  \global\setbox\formationtempboxb=\box\rowcaptionsbox}

\newcommand\restorealignstate{\relax
  \numberofcolumns=\dancertempcountc
  \diagramsinrow=\dancertempcountd
  \dancertempcountz=\dancertempcounte
  %\expandafter\preamblematerial\expandafter=\expandafter{\the\dancertemptoks}%
  \setbox\rowdiagramsbox=\box\formationtempboxa
  \setbox\rowcaptionsbox=\box\formationtempboxb
  \aligningtrue  % This we know
  }

\newcommand\processbigcaption[1]{\finishline\finishoverlay\finishdiagram{}\relax
  \ifnum \numberofcolumns>0 \def\tempc{\finishrow{#1}\restorealignstate}\else
    \def\tempc{\finishrow{#1}\finishalignment\restorealignstate}\fi
  \tempc\startrow\startdiagram\startoverlay\startline}


% Argument is the number of diagrams per row (for computing skips).
\newcommand\startalignment[1]{\aligningtrue
  \edef\tempa{\tabskip 0pt plus 1fil}\edef\tempb{\tabskip 0pt plus 1fil}%
  \ifnum #1>1
    \edef\tempa{\tabskip\csname SquareDanceOuterSkip\romannumeral#1\endcsname}%
    \edef\tempb{\tabskip\csname SquareDanceInnerSkip\romannumeral#1\endcsname}%
  \fi
  \setbox\formationtempboxa\vbox\bgroup
    \tempa\advance\tabskip by \@totalleftmargin
    \halign to\hsize\bgroup\span\tempb\span\the\preamblematerial\span\tempa\cr}

\newcommand\finishalignment{\egroup\egroup
  %\showbox \formationtempboxa
  \unvbox\formationtempboxa}

% When calculating a bounding box for a rotated formation,
% ideally we would use the sine and cosine of the exact
% rotation angle.  But computing an exact sine and cosine in TeX
% is a pain, and anyway the only angles we really care about
% are multiples of $\pi/8$ (which is 22.5 degrees).  But using
% just those angles is a bit too coarse.  Our compromise is to
% figure out which multiple of $\pi/24$ (rather than $\pi/8$)
% is closest to the specified angle and use that.  This has the
% additional advantage of supporting multiples of 30 degrees,
% so one can make (somewhat crude) diagrams for hexagons
% and still get perfect bounding boxes. For angles that are not
% exact multiples of $\pi/24$, the width or height of the
% bounding box may be a bit too small; we can live with that.

\newcommand\dancercalculatebounds{\formationxdimen=\formationx\dancermatrixunit
  \formationydimen=\formationy\dancermatrixunit
  \ifdanceradjustleft \advance\formationxdimen by -0.5\dancermatrixunit \fi
  \ifdanceradjustright \advance\formationxdimen by 0.5\dancermatrixunit \fi
  \ifdanceradjustdown \advance\formationydimen by -0.5\dancermatrixunit \fi
  \ifdanceradjustup \advance\formationydimen by 0.5\dancermatrixunit \fi
  \ifformationrotated
    \formationtempdimen=\dancerformationrotation pt
    \dancertempcounta=\formationtempdimen
    \divide \dancertempcounta by 16384   % Work in quarter-degrees (1440 to a full circle)
    \advance \dancertempcounta by 15     % Add offset (3.75 degrees) for rounding
    \divide \dancertempcounta by 30      % Now work in 48ths of a circle
    % And we want to know which 48th of a circle, mod 24
    \dancertempcountc=\dancertempcounta
    \divide \dancertempcountc by 24
    \multiply \dancertempcountc by 24
    \advance \dancertempcounta by -\dancertempcountc
    \ifnum \dancertempcounta <0 \advance \dancertempcounta by 24\fi
  \else
    \dancertempcounta=0
  \fi
  \dancertempcountb=\dancertempcounta
  \divide\dancertempcountb by 6
  % Now \dancertempcountb says which 1/8 of the circle we are in;
  % we can use symmetries to calculate the sine and cosine.
  \ifcase \dancertempcountb
    % 0 degrees <= angle < 45 degrees: do nothing
  \or
    % 45 degrees <= angle < 90 degrees
    \dancertempcounta=-\dancertempcounta
    \advance\dancertempcounta by 12
  \or
    % 90 degrees <= angle < 135 degrees
    \advance\dancertempcounta by -12
  \or
    % 135 degrees <= angle < 180 degrees
    \dancertempcounta=-\dancertempcounta
    \advance\dancertempcounta by 24
  \fi
  % Here is the actual table of sines and cosines.
  \ifcase \dancertempcounta
    \def\COS{1}\def\SIN{0}\relax                %   0   degrees
  \or
    \def\COS{0.991444}\def\SIN{0.130526}\relax  %   7.5 degrees
  \or
    \def\COS{0.965925}\def\SIN{0.258819}\relax  %  15   degrees
  \or
    \def\COS{0.923879}\def\SIN{0.382683}\relax  %  22.5 degrees
  \or
    \def\COS{0.866025}\def\SIN{0.500000}\relax  %  30   degrees
  \or
    \def\COS{0.793353}\def\SIN{0.608761}\relax  %  37.5 degrees
  \or
    \def\COS{0.707106}\def\SIN{0.707106}\relax  %  45   degrees
  \fi
  \ifcase \dancertempcountb
    % 0 degrees <= angle < 45 degrees: do nothing
  \or
    % 45 degrees <= angle < 90 degrees
    \edef\tempa{\COS}\edef\COS{\SIN}\edef\SIN{\tempa}\relax
  \or
    % 90 degrees <= angle < 135 degrees
    \edef\tempa{\COS}\edef\COS{-\SIN}\edef\SIN{\tempa}\relax
  \or
    % 135 degrees <= angle < 180 degrees
    \edef\COS{-\COS}\relax
  \fi
  % Rotate the vector (\formationxdimen, \formationydimen).
  \formationtempdimen=\SIN\formationxdimen
  \advance\formationtempdimen by \COS\formationydimen
  \formationxdimen=\COS\formationxdimen
  \advance\formationxdimen by -\SIN\formationydimen
  \formationydimen=\formationtempdimen
  % Use the rotated vector to adjust the min and max bounds.
  \ifdim \formationminx > \formationxdimen \formationminx=\formationxdimen \fi
  \ifdim \formationmaxx < \formationxdimen \formationmaxx=\formationxdimen \fi
  \ifdim \formationminy > \formationydimen \formationminy=\formationydimen \fi
  \ifdim \formationmaxy < \formationydimen \formationmaxy=\formationydimen \fi}

\newcommand\packagedancerlabel[1]{{\setbox\formationtempboxa=\vbox to 0pt{\vss
      \hbox to 0pt{\hss\ignorespaces#1\unskip\hss}\vss}\relax
  \ifformationrotated
    \rotatebox{-\dancerformationrotation}{\box\formationtempboxa}\relax
  \else \box\formationtempboxa \fi}}

\newcount\dancerarctanY
\newcount\dancerarctanX
\newcount\dancerarctanresult

% In order to rotate dancers correctly for a hexagon, bigon, or octagon,
% we need to be able to calculate the angle formed at flagpole center by
% the X-axis and a line from the dancer to flagpole center.  Since we have
% X and Y coordinates for the dancer, this amounts to calculating an arctangent.
% First argument is y, second argument is x; the result, measured in hundredths
% of a degree and lying in the range -9000 <= result <= 9000, is placed in the
% counter \dancerarctanresult.
\newcommand\dancerarctan[2]{\dancerarctanY=#1\dancerarctanX=#2\dancercalculatearctan}

\newcommand\dancercalculatearctan{\relax
  \ifnum \dancerarctanY < 0
    \ifnum \dancerarctanX < 0
      \dancerarctanY=-\dancerarctanY
      \dancerarctanX=-\dancerarctanX
    \else
      \dancerarctanY=-\dancerarctanY
    \fi
  \else
    \ifnum \dancerarctanx < 0
      \dancerarctanX=-\dancerarctanX
    \else
    \fi
  \fi
  \ifnum \dancerarctanY > \dancerarctanX
    \dancertempcounta=\dancerarctanY
    \dancerarctanY=\dancerarctanX
    \dancerarctanX=\dancertempcounta
    \dancerunsignedarctan
    \dancerarctanresult=-\dancerarctanresult
    \advance\dancerarctanresult by 9000
  \else
    \dancerunsignedarctan
  \fi}

%0 <= \dancerarctanY <= \dancerarctanX
%If X is greater than 32, we use an approximation:
% if Y is 0 or 1, set X to 32; else set X:=(X+1)/2 and Y:=(Y+1)/2.  Try again.
%If Y=X, then the answer is 45 degrees.
%Otherwise, if both even but not both zero, we divide both by 2, which is exact; try again.
\newcommand\dancerunsignedarctan{\relax
  \ifnum \dancerarctanX > 32
    \ifnum \dancerarctanY < 2
      \dancerarctanX=32
    \else
      \advance \dancerarctanX by 1
      \advance \dancerarctanY by 1
      \divide \dancerarctanX by 2
      \divide \dancerarctanY by 2
      \dancerunsignedarctan
    \fi
  \else
    \ifnum \dancerarctanY = 0
      \dancerarctanresult=0
    \else
      \ifnum \dancerarctanY = \dancerarctanX
        \dancerarctanresult=4500
      \else
        \dancerarctanresult=\dancerarctanlookup\relax  % The \relax will detect errors in the lookup
      \fi
    \fi
  \fi}

%0 < \dancerarctanY < \dancerarctanX <= 32
% and at least one of \dancerarctanY and \dancerarctanX is odd.
\newcommand\dancerarctanlookup{\ifcase\dancerarctanX
  \or\or
  \dancerarctanlookupII\or
  \dancerarctanlookupIII\or
  \dancerarctanlookupIV\or
  \dancerarctanlookupV\or
  \dancerarctanlookupVI\or
  \dancerarctanlookupVII\or
  \dancerarctanlookupVIII\or
  \dancerarctanlookupIX\or
  \dancerarctanlookupX\or
  \dancerarctanlookupXI\or
  \dancerarctanlookupXII\or
  \dancerarctanlookupXIII\or
  \dancerarctanlookupXIV\or
  \dancerarctanlookupXV\or
  \dancerarctanlookupXVI\or
  \dancerarctanlookupXVII\or
  \dancerarctanlookupXVIII\or
  \dancerarctanlookupXIX\or
  \dancerarctanlookupXX\or
  \dancerarctanlookupXXI\or
  \dancerarctanlookupXXII\or
  \dancerarctanlookupXXIII\or
  \dancerarctanlookupXXIV\or
  \dancerarctanlookupXXV\or
  \dancerarctanlookupXXVI\or
  \dancerarctanlookupXXVII\or
  \dancerarctanlookupXXVIII\or
  \dancerarctanlookupXXIX\or
  \dancerarctanlookupXXX\or
  \dancerarctanlookupXXXI\or
  \dancerarctanlookupXXXII\fi}

% Emacs Lisp code for generating the arctangent tables:
%
% (dotimes (xmtwo 31) (let ((x (+ xmtwo 2)))
%   (princ "\\newcommand\\dancerarctanlookup")
%   (princ (substring "XXX" 0 (/ x 10)))
%   (cond ((= (mod x 10) 9) (princ "IX"))
%         ((= (mod x 10) 4) (princ "IV"))
%         (t (when (>= (mod x 10) 5) (princ "V"))
%            (princ (substring "III" 0 (mod x 5)))))
%   (princ "{\\ifcase\\dancerarctanY")
%   (dotimes (ymone (- x 1))
%     (let ((y (+ ymone 1)))
%        (when (and (> x 5) (= (mod y 12) 1)) (princ "\n  "))
%        (princ (format "\\or%d"
%                       (floor (+ 0.5 (* 100 (/ 180 3.1415926535897932384)
%                                        (atan (float y) (float x)))))))))
%   (princ "\\fi}\n")))

\newcommand\dancerarctanlookupII{\ifcase\dancerarctanY\or2657\fi}
\newcommand\dancerarctanlookupIII{\ifcase\dancerarctanY\or1843\or3369\fi}
\newcommand\dancerarctanlookupIV{\ifcase\dancerarctanY\or1404\or2657\or3687\fi}
\newcommand\dancerarctanlookupV{\ifcase\dancerarctanY\or1131\or2180\or3096\or3866\fi}
\newcommand\dancerarctanlookupVI{\ifcase\dancerarctanY
  \or946\or1843\or2657\or3369\or3981\fi}
\newcommand\dancerarctanlookupVII{\ifcase\dancerarctanY
  \or813\or1595\or2320\or2974\or3554\or4060\fi}
\newcommand\dancerarctanlookupVIII{\ifcase\dancerarctanY
  \or713\or1404\or2056\or2657\or3201\or3687\or4119\fi}
\newcommand\dancerarctanlookupIX{\ifcase\dancerarctanY
  \or634\or1253\or1843\or2396\or2905\or3369\or3787\or4163\fi}
\newcommand\dancerarctanlookupX{\ifcase\dancerarctanY
  \or571\or1131\or1670\or2180\or2657\or3096\or3499\or3866\or4199\fi}
\newcommand\dancerarctanlookupXI{\ifcase\dancerarctanY
  \or519\or1030\or1526\or1998\or2444\or2861\or3247\or3603\or3929\or4227\fi}
\newcommand\dancerarctanlookupXII{\ifcase\dancerarctanY
  \or476\or946\or1404\or1843\or2262\or2657\or3026\or3369\or3687\or3981\or4251\fi}
\newcommand\dancerarctanlookupXIII{\ifcase\dancerarctanY
  \or440\or875\or1299\or1710\or2104\or2478\or2830\or3161\or3470\or3757\or4024\or4271\fi}
\newcommand\dancerarctanlookupXIV{\ifcase\dancerarctanY
  \or409\or813\or1209\or1595\or1965\or2320\or2657\or2974\or3274\or3554\or3816\or4060
  \or4288\fi}
\newcommand\dancerarctanlookupXV{\ifcase\dancerarctanY
  \or381\or759\or1131\or1493\or1843\or2180\or2502\or2807\or3096\or3369\or3625\or3866
  \or4091\or4303\fi}
\newcommand\dancerarctanlookupXVI{\ifcase\dancerarctanY
  \or358\or713\or1062\or1404\or1735\or2056\or2363\or2657\or2936\or3201\or3451\or3687
  \or3909\or4119\or4315\fi}
\newcommand\dancerarctanlookupXVII{\ifcase\dancerarctanY
  \or337\or671\or1001\or1324\or1639\or1944\or2238\or2520\or2790\or3047\or3291\or3522
  \or3741\or3947\or4142\or4326\fi}
\newcommand\dancerarctanlookupXVIII{\ifcase\dancerarctanY
  \or318\or634\or946\or1253\or1552\or1843\or2125\or2396\or2657\or2905\or3143\or3369
  \or3584\or3787\or3981\or4163\or4336\fi}
\newcommand\dancerarctanlookupXIX{\ifcase\dancerarctanY
  \or301\or601\or897\or1189\or1474\or1753\or2022\or2283\or2535\or2776\or3007\or3228
  \or3438\or3638\or3829\or4010\or4182\or4345\fi}
\newcommand\dancerarctanlookupXX{\ifcase\dancerarctanY
  \or286\or571\or853\or1131\or1404\or1670\or1929\or2180\or2423\or2657\or2881\or3096
  \or3302\or3499\or3687\or3866\or4036\or4199\or4353\fi}
\newcommand\dancerarctanlookupXXI{\ifcase\dancerarctanY
  \or273\or544\or813\or1078\or1339\or1595\or1843\or2085\or2320\or2546\or2765\or2974
  \or3176\or3369\or3554\or3730\or3899\or4060\or4214\or4360\fi}
\newcommand\dancerarctanlookupXXII{\ifcase\dancerarctanY
  \or260\or519\or777\or1030\or1280\or1526\or1765\or1998\or2225\or2444\or2657\or2861
  \or3058\or3247\or3429\or3603\or3769\or3929\or4082\or4227\or4367\fi}
\newcommand\dancerarctanlookupXXIII{\ifcase\dancerarctanY
  \or249\or497\or743\or987\or1226\or1462\or1693\or1918\or2137\or2350\or2556\or2755
  \or2948\or3133\or3311\or3482\or3647\or3805\or3956\or4101\or4240\or4373\fi}
\newcommand\dancerarctanlookupXXIV{\ifcase\dancerarctanY
  \or239\or476\or713\or946\or1177\or1404\or1626\or1843\or2056\or2262\or2462\or2657
  \or2844\or3026\or3201\or3369\or3531\or3687\or3837\or3981\or4119\or4251\or4378\fi}
\newcommand\dancerarctanlookupXXV{\ifcase\dancerarctanY
  \or229\or457\or684\or909\or1131\or1350\or1564\or1774\or1980\or2180\or2375\or2564
  \or2747\or2925\or3096\or3262\or3422\or3575\or3723\or3866\or4003\or4135\or4261\or4383\fi}
\newcommand\dancerarctanlookupXXVI{\ifcase\dancerarctanY
  \or220\or440\or658\or875\or1089\or1299\or1507\or1710\or1909\or2104\or2293\or2478
  \or2657\or2830\or2998\or3161\or3318\or3470\or3616\or3757\or3893\or4024\or4150\or4271
  \or4388\fi}
\newcommand\dancerarctanlookupXXVII{\ifcase\dancerarctanY
  \or212\or424\or634\or843\or1049\or1253\or1453\or1650\or1843\or2032\or2217\or2396
  \or2571\or2741\or2905\or3065\or3220\or3369\or3513\or3653\or3787\or3917\or4043\or4163
  \or4280\or4392\fi}
\newcommand\dancerarctanlookupXXVIII{\ifcase\dancerarctanY
  \or205\or409\or612\or813\or1012\or1209\or1404\or1595\or1782\or1965\or2145\or2320
  \or2490\or2657\or2818\or2974\or3126\or3274\or3416\or3554\or3687\or3816\or3940\or4060
  \or4176\or4288\or4396\fi}
\newcommand\dancerarctanlookupXXIX{\ifcase\dancerarctanY
  \or197\or395\or591\or785\or978\or1169\or1357\or1542\or1724\or1903\or2077\or2248
  \or2415\or2577\or2735\or2889\or3038\or3183\or3323\or3459\or3591\or3718\or3842\or3961
  \or4076\or4188\or4295\or4399\fi}
\newcommand\dancerarctanlookupXXX{\ifcase\dancerarctanY
  \or191\or381\or571\or759\or946\or1131\or1313\or1493\or1670\or1843\or2014\or2180
  \or2343\or2502\or2657\or2807\or2954\or3096\or3235\or3369\or3499\or3625\or3748\or3866
  \or3981\or4091\or4199\or4303\or4403\fi}
\newcommand\dancerarctanlookupXXXI{\ifcase\dancerarctanY
  \or185\or369\or553\or735\or916\or1095\or1272\or1447\or1619\or1788\or1954\or2116
  \or2275\or2430\or2582\or2730\or2874\or3014\or3150\or3283\or3411\or3536\or3657\or3775
  \or3888\or3999\or4105\or4209\or4309\or4406\fi}
\newcommand\dancerarctanlookupXXXII{\ifcase\dancerarctanY
  \or179\or358\or536\or713\or888\or1062\or1234\or1404\or1571\or1735\or1897\or2056
  \or2211\or2363\or2511\or2657\or2798\or2936\or3070\or3201\or3327\or3451\or3571\or3687
  \or3800\or3909\or4016\or4119\or4218\or4315\or4409\fi}

\expandafter\newcommand\csname subprocessformation0\endcsname{\ifdancerseprule
  \def\dancerseprulecolorid{0}\else
  \dancerspottrue \edef\dancernumericlabel{\dancernumericlabel 0}\fi \processdancer}
\expandafter\newcommand\csname subprocessformation1\endcsname{\ifdancerseprule
  \def\dancerseprulecolorid{1}\else
  \dancerspottrue \edef\dancernumericlabel{\dancernumericlabel 1}\fi \processdancer}
\expandafter\newcommand\csname subprocessformation2\endcsname{\ifdancerseprule
  \def\dancerseprulecolorid{2}\else
  \dancerspottrue \edef\dancernumericlabel{\dancernumericlabel 2}\fi \processdancer}
\expandafter\newcommand\csname subprocessformation3\endcsname{\ifdancerseprule
  \def\dancerseprulecolorid{3}\else
  \dancerspottrue \edef\dancernumericlabel{\dancernumericlabel 3}\fi \processdancer}
\expandafter\newcommand\csname subprocessformation4\endcsname{\ifdancerseprule
  \def\dancerseprulecolorid{4}\else
  \dancerspottrue \edef\dancernumericlabel{\dancernumericlabel 4}\fi \processdancer}
\expandafter\newcommand\csname subprocessformation5\endcsname{\ifdancerseprule
  \def\dancerseprulecolorid{5}\else
  \dancerspottrue \edef\dancernumericlabel{\dancernumericlabel 5}\fi \processdancer}
\expandafter\newcommand\csname subprocessformation6\endcsname{\ifdancerseprule
  \def\dancerseprulecolorid{6}\else
  \dancerspottrue \edef\dancernumericlabel{\dancernumericlabel 6}\fi \processdancer}
\expandafter\newcommand\csname subprocessformation7\endcsname{\ifdancerseprule
  \def\dancerseprulecolorid{7}\else
  \dancerspottrue \edef\dancernumericlabel{\dancernumericlabel 7}\fi \processdancer}
\expandafter\newcommand\csname subprocessformation8\endcsname{\ifdancerseprule
  \def\dancerseprulecolorid{8}\else
  \dancerspottrue \edef\dancernumericlabel{\dancernumericlabel 8}\fi \processdancer}
\expandafter\newcommand\csname subprocessformation9\endcsname{\ifdancerseprule
  \def\dancerseprulecolorid{9}\else
  \dancerspottrue \edef\dancernumericlabel{\dancernumericlabel 9}\fi \processdancer}

\expandafter\newcommand\csname subprocessformationd\endcsname{\dancerspottrue
  \setbox\dancerlabelbox=\hbox{\box\dancerlabelbox\packagedancerlabel{$\bullet$}}\processdancer}
\expandafter\newcommand\csname subprocessformationt\endcsname{\dancerspottrue
  \let\dancerstyle=\dancercircle
  \setbox\dancerlabelbox=\hbox{\box\dancerlabelbox\packagedancerlabel{$\bullet$}}\processdancer}
\expandafter\newcommand\csname subprocessformationx\endcsname{\dancerspottrue\dancerlabelisXtrue
  \setbox\dancerlabelbox=\hbox{\box\dancerlabelbox\packagedancerlabel{$\times$}}\processdancer}
\expandafter\newcommand\csname subprocessformationz\endcsname{\dancerspottrue\dancerlabelisslashtrue
  \setbox\dancerlabelbox=\hbox{\box\dancerlabelbox\packagedancerlabel{$/$}}\processdancer}
\expandafter\newcommand\csname subprocessformationq\endcsname{\dancerspottrue\dancerlabelisboxtrue
  \setbox\dancerlabelbox=\hbox{\box\dancerlabelbox\packagedancerlabel{\dancerboxlabelbox}}\processdancer}


\expandafter\def\csname subprocessformation[\endcsname#1]{\dancerspottrue
  \setbox\dancerlabelbox=\hbox{\box\dancerlabelbox\packagedancerlabel{#1}}\processdancer}

\expandafter\def\csname subprocessformation(\endcsname#1){\ifformationrotated
    \dancerspottrue
    \setbox\dancerlabelbox=\hbox{\box\dancerlabelbox\vbox to 0pt{\vss\hbox to 0pt{\hss#1\hss}\vss}}\relax
  \else
    \ifnum \formationx>0 \errmessage{Misplaced angle specifier (#1)}\else
    \ifnum \formationy>0 \errmessage{Misplaced angle specifier (#1)}\else
    \def\dancerformationrotation{#1}\formationrotatedtrue\fi\fi
  \fi\processdancer}

\expandafter\newcommand\csname subprocessformationb\endcsname{\dancerspottrue
  \let\dancerstyle=\dancerbox \processdancer}
\expandafter\newcommand\csname subprocessformationB\endcsname{\dancerspottrue
  \let\dancerstyle=\dancerbox \processdancer}
\expandafter\newcommand\csname subprocessformationg\endcsname{\dancerspottrue
  \let\dancerstyle=\dancercircle \processdancer}
\expandafter\newcommand\csname subprocessformationG\endcsname{\dancerspottrue
  \let\dancerstyle=\dancercircle \processdancer}
\expandafter\newcommand\csname subprocessformationy\endcsname{\dancerspottrue
  \let\dancerstyle=\dancergraybox \def\dancerlabelcolor{\color{black}}\processdancer}
\expandafter\newcommand\csname subprocessformationY\endcsname{\dancerspottrue
  \let\dancerstyle=\dancerblackbox \def\dancerlabelcolor{\color{white}}\processdancer}
\expandafter\newcommand\csname subprocessformationX\endcsname{\dancerspottrue
  \let\dancerstyle=\dancerblackcircle \def\dancerlabelcolor{\color{white}}\processdancer}
\expandafter\newcommand\csname subprocessformationr\endcsname{\dancerspottrue
  \let\dancerstyle=\dancergraycircle \def\dancerlabelcolor{\color{black}}\processdancer}
\expandafter\newcommand\csname subprocessformationp\endcsname{\dancerspottrue
  \let\dancerstyle=\dancerphantombox \processdancer}
\expandafter\newcommand\csname subprocessformationP\endcsname{\dancerspottrue
  \let\dancerstyle=\dancerphantombox \processdancer}
\expandafter\newcommand\csname subprocessformation+\endcsname{\dancerspottrue
  \let\dancerstyle=\LTSQ@dancerspot \processdancer}
\expandafter\newcommand\csname subprocessformation@\endcsname{\dancerspottrue
  \ifnum \formationcenterx < 0 \else\errmessage{Two flagpoles seen in one formation}\fi
  \ifdancerflagpole \dancerwarpfalse \else \dancerwarptrue \fi
  \dancerflagpoletrue
  \let\dancerstyle=\dancerflagpolecenter \processdancer}
\expandafter\newcommand\csname subprocessformationF\endcsname{\dancerspottrue
  \ifnum \formationcenterx < 0 \else\errmessage{Two flagpoles seen in one formation}\fi
  \ifdancerflagpole \dancerwarpfalse \else \dancerwarptrue \fi
  \dancerflagpoletrue
  \let\dancerstyle=\dancerflagpoledot \processdancer}
\expandafter\newcommand\csname subprocessformation*\endcsname{\dancerspottrue
  % A star is a flagpole only if it has a nonempty numeric label.
  \def\tempa{}\ifx\dancernumericlabel\tempa
    \let\dancerstyle=\dancerstar
  \else
    \ifnum \formationcenterx < 0 \else\errmessage{Two flagpoles seen in one formation}\fi
    \ifdancerflagpole \dancerwarpfalse \else \dancerwarptrue \fi
    \dancerflagpoletrue
    \let\dancerstyle=\dancercenterstar
  \fi
  \processdancer}
\expandafter\newcommand\csname subprocessformation_\endcsname{\dancerspottrue
  \let\dancerstyle=\relax \processdancer}
\expandafter\newcommand\csname subprocessformation.\endcsname{\dancerspottrue
  \let\dancerstyle=\relax \dancercountsfalse \processdancer}

\newcommand\flagpolecheck{\ifnum \flagpolenose = 0 \else
  \errmessage{Incorrect multiple nose for flagpole marker}\fi}
\expandafter\newcommand\csname subprocessformationn\endcsname{\dancerspottrue
  \ifdancerflagpole\flagpolecheck\flagpolenose=4\else\edef\dancernose{\dancernose North}\fi\processdancer}
\expandafter\newcommand\csname subprocessformation^\endcsname{\dancerspottrue
  \ifdancerflagpole\flagpolecheck\flagpolenose=4\else\edef\dancernose{\dancernose North}\fi\processdancer}
\expandafter\newcommand\csname subprocessformations\endcsname{\dancerspottrue
  \ifdancerflagpole\flagpolecheck\flagpolenose=1\else\edef\dancernose{\dancernose South}\fi\processdancer}
\expandafter\newcommand\csname subprocessformationV\endcsname{\dancerspottrue
  \ifdancerflagpole\flagpolecheck\flagpolenose=1\else\edef\dancernose{\dancernose South}\fi\processdancer}
\expandafter\newcommand\csname subprocessformatione\endcsname{\dancerspottrue
  \ifdancerflagpole\flagpolecheck\flagpolenose=2\else\edef\dancernose{\dancernose East}\fi\processdancer}
\expandafter\newcommand\csname subprocessformation>\endcsname{\dancerspottrue
  \ifdancerflagpole\flagpolecheck\flagpolenose=2\else\edef\dancernose{\dancernose East}\fi\processdancer}
\expandafter\newcommand\csname subprocessformationw\endcsname{\dancerspottrue
  \ifdancerflagpole\flagpolecheck\flagpolenose=8\else\edef\dancernose{\dancernose West}\fi\processdancer}
\expandafter\newcommand\csname subprocessformation<\endcsname{\dancerspottrue
  \ifdancerflagpole\flagpolecheck\flagpolenose=8\else\edef\dancernose{\dancernose West}\fi\processdancer}
\expandafter\newcommand\csname subprocessformationv\endcsname{\dancerspottrue
  \ifdancerflagpole\errmessage{Incorrect multiple nose for flagpole marker}\fi
  \edef\dancernose{\dancernose NorthSouth}\processdancer}
\expandafter\newcommand\csname subprocessformation|\endcsname{%
  \ifdancerflagpole\errmessage{Incorrect multiple nose for flagpole marker}\fi
  \ifdancerseprule
    \advance\dancerseprulecount by 1
  \else
    \ifdancerspot
      \edef\dancernose{\dancernose NorthSouth}\else
      \dancersepruletrue \dancerseprulecount=1 \dancerspottrue \let\dancerstyle=\relax
      \let\dancersepruledefaultshape=\dancerNorthSouthSeprule
    \fi
  \fi
  \processdancer}
\expandafter\newcommand\csname subprocessformationh\endcsname{\dancerspottrue
  \ifdancerflagpole\errmessage{Incorrect multiple nose for flagpole marker}\fi
  \edef\dancernose{\dancernose EastWest}\processdancer}
\expandafter\newcommand\csname subprocessformation-\endcsname{%
  \ifdancerflagpole\errmessage{Incorrect multiple nose for flagpole marker}\fi
  \ifdancerseprule
    \advance\dancerseprulecount by 1
  \else
    \ifdancerspot
      \edef\dancernose{\dancernose EastWest}\else
      \dancersepruletrue \dancerseprulecount=1 \dancerspottrue \let\dancerstyle=\relax
      \let\dancersepruledefaultshape=\dancerEastWestSeprule
    \fi
  \fi
  \processdancer}

\expandafter\newcommand\csname subprocessformationD\endcsname{\ifdancerseprule
    \advance\dancerseprulecount by 1
  \else
    \dancersepruletrue \dancerseprulecount=1 \dancerspottrue \let\dancerstyle=\relax
    \let\dancersepruledefaultshape=\dancerNWtoSESeprule
  \fi
  \processdancer}
\expandafter\newcommand\csname subprocessformationS\endcsname{\ifdancerseprule
    \advance\dancerseprulecount by 1
  \else
    \dancersepruletrue \dancerseprulecount=1 \dancerspottrue \let\dancerstyle=\relax
    \let\dancersepruledefaultshape=\dancerSWtoNESeprule
  \fi
  \processdancer}



% When in \halfdancerspots style, three characters provide 1/4-spot adjustments:
%    ` moves 1/4 spot up
%    , moves 1/4 spot down
%    ' on the left moves 1/4 spot left
%    ' on the right moves 1/4 spot right
% These conventions were chosen to use inconspicuous characters, and in such a
% way that one can adjust in any of 8 directions while using at most one character
% on each side of a dancer descriptor.

\expandafter\newcommand\csname subprocessformation`\endcsname{%
  \ifnum \dancerspotdenom = 2 \else \errmessage{Dancer adjustment should be used only with halfdancerspots}\fi
  \ifdanceradjustup \errmessage{Dancer adjusted up more than once}\fi
  \ifdanceradjustdown \errmessage{Dancer adjusted both up and down}\fi
  \danceradjustuptrue
  \processdancer}
\expandafter\newcommand\csname subprocessformation,\endcsname{%
  \ifnum \dancerspotdenom = 2 \else \errmessage{Dancer adjustment should be used only with halfdancerspots}\fi
  \ifdanceradjustup \errmessage{Dancer adjusted both up and down}\fi
  \ifdanceradjustdown \errmessage{Dancer adjusted down more than once}\fi
  \danceradjustdowntrue
  \processdancer}
\expandafter\newcommand\csname subprocessformation'\endcsname{%
  \ifnum \dancerspotdenom = 2 \else \errmessage{Dancer adjustment should be used only with halfdancerspots}\fi
  \ifdancerspot  %postcommand--adjust right
    \ifdanceradjustleft \errmessage{Dancer adjusted both left and right}\fi
    \ifdanceradjustright \errmessage{Dancer adjusted right more than once}\fi
    \danceradjustrighttrue
  \else          %precommand--adjust left
    \ifdanceradjustleft \errmessage{Dancer adjusted left more than once}\fi
    \ifdanceradjustright \errmessage{Internal error in square dance macro for |}\fi
    \danceradjustlefttrue
  \fi\processdancer}

\newcommand\dancernoseglyph{\iffontdancers \fontdancernose \else \circle*{\dancernosesize}\fi}

\newcommand\fontdancernose{\hbox{\vbox to 0pt{\vss\hbox to 0pt{\hss\tiny\FilledSmallCircle\hss}\vss}}}

%No nose
\newcommand\dancerNose{}
%One-direction noses
\newcommand\dancerNorthNose{{\unitlength=\dancerrulethickness
  \begin{picture}(0,0)\put(0,-0.5){{\unitlength=\dancernosesize\dancersize
    \begin{picture}(0,0)\put(0,-0.5){%
      \begin{picture}(0,0)\put(0,\dancernosefraction){{\unitlength=\dancersize
        \begin{picture}(0,0)\put(0,0.5){\circle*{\dancernosesize}}\end{picture}}}%
      \end{picture}}%
    \end{picture}}}%
  \end{picture}}}
\newcommand\dancerSouthNose{{\unitlength=\dancerrulethickness
  \begin{picture}(0,0)\put(0,0.5){{\unitlength=\dancernosesize\dancersize
    \begin{picture}(0,0)\put(0,0.5){%
      \begin{picture}(0,0)\put(0,-\dancernosefraction){{\unitlength=\dancersize
        \begin{picture}(0,0)\put(0,-0.5){\circle*{\dancernosesize}}\end{picture}}}%
      \end{picture}}%
    \end{picture}}}%
  \end{picture}}}
\newcommand\dancerEastNose{{\unitlength=\dancerrulethickness
  \begin{picture}(0,0)\put(-0.5,0){{\unitlength=\dancernosesize\dancersize
    \begin{picture}(0,0)\put(-0.5,0){%
      \begin{picture}(0,0)\put(\dancernosefraction,0){{\unitlength=\dancersize
        \begin{picture}(0,0)\put(0.5,0){\circle*{\dancernosesize}}\end{picture}}}%
      \end{picture}}%
    \end{picture}}}%
  \end{picture}}}
\newcommand\dancerWestNose{{\unitlength=\dancerrulethickness
  \begin{picture}(0,0)\put(0.5,0){{\unitlength=\dancernosesize\dancersize
    \begin{picture}(0,0)\put(0.5,0){%
      \begin{picture}(0,0)\put(-\dancernosefraction,0){{\unitlength=\dancersize
        \begin{picture}(0,0)\put(-0.5,0){\circle*{\dancernosesize}}\end{picture}}}%
      \end{picture}}%
    \end{picture}}}%
  \end{picture}}}
%Two-direction noses
\newcommand\dancerNorthEastTilt{\dancertiltedtrue\def\dancertiltangle{45}}
\newcommand\dancerNorthEastNose{{\unitlength=\dancerrulethickness
  \begin{picture}(0,0)\put(-0.35355,-0.35355){{\unitlength=\dancersize
    \ifhalfcirclenoses
      \begin{picture}(0,0)\put(0.35355,0.35355){\circle*{0.45}}\end{picture}%
    \else
      \begin{picture}(0,0)\put(0.49497,0.49497){\circle*{0.4}}\end{picture}%
    \fi}}
  \end{picture}}}
\newcommand\dancerEastNorthNose{\dancerEastNose\dancerNorthNose}
\newcommand\dancerNorthWestTilt{\dancertiltedtrue\def\dancertiltangle{45}}
\newcommand\dancerNorthWestNose{{\unitlength=\dancerrulethickness
  \begin{picture}(0,0)\put(0.35355,-0.35355){{\unitlength=\dancersize
    \ifhalfcirclenoses
      \begin{picture}(0,0)\put(-0.35355,0.35355){\circle*{0.45}}\end{picture}%
    \else
      \begin{picture}(0,0)\put(-0.49497,0.49497){\circle*{0.4}}\end{picture}%
    \fi}}
  \end{picture}}}
\newcommand\dancerWestNorthNose{\dancerWestNose\dancerNorthNose}
\newcommand\dancerSouthEastTilt{\dancertiltedtrue\def\dancertiltangle{45}}
\newcommand\dancerSouthEastNose{{\unitlength=\dancerrulethickness
  \begin{picture}(0,0)\put(-0.35355,0.35355){{\unitlength=\dancersize
    \ifhalfcirclenoses
      \begin{picture}(0,0)\put(0.35355,-0.35355){\circle*{0.45}}\end{picture}%
    \else
      \begin{picture}(0,0)\put(0.49497,-0.49497){\circle*{0.4}}\end{picture}%
    \fi}}
  \end{picture}}}
\newcommand\dancerEastSouthNose{\dancerEastNose\dancerSouthNose}
\newcommand\dancerSouthWestTilt{\dancertiltedtrue\def\dancertiltangle{45}}
\newcommand\dancerSouthWestNose{{\unitlength=\dancerrulethickness
  \begin{picture}(0,0)\put(0.35355,0.35355){{\unitlength=\dancersize
    \ifhalfcirclenoses
      \begin{picture}(0,0)\put(-0.35355,-0.35355){\circle*{0.45}}\end{picture}%
    \else
      \begin{picture}(0,0)\put(-0.49497,-0.49497){\circle*{0.4}}\end{picture}%
    \fi}}
  \end{picture}}}
\newcommand\dancerWestSouthNose{\dancerWestNose\dancerSouthNose}
\newcommand\dancerNorthSouthNose{\dancerNorthNose\dancerSouthNose}
\newcommand\dancerSouthNorthNose{\dancerNorthSouthNose}
\newcommand\dancerEastWestNose{\dancerEastNose\dancerWestNose}
\newcommand\dancerWestEastNose{\dancerEastWestNose}
%Three-direction noses
\newcommand\dancerNorthSouthEastNose{\dancerNorthSouthNose\dancerEastNose}
\newcommand\dancerNorthEastSouthNose{\dancerNorthSouthEastNose}
\newcommand\dancerSouthNorthEastNose{\dancerNorthSouthEastNose}
\newcommand\dancerSouthEastNorthNose{\dancerNorthSouthEastNose}
\newcommand\dancerEastNorthSouthNose{\dancerNorthSouthEastNose}
\newcommand\dancerEastSouthNorthNose{\dancerNorthSouthEastNose}
\newcommand\dancerNorthSouthWestNose{\dancerNorthSouthNose\dancerWestNose}
\newcommand\dancerNorthWestSouthNose{\dancerNorthSouthWestNose}
\newcommand\dancerSouthNorthWestNose{\dancerNorthSouthWestNose}
\newcommand\dancerSouthWestNorthNose{\dancerNorthSouthWestNose}
\newcommand\dancerWestNorthSouthNose{\dancerNorthSouthWestNose}
\newcommand\dancerWestSouthNorthNose{\dancerNorthSouthWestNose}
\newcommand\dancerEastWestNorthNose{\dancerEastWestNose\dancerNorthNose}
\newcommand\dancerEastNorthWestNose{\dancerEastWestNorthNose}
\newcommand\dancerWestEastNorthNose{\dancerEastWestNorthNose}
\newcommand\dancerWestNorthEastNose{\dancerEastWestNorthNose}
\newcommand\dancerNorthEastWestNose{\dancerEastWestNorthNose}
\newcommand\dancerNorthWestEastNose{\dancerEastWestNorthNose}
\newcommand\dancerEastWestSouthNose{\dancerEastWestNose\dancerSouthNose}
\newcommand\dancerEastSouthWestNose{\dancerEastWestSouthNose}
\newcommand\dancerWestEastSouthNose{\dancerEastWestSouthNose}
\newcommand\dancerWestSouthEastNose{\dancerEastWestSouthNose}
\newcommand\dancerSouthEastWestNose{\dancerEastWestSouthNose}
\newcommand\dancerSouthWestEastNose{\dancerEastWestSouthNose}
%Four-direction noses
\newcommand\dancerNorthSouthEastWestNose{\dancerNorthSouthNose\dancerEastWestNose}
\newcommand\dancerNorthSouthWestEastNose{\dancerNorthSouthEastWestNose}
\newcommand\dancerSouthNorthEastWestNose{\dancerNorthSouthEastWestNose}
\newcommand\dancerSouthNorthWestEastNose{\dancerNorthSouthEastWestNose}
\newcommand\dancerEastWestNorthSouthNose{\dancerNorthSouthEastWestNose}
\newcommand\dancerWestEastNorthSouthNose{\dancerNorthSouthEastWestNose}
\newcommand\dancerEastWestSouthNorthNose{\dancerNorthSouthEastWestNose}
\newcommand\dancerWestEastSouthNorthNose{\dancerNorthSouthEastWestNose}
\newcommand\dancerNorthEastSouthWestTilt{\dancertiltedtrue\def\dancertiltangle{45}}
\newcommand\dancerNorthEastSouthWestNose{\dancerNorthEastNose\dancerSouthWestNose}
\newcommand\dancerSouthWestNorthEastNose{\dancerNorthEastSouthWestNose}
\newcommand\dancerNorthEastWestSouthNose{\dancerNorthEastSouthWestNose}
\newcommand\dancerSouthWestEastNorthNose{\dancerNorthEastSouthWestNose}
\newcommand\dancerEastNorthSouthWestNose{\dancerNorthEastSouthWestNose}
\newcommand\dancerWestSouthNorthEastNose{\dancerNorthEastSouthWestNose}
\newcommand\dancerEastNorthWestSouthNose{\dancerNorthEastSouthWestNose}
\newcommand\dancerWestSouthEastNorthNose{\dancerNorthEastSouthWestNose}
\newcommand\dancerNorthWestSouthEastTilt{\dancertiltedtrue\def\dancertiltangle{45}}
\newcommand\dancerNorthWestSouthEastNose{\dancerNorthWestNose\dancerSouthEastNose}
\newcommand\dancerSouthEastNorthWestNose{\dancerNorthWestSouthEastNose}
\newcommand\dancerNorthWestEastSouthNose{\dancerNorthWestSouthEastNose}
\newcommand\dancerSouthEastWestNorthNose{\dancerNorthWestSouthEastNose}
\newcommand\dancerWestNorthSouthEastNose{\dancerNorthWestSouthEastNose}
\newcommand\dancerEastSouthNorthWestNose{\dancerNorthWestSouthEastNose}
\newcommand\dancerWestNorthEastSouthNose{\dancerNorthWestSouthEastNose}
\newcommand\dancerEastSouthWestNorthNose{\dancerNorthWestSouthEastNose}

%Thirty-degree noses
\newcommand\dancerEastEastSouthTilt{\dancertiltedtrue\def\dancertiltangle{30}}
\newcommand\dancerEastEastSouthNose{\dancerEastNose}

\expandafter\newcommand\csname dancerseprulecolor\endcsname{\color{black}}
\expandafter\newcommand\csname dancerseprulecolor0\endcsname{\color{blue}}
\expandafter\newcommand\csname dancerseprulecolor1\endcsname{\color{magenta}}
\expandafter\newcommand\csname dancerseprulecolor2\endcsname{\color{green}}
\expandafter\newcommand\csname dancerseprulecolor3\endcsname{\color{cyan}}
\expandafter\newcommand\csname dancerseprulecolor4\endcsname{\color{red}}
\expandafter\newcommand\csname dancerseprulecolor5\endcsname{\color{yellow}}
\expandafter\newcommand\csname dancerseprulecolor6\endcsname{\color{BurntOrange}}
\expandafter\newcommand\csname dancerseprulecolor7\endcsname{\color{DarkBlue}}
\expandafter\newcommand\csname dancerseprulecolor8\endcsname{\color{DarkOrchid}}
\expandafter\newcommand\csname dancerseprulecolor9\endcsname{\color{Purple}}

\newcommand\dancerSeprule{\dancersepruledefaultshape}
\newcommand\dancerNorthSeprule{}
\newcommand\dancerSouthSeprule{}
\newcommand\dancerEastSeprule{}
\newcommand\dancerWestSeprule{}
\newcommand\dancerNorthSouthSeprule{\relax
  \vbox to 0pt{\vss\hbox to 0pt{\hss\vrule height \dancermatrixunit
                                           width \dancerseprulecount\dancerseprulethickness
                                           \hss}\vss}}
\newcommand\dancerEastWestSeprule{\relax
  \vbox to 0pt{\vss\hbox to 0pt{\hss\vrule height \dancerseprulecount\dancerseprulethickness
                                           width \dancermatrixunit
                                           \hss}\vss}}

\newcommand\dancerSouthNorthSeprule{\dancerNorthSouthSeprule}
\newcommand\dancerWestEastSeprule{\dancerEastWestSeprule}


\newcommand\dancerNWtoSESeprule{{\unitlength=\dancermatrixunit
  \begin{picture}(0,0)\linethickness{\dancerseprulethickness}%
  \put(-0.5,0.5){\line(1,-1){1}}\end{picture}}}

\newcommand\dancerSWtoNESeprule{{\unitlength=\dancermatrixunit
  \begin{picture}(0,0)\linethickness{\dancerseprulethickness}%
  \put(-0.5,-0.5){\line(1,1){1}}\end{picture}}}


% What can you put on a dancer spot?
% There are three basic attributes:
%    label (default none)
%    dancer style (default "b")
%    nose direction (default none)
%
%   digits serves as dancer label
%   [xxx]  serves as dancer label
%   B b    boy style (square)
%   G g    girl style (circle)
%   P p    phantom (dashed square)
%   Y      black square (label will be white)
%   y      gray square (label will be black)
%   X      black circle (label will be white)
%   r      gray circle (label will be black)
%   d      box with dot in it
%   t      circle with dot in it
%   x      box with large "x" in it
%   z      box with large slash (sinister) in it
%   q      box with hollow square in it
%   +      dancer spot mark
%   O      dancer bullet mark
%   *      star mark (suitable for indicating a star or thar)
%   .      no dancer style, does not count for bounding box calculation
%   _      no dancer style, does count for bounding box calculation
%   n ^    north nose
%   s V    south nose
%   e >    east nose
%   w <    west nose
%   v |    north-south nose
%   h -    east-west nose
%   @      flagpole center: no dancer (label and direction have special uses)
%   F      flagpole dot: no dancer (label and direction have special uses)
%   (rot)  first occurrence sets diagram rotation angle
%   (xxx)  other occurrence serves as a rotated label
%   |      vertical separation rule
%   -      horizontal separation rule
%   D      dexter diagonal separation rule \
%   S      sinister diagonal separation rule /
% The following apply in \halfdancerspot mode only:
%   ` moves 1/4 spot up
%   , moves 1/4 spot down
%   ' on the left moves 1/4 spot left
%   ' on the right moves 1/4 spot right
% Special control sequences:
% \end
% \caption
% \bigcaption
% \overlay
% \\
% \cr




\newcommand\ConcentricDiamondsBeltwayPath{\begin{picture}(0,0)
    \thicklines
    \put(-0.5,-3){\line(1,0){1}}
    \put(-0.5,3){\line(1,0){1}}
    \put(0.5,0){\oval(3,6)[r]}
    \put(-0.5,0){\oval(3,6)[l]}
  \end{picture}}
\newcommand\HourglassBeltwayPath{\begin{picture}(0,0)
    \thicklines
    \put(-0.5,-3){\line(1,0){1}}
    \put(-0.5,3){\line(1,0){1}}
    \put(0.5,0){\oval(1,6)[r]}
    \put(-0.5,0){\oval(1,6)[l]}
  \end{picture}}
\newcommand\TallPlanetBeltwayPath{\ConcentricDiamondsBeltwayPath}
\newcommand\StarPlanetBeltwayPath{\HourglassBeltwayPath}
\newcommand\WidePlanetBeltwayPath{\begin{picture}(0,0)
    \thicklines
    \put(-0.5,-4){\line(1,0){1}}
    \put(-0.5,4){\line(1,0){1}}
    \put(0.5,0){\oval(1,8)[r]}
    \put(-0.5,0){\oval(1,8)[l]}
  \end{picture}}



\def\sectionhead#1{\section*{#1}\addcontentsline{toc}{chapter}{#1}}

\newcount\calldepth  \calldepth=0
\newcommand\Call[1]{\ifcase\calldepth
   \list{}{\leftmargin=0pt\topsep=10pt}\item\relax{\refstepcounter{section}\normalfont\normalsize\bfseries #1}\par\nobreak
   \def\tocline{\addcontentsline{toc}{section}{#1}}\relax
 \or
   \list{}{\topsep=2pt}\item\relax{\refstepcounter{subsection}\normalfont\normalsize\bfseries #1}\par\nobreak
   \def\tocline{\addcontentsline{toc}{subsection}{#1}}\relax
 \else
   \list{}{\topsep=2pt}\item\relax{\normalfont\normalsize\bfseries #1}\par\nobreak
   \let\tocline=\relax
 \fi
 \catcode`\#=12 \catcode`\&=12\relax
 \advance\calldepth by 1\@ifstar{\relax}{\tocline}}
\def\endCall{\endlist}

% \expandafter\newcommand\csname Call*\endcsname[1]{\ifcase\calldepth
%    \list{}{\leftmargin=0pt}\item\relax{\normalfont\normalsize\bfseries #1}\par
%  \or
%    \list{}{}\item\relax{\normalfont\normalsize\bfseries #1}\par
%  \else
%    \list{}{}\item\relax{\normalfont\normalsize\bfseries #1}\par
%  \fi
%  \advance\calldepth by 1\relax}
% \expandafter\def\csname endCall*\endcsname{\endlist}

\newcommand\changelog{\relax
  \def\entry##1##2{##1&\parbox[t]{5.3in}{\strut\ignorespaces##2\strut}\\ \hline}
  \catcode`\#=12 \catcode`\&=12\relax
  \tabular{@{}|r|l|@{}}
  \hline
  \multicolumn{1}{@{}|l|}{Date} & Change \\ \hline}
\def\endchangelog{\endtabular}

%\makeatletter
\ifx\l@chapter\relax
  \renewcommand*\l@chapter{\@dottedtocline{0}{0em}{2.4em}}
\fi
\ifx\l@section\relax
  \renewcommand*\l@section{\@dottedtocline{1}{0.8em}{2.4em}}
\fi
\ifx\l@subsection\relax
  \renewcommand*\l@subsection{\@dottedtocline{2}{1.6em}{2.4em}}
\fi
\ifx\tableofcontents\relax
  {\catcode`\#=13 \global\def\defineactivesharp{\def##{{\char'43}}}}
  \renewcommand\tableofcontents{%
    \if@twocolumn
      \@restonecolfalse
    \else
      \@restonecoltrue
    \fi
    \twocolumn[\hbox to \textwidth{\hss\Large\bf Table of Contents\hss}\vskip12pt]
    {\catcode`\#=13 \catcode`\&=12 \defineactivesharp\@starttoc{toc}}%
    \onecolumn
    \if@restonecol\onecolumn\fi
    }
\fi
\ifx\@pnumwidth\relax
  \renewcommand\@pnumwidth{1em}
\fi
%\makeatother

\raggedbottom

% latex-squares.sty ends here
