% \iffalse
% <*chit>
% --------------------------------------------------------------------
% \fi
% \subsubsection{Table of chits}
%
%    \begin{macrocode}
\tikzset{
  chit/cell background/.style={draw=black},
  %% Define to not dp anyting to disable wings 
  chit/cell wings/.style={
    dashed,
    dash pattern=on 0pt off 3pt on 3pt off 0pt,
    draw=black},
  %chit/cell background flipped/.style={fill=black},
  blank chit/.style={/chit/frame={draw=none,fill=none}},
  chit/grid lines/.style={dashed},
}
%    \end{macrocode}
% 
% These macros are used when we set tables of chits.  This allows us
% to define blank spaces in the table by giving the element
% \texttt{blank~chit}.
% 
%    \begin{macrocode}
\def\chit@blank{blank chit}
\def\chit@cellbg(#1,#2)#3{%
  \draw[chit/cell background](#1-#3/2,#2-#3/2) rectangle++(#3,#3);
}
\def\chit@celldblbg(#1,#2)#3{%
  \draw[chit/cell background,chit/cell background flipped/.try]%
  (#1-#3/2,#2-#3/2) rectangle++(#3,#3);
}
%    \end{macrocode}
% 
% \begin{Macro}{\ifchits@reset}
%
%   This `if' controls whether to reset the coordinates to the origin
%   when \cs{chits} is called.  If true, then reset for a new table.
%   
%    \begin{macrocode}
\newif\ifchits@reset\chits@resettrue
%    \end{macrocode}
% \end{Macro}
%
% \begin{enumerate}
% \item column
% \item row
% \item width
% \item number of columns
% \end{enumerate}
% 
% \begin{Macro}{\chits,\@chits,\chit@sng@cellupdate}
%    \begin{macrocode}
\def\chit@sng@cellupdate(#1,#2)#3#4{%
  \chit@dbg{2}{Current `#1' vs `#4'*(`#3'+1)}
  \pgfmathparse{ifthenelse(#1>=#4*(#3-1),#2-#4,#2)}%
  \xdef#2{\pgfmathresult}%
  \pgfmathparse{ifthenelse(#1>=#4*(#3-1),0,#1+#4)}%
  \xdef#1{\pgfmathresult}%
}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\chit@cell@frame#1#2{%
  \coordinate(cell@frame@north east) at ($(#1.north east)+( #2/2, #2/2)$);
  \coordinate(cell@frame@north west) at ($(#1.north west)+(-#2/2, #2/2)$);
  \coordinate(cell@frame@south east) at ($(#1.south east)+( #2/2,-#2/2)$);
  \coordinate(cell@frame@south west) at ($(#1.south west)+(-#2/2,-#2/2)$);
  \draw[chit/cell background,fill=none,scale line widths,line width=#2cm]
  (cell@frame@south west) rectangle (cell@frame@north east);
  \draw[scale line widths,line width=#2cm,chit/cell wings=10pt,fill=none,scale line widths,line width=#2cm]
  (cell@frame@north east)--++(     0,10*#2)
  (cell@frame@north east)--++( 10*#2,0)
  (cell@frame@north west)--++(     0,10*#2)
  (cell@frame@north west)--++(-10*#2,0)
  (cell@frame@south east)--++(     0,-10*#2)
  (cell@frame@south east)--++( 10*#2,0)
  (cell@frame@south west)--++(     0,-10*#2)
  (cell@frame@south west)--++(-10*#2,0)
  ;
}
%    \end{macrocode}
% The stared version (\cs{chits*}) of this macro continues the
% previously set chit table.
% 
%    \begin{macrocode}
\def\chits{%
  \@ifstar{\chits@resetfalse\@chits}{\chits@resettrue\@chits}}
%    \end{macrocode}
% 
% \begin{enumerate}
% \item Chits.  List (or list of list) of chit styles.
% \item Number of columns
% \item Size of cells 
% \end{enumerate}
% 
%    \begin{macrocode}
\newcount\wg@col
\newcount\wg@row
\def\@chits#1#2#3{
  \ifchits@reset
    %\def\r{0}%
    %\def\c{0}%
    \coordinate(wg@next) at (0,0);
    \wg@col=1
    \wg@row=1
  \fi
  \chit@dbg{1}{Chits to make: #1}%
  \foreach[count=\ti from 0] \t/\x in #1{%
    \chit@dbg{2}{Turn `\t' with option `\x'}
    \ifx\t\empty\else%
      \foreach \u/\m in \t{%
        \ifx\u\empty\else%
          \chit@dbg{2}{Next chit `\u' with possible multiplicity `\m'}%
          \ifx\m\@empty\def\m{1}\fi%
          \ifx\u\m\def\m{1}\fi%
          \chit@dbg{2}{Next chit `\u' multiplicity `\m'}%
          \foreach \n in {1,...,\m}{%
            \ifx\u\chit@blank%
              \chit@dbg{3}{Ignoring blank chit:\u}%
            \else%
              \ifx\u\chit@oob@vspacer%
                \chit@dbg{3}{Ignoring vertical spacer:\u}%
              \else
                \ifx\u\chit@oob@spacer%
                  \chit@dbg{3}{Ignoring horizontal spacer:\u}%
                \else
                  % \chit@cellbg(\c,\r){#3}%
                  %\chit[\u=\ti](\c,\r)%
                  \chit[\u=\ti](wg@next)(wg@chit)%
                  \chit@cell@frame{wg@chit}{#3}
                  \global\advance\wg@col1
                  \ifnum\wg@col>#2%
                    \path
                    let
                    \p1=(wg@chit.north),
                    \p2=(wg@chit.south),
                    \p3=(wg@chit),
                    \n1={\y3-\y1+\y2-#3cm} in 
                    coordinate(wg@next) at (0,\n1);
                    \global\wg@col=1
                    \global\advance\wg@row1
                    \chit@dbg{10}{Row row, next column `\the\wg@col'}
                  \else
                    \path
                    let
                    \p1=(wg@chit.east),
                    \p2=(wg@chit.west),
                    \p3=(wg@chit),
                    \n1={\x3+\x1-\x2+#3cm} in 
                    coordinate(wg@next) at (\n1,\y3);
                    \chit@dbg{10}{Next column `\the\wg@col'}
                  \fi
                  %\chit@sng@cellupdate(\c,\r){#2}{#3}%
                \fi%
              \fi%
            \fi%
          }%
        \fi%
      }%
    \fi%
  }%
  \chit@dbg{10}{Table is `\the\wg@col'x`\the\wg@row'}
  \@ifnextchar;{\@gobble}{}}
%    \end{macrocode}
% \end{Macro}
% \begin{Macro}{\chitgrid}
% \begin{enumerate}
% \item columns
% \item rows
% \item cell-size
% \end{enumerate}
%
%    \begin{macrocode}
\def\chitgrid#1#2#3{%
  \pgfmathparse{#3/2}\edef\rmin{\pgfmathresult}%
  \pgfmathparse{#2*#3-#3/2}\edef\rmax{\pgfmathresult}%
  %\draw[red](-#3/2,\rmin)rectangle(#3*#1-#3/2,-\rmax);
  \foreach \cc in {0,...,#1}{
    \draw[chit/grid lines] (\cc*#3-#3/2,3*#3/4)--(\cc*#3-#3/2,-\rmax-#3/4);}
  %\chit@dbg{0}{Drawing horizontal lines from `\rmin, `-\rmin', ..., `-\rmax'}
  \foreach \rr in {\rmin,-\rmin,...,-\rmax}{
    %\chit@dbg{0}{Horizontal line at `\rr'}
    \draw[chit/grid lines] (-3*#3/4,\rr)--(#1*#3-#3/4,\rr);}  
}
%    \end{macrocode}
% \end{Macro}
%
% \begin{Macro}{\doublechits,
%   \@doublechits,
%   \chit@dbl@cellupdate,
%   \chit@dbl@flip}
% \begin{enumerate}
% \item coordinates
% \item coordinates
% \item cell-size
% \end{enumerate}
% 
%    \begin{macrocode}
\def\chit@dbl@flip(#1,#2)#3{%
  \pgfmathparse{-#1}%
  \xdef\mc{\pgfmathresult}%
}
%    \end{macrocode}
% 
%   \begin{enumerate}
%   \item coordinates
%   \item coordinates
%   \item Number of columns
%   \item cell-size
%   \end{enumerate}
%
%    \begin{macrocode}
\def\chit@dbl@cellupdate(#1,#2)#3#4{%
  \pgfmathparse{ifthenelse(#1<-#4/2,#2,#4+#2)}%
  \xdef#2{\pgfmathresult}%
  \pgfmathparse{ifthenelse(#1<-#4/2,#4+#1,-(#3-.5)*#4)}%
  \xdef#1{\pgfmathresult}%
}
%    \end{macrocode}
%
%   \begin{enumerate}
%   \item List of list of keys
%   \item Number of columns
%   \item size of each cell
%   \end{enumerate}
% 
%   The stared version (\cs{doublechits*}) of this macro continues the
%   previously set chit table.
% 
%    \begin{macrocode}
\def\doublechits{%
  \@ifstar{\chits@resetfalse\@doublechits}{\chits@resettrue\@doublechits}}
%    \end{macrocode}
%
% \begin{enumerate}
% \item List (or list of list) of counters
% \item Max number columns (for front)
% \item Extra spacing
% \end{enumerate}
% 
%    \begin{macrocode}
\newdimen\chit@w
\newdimen\chit@h
\def\@doublechits#1#2#3{%
  \chit@dbg{1}{Setting double-sided chits: #1}
  \ifchits@reset%
    %\pgfmathparse{-(#2-.5)*#3}
    %\xdef\c{\pgfmathresult}
    %\def\r{0}
    \coordinate(wg@next) at (0,0);%
    \coordinate(wg@fnxt) at (0,0);%
    \wg@col=1%
    \wg@row=1%
    \chit@w=0pt%
    \chit@h=0pt%
  \fi%
  %
  \foreach[count=\ti from 0] \t/\x in #1{
    \ifx\t\empty\else%
      \foreach \u/\m in \t{
        \ifx\u\empty\else
          \ifx\m\@empty\def\m{1}\else%
            \ifx\u\m\def\m{1}\fi\fi
          \chit@dbg{2}{`\u'=`\m' (\c,\r)}
          \foreach \n in {1,...,\m}{%
            \ifx\u\chit@blank
              \chit@dbg{3}{Ignoring blank chit:\u}
            \else
              \ifx\u\chit@oob@vspacer%
                \chit@dbg{3}{Ignoring vertical spacer:\u}%
              \else
                \ifx\u\chit@oob@spacer%
                  \chit@dbg{3}{Ignoring horizontal spacer:\u}%
                \else
                  \chit@dbg{10}{Drawing `\u' at wg@next}
                  \chit[\u=\ti](wg@next)(wg@chit)%
                  \chit@cell@frame{wg@chit}{#3}
                  \ifdim\chit@w=0pt
                    \pgfextractx\chit@w{\pgfpointanchor{wg@chit}{east}}%
                    \pgfextractx\wg@tmpa{\pgfpointanchor{wg@chit}{west}}%
                    \advance\chit@w-\wg@tmpa%
                    \global\advance\chit@w#3cm%
                    \pgfextracty\chit@h{\pgfpointanchor{wg@chit}{north}}%
                    \pgfextracty\wg@tmpa{\pgfpointanchor{wg@chit}{south}}%
                    \advance\chit@h-\wg@tmpa%
                    \global\advance\chit@h#3cm%
                  \fi
                  \ifnum\wg@col=1
                    \pgfextractx\wg@tmpa{\pgfpointanchor{wg@chit}{center}}%
                    \advance\wg@tmpa+\chit@w%
                    \coordinate(wg@fnxt) at ($(wg@chit)+(\the\chit@w,0pt)$);
                    %\coordinate(wg@fnxt
                    %\chit@dbg{10}{Calculating wg@fnxt since `\the\wg@col'==1}
                    %\path
                    %let
                    %\p1=(wg@chit.east),
                    %\p2=(wg@chit.west),
                    %\p3=(wg@chit),
                    %\n1={\x3+\x1-\x2+#3cm} in 
                    %coordinate(wg@fnxt) at (\n1,\y3);
                  \fi                  
                  %\chit@dbg{10}{Drawing `\u\space flipped' at wg@fnxt}
                  \chit[\u\space flipped=\ti,
                  zone turn=\t,
                  zone mult=\n](wg@fnxt)(wg@flip)%
                  \chit@cell@frame{wg@flip}{#3}
                  \chit@dbg{10}{Next column: `\the\wg@col'}
                  \ifnum\wg@col=1
                    \ifnum\wg@row=1
                      \coordinate(wg@table@top) at (wg@chit.north east);
                    \fi
                    \coordinate(wg@table@bot) at (wg@chit.south east);
                  \fi
                  \global\advance\wg@col1
                  \ifnum\wg@col>#2%
                    % \path
                    %    let
                    %    \p1=(wg@chit.north),
                    %    \p2=(wg@chit.south),
                    %    \p3=(wg@chit),
                    %    \n1={\y3-\y1+\y2-#3cm} in %1.21991
                    %    coordinate(wg@next) at (0,\n1);
                    \coordinate(wg@next) at (0,-\wg@row\chit@h);
                    \global\wg@col=1
                    \global\advance\wg@row1
                    \chit@dbg{10}{Row row, next column `\the\wg@col'}
                  \else
                    % \path
                    % let
                    % \p1=(wg@chit.east),
                    % \p2=(wg@chit.west),
                    % \p3=(wg@chit),
                    % \p4=(wg@flip.east),
                    % \p5=(wg@flip.west),
                    % \p6=(wg@flip),
                    % \n1={\x3-\x1+\x2-#3cm},
                    % \n2={\x6+\x4-\x5+#3cm} in 
                    % coordinate(wg@next) at (\n1,\y3)
                    % coordinate(wg@fnxt) at (\n2,\y6);
                    \coordinate(wg@next) at ($(wg@chit)+(-\the\chit@w,0)$);
                    \coordinate(wg@fnxt) at ($(wg@flip)+(+\the\chit@w,0)$);
                    \chit@dbg{10}{Next column `\the\wg@col'}
                  \fi
                \fi%
              \fi%
            \fi%
          }%
        \fi%
      }%
    \fi%
  }%
  \draw[dashed,scale line widths](wg@table@top)--++(0,.4);
  \draw[dashed,scale line widths](wg@table@bot)--++(0,-.4);
  \draw[dashed,scale line widths,{Stealth[]}-]
  ($(wg@table@top)+(0,.2)$) -- ++(.5,0)
  node[transform shape,anchor=west]{Back};
  \draw[dashed,scale line widths,{Stealth[]}-]
  ($(wg@table@top)+(0,.2)$) -- ++(-.5,0)
  node[transform shape,anchor=east]{Front};
  \draw[dashed,scale line widths,{Stealth[]}-]
  ($(wg@table@bot)+(0,-.2)$) -- ++(.5,0)
  node[transform shape,anchor=west]{Back};
  \draw[dashed,scale line widths,{Stealth[]}-]
  ($(wg@table@bot)+(0,-.2)$) -- ++(-.5,0)
  node[transform shape,anchor=east]{Front};
  % \draw[dashed,<-] (#3/5,-2*#3/3)--(#3/2,-2*#3/3) node[transform shape,anchor=west]{Back};%
  % \draw[dashed,<-] (-#3/5,-2*#3/3)--(-#3/2,-2*#3/3) node[transform shape,anchor=east]{Front};%
  % \foreach \cc in {0,...,#2}{
  %   \draw[dashed] (\cc*#3,-3*#3/4)--(\cc*#3,\r-#3/4);
  %   \draw[dashed] (-\cc*#3,-3*#3/4)--(-\cc*#3,\r-#3/4);}
  % \pgfmathparse{#3/2}\edef\rmin{\pgfmathresult}%
  % \chit@dbg{0}{Drawing horizontal lines from `-\rmin, `\rmin', ..., `\r'}
  % \foreach \rr in {-\rmin,\rmin,...,\r}{
  %   \chit@dbg{0}{Horizontal line at `\rr'}
  %   \draw[dashed] (-#2*#3-#3/4,\rr)--(#2*#3+#3/4,\rr);}
  \@ifnextchar;{\@gobble}{}}  
%    \end{macrocode}
% \end{Macro}
%    
% \begin{Macro}{\doublechitgrid}
% \begin{enumerate}
% \item columns
% \item rows
% \item cell-size
% \end{enumerate}
%
%    \begin{macrocode}
\def\doublechitgrid#1#2#3{%
  \message{^^JWARNING - the grid table might be messed up!}
  %\iffalse
  \pgfmathparse{#3/2}\edef\rmin{\pgfmathresult}%
  \pgfmathparse{#2*#3-#3/2}\edef\rmax{\pgfmathresult}%
  \foreach \cc in {0,...,#1}{
    \draw[chit/grid lines] (\cc*#3+\rmin,3*#3/4)--(\cc*#3+\rmin,-\rmax-#3/4);
    \draw[chit/grid lines] (-\cc*#3+\rmin,3*#3/4)--(-\cc*#3+\rmin,-\rmax-#3/4);}
  %\chit@dbg{0}{Drawing horizontal lines from `-\rmin, `\rmin', ..., `\rmax'}
  \foreach \rr in {\rmin,-\rmin,...,-\rmax}{
    \chit@dbg{0}{Horizontal line at `\rr'}
    \draw[chit/grid lines] (-#1*#3-#3/4+\rmin,\rr)--(#1*#3+#3/4+\rmin,\rr);}
  %\fi
}
%    \end{macrocode}
% \end{Macro}
% \iffalse
% </chit>
% --------------------------------------------------------------------
% \fi