% \iffalse meta-comment
%
% texmate - Chess typesetting.
% Copyright 2005-6 Federico Garcia (federook@gmail.com)
% -------------------------------------------
% 
%
% This program can be redistributed and/or modified under the terms
% of the LaTeX Project Public License distributed from CTAN archives
% in the directory macros/latex/base/lppl.txt; either version 1 of
% the License, or (at your option) any later version.
%
%
%<*driver>
% \fi
\ProvidesFile{texmate.dtx}[2006/05/31 v2 Chess typesetting]
% \iffalse
\documentclass{ltxdoc}
\GetFileInfo{texmate.dtx}
\title{\TeX mate\,2\\(comprehensive chess annotation in \LaTeX)\\Implementation}
\date{\filedate{}}
 \author{Federico Garcia\\\texttt{federook@gmail.com}}

\begin{document}
\maketitle
 \DocInput{\filename}
\end{document}
%</driver>
% \fi
%The user's manual and a sample of the package are found as an independent document (it \emph{uses} the package, so it has to be typeset after installation): \texttt{texmate2manual.pdf} (source \texttt{texmate2manual.tex}). Here is the code. 
%
%I have had no time to comment this yet. I've divided the code into parts, but beyond that, I have to refer the interested reader to the implementation of version~1, which provides some explanation.
%
%Each `part' usually follows the same order: variables are declared, the main one or two functions are defined, then auxiliary functions, then commands for user customization, and then defaults. Sometimes this order changes.
%
%\tableofcontents
%\section{Prelimitary matters}
%    \begin{macrocode}
%<*package>
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{texmate}[2005/06/31 v2 Chess typesetting (Federico Garcia)]
\newcommand*\TeXmate{\TeX mate}
\newif\if@skakon\@skakontrue
\newif\if@skaking
\newif\if@diagnostics\@diagnosticsfalse
\DeclareOption{filling}{\typeout{Option `filling' obsolete.}}
\DeclareOption{notfilling}{\typeout{Option `nofilling' obsolete.}}
\DeclareOption{skakoff}{\@skakonfalse
    \typeout{Using TeXmate 2 without skak is dangerous. %
        Consider using skak too, or using TeXmate 1 instead.}%
    \@skakingfalse
    }
\DeclareOption{diagnostics}{\@diagnosticstrue}
\DeclareOption*{\typeout{Unknown option (`\CurrentOption')}}
\ExecuteOptions{}
\ProcessOptions
\RequirePackage{amssymb}
\RequirePackage{chessfss}
\usesymfig
\if@skakon
    \RequirePackage{skak}[2005/06/29 v1.4a]
    \smallboard
    \notationoff
    \@skakontrue\@skakingtrue
    \newtoks\@tmtoskak
\fi
%    \end{macrocode}
%
%\section{Chess mode}
%
%   \begin{macrocode}
\def\@chesscodes{\catcode`\ =\active \catcode`\.=\active \catcode`\;=\active
    \catcode`\[=\active \catcode`\]=\active
    \catcode`\>=\active \catcode`\+=\active 
    \catcode`\:=\active }
\def\@nochesscodes{\catcode`\ =10 \catcode`\.=12 \catcode`\;=12
    \catcode`\]=12 \catcode`\[=12
    \catcode`\>=12 \catcode`\x=11 \catcode`\+=12 
    \catcode`\:=12 }
\def\@semicolon{; }
{\@chesscodes\iffalse
\fi\gdef\@chesschars{\iffalse
    \fi\def+{\checksign}\iffalse
    \fi\long\def ##1{\ifx ##1\else\iffalse
        \fi\if@delimited\else\@@turn\fi\expandafter\@execute\fi##1}\iffalse
    \fi\def.{ }\gdef;{ }\global\let\;\@semicolon\iffalse
    \fi\def[{\@opencomm{open}}\iffalse
    \fi\def\[{\@opencomm{opent}}\iffalse
    \fi\def]{ \@closecomm{close} }\iffalse
    \fi\def\]{\@closecomm{closet} }\iffalse
    \fi\let>\egroup}\iffalse
\fi}
\def\@@openchess{%
    \advance\@commlevel-1\relax
    \if@skaking
        \@tmtoskak{}%
    \fi
    \csname\@roman{\the\@commlevel}font\endcsname
    \@chesscodes
    \@chesschars
    \@execute}
\def\@@closechess{%
    \@nochesscodes
    \if@delimited\else\@@turn\fi
    \advance\@commlevel1\relax
    \normalfont
    \@resumingtrue\unskip}
\newcommand*\makebarother{\catcode`\|=12 }
{\catcode`\|=\active
\gdef\makebarchess{\catcode`\|=\active
    \let|\@openchess}
\gdef\@openchess{\null
    \let|\@closechess
    \@@openchess}
\gdef\@closechess{\let|\@openchess
    \@@closechess}
}
\AtBeginDocument{\ifnum\catcode`\|=12\relax
        \makebarchess
    \else
        \PackageWarning{TeXmate}{`|' not used for chess 
        (it seems to have a special meaning for another package). 
        Use \string\begin{texmate} instead.}%
    \fi} 
\newenvironment{texmate}{\@@openchess}{\@@closechess}
%    \end{macrocode}
%
%\section{The input}
%
%    \begin{macrocode}
\def\pieceinitials#1{\@initials#1}
\def\@initials#1#2#3#4#5#6{%
    \if@skakon
        \newskaklanguage{texmate}{#6#5#2#4#3#1}%
        \skaklanguage[texmate]%
    \fi
    \setfigtextchars #6#5#2#4#3#1%
    \gdef\@Pawn{#1}\gdef\@Rook{#2}\gdef\@Knight{#3}%
    \gdef\@Bishop{#4}\gdef\@Queen{#5}\gdef\@King{#6}%
    \lowercase{\gdef\@pawn{#1}\gdef\@rook{#2}\gdef\@knight{#3}%
    \gdef\@bishop{#4}\gdef\@queen{#5}\gdef\@king{#6}}}
\pieceinitials{PRNBQK}
\def\@Castle{O}
%    \end{macrocode}
%
%\section{The moves}
%
%    \begin{macrocode}
\newif\if@white
\newif\if@resuming
\newif\if@delimited
\newcount\move
\long\def\@execute#1{\let\next\relax
    \ifcat1\noexpand#1%
        \ifnum0=#1
            \if@white
                \if@resuming
                    \def\next{%
                        \beforeno\the\move\afterno
                        \expandafter\@castle\@gobble
                        }%
                \else
                    \def\next{%
                        \afterb\beforeno\the\move\afterno
                        \expandafter\@castle\@gobble}%
                \fi
            \else
                \if@resuming
                    \def\next{%
                        \beforeb
                        \advance\move1\relax
                        \expandafter\@castle\@gobble}%
                \else
                    \def\next{%
                        \afterw
                        \advance\move1\relax
                        \expandafter\@castle\@gobble}%
                \fi
            \fi
            \@delimitedfalse
            \@resumingfalse
        \else
            \def\next{\move}%
        \fi
    \else
    \ifcat a\noexpand#1%
        \if@white
            \if@resuming
                \def\next{\beforeno\the\move\afterno
                    \catcode`\x=\active
                    \@@piece}%
            \else
                \def\next{\afterb\beforeno\the\move\afterno
                    \catcode`\x=\active
                    \@@piece}%
            \fi
        \else
            \if@resuming
                \def\next{%
                    \beforeb
                    \advance\move1
                    \catcode`\x=\active
                    \@@piece}%
            \else
                \def\next{%
                    \afterw
                    \advance\move1
                    \catcode`\x=\active
                    \@@piece}%
            \fi
        \fi
        \@delimitedfalse
        \@resumingfalse
    \fi\fi
    \next#1}
\def\@@piece#1{%
    \ifcat\noexpand~\noexpand#1%
        \def\next{\@#1}%
    \else
    \catcode`\x=11\relax
    \ifcat\relax\noexpand#1%
        \def\next{\@#1}%
    \else
        \def\next{\catcode`\x=\active\@@piece}%
        \if@skaking
            \ifcat1\noexpand#1%
                \@tmtoskak\expandafter{\the\@tmtoskak #1}%
            \else\ifcat a#1%
                \@tmtoskak\expandafter{\the\@tmtoskak #1}%
            \fi\fi
        \fi
        \def\temp{#1}%
        \ifx\temp\@Rook\textsymrook\else
        \ifx\temp\@Knight\textsymknight\else
        \ifx\temp\@Bishop\textsymbishop\else
        \ifx\temp\@Queen\textsymqueen\else
        \ifx\temp\@King\textsymking\else
        \ifx\temp\@Castle\let\next\@castle\else
        \def\next{#1\catcode`\x=\active\@@piece}%
        \fi\fi\fi\fi\fi\fi
    \fi\fi
    \next}
\def\@castleadd{--\@castlechar}
\def\CastleO{\def\@castlechar{O}}
\def\Castle#1{\def\@castlechar{0}}
\long\def\@castle#1#2#3{%
    \ifx-#3%
        \mbox{\@castlechar\@castleadd\@castleadd}%
        \if@skaking\@tmtoskak{O-O-O}\fi
        \let\next\@gobble
    \else
        \mbox{\@castlechar\@castleadd}%
        \if@skaking\@tmtoskak{O-O}\fi
        \def\next{#3}%
    \fi
    \next
    }
\Castle0
\def\takes{\makebox[1.2ex][c]{$\times$}}
\if@skakon
    \def\@takes{\catcode`\x=11\relax\@tmtoskak\expandafter
        {\the\@tmtoskak x}\takes\@@piece}
\else
    \def\@takes{\catcode`\x=11\relax\takes}
\fi
{\catcode`\x=\active \catcode`\:=\active \gdefx{\@takes}\gdef:{\@takes}}
%    \end{macrocode}
%
%\section{Commentary}
%
%    \begin{macrocode}
\newcount\@commlevel
\let\tm@aftermove\relax
\def\@opencomm#1{%
    \@resumingtrue
    \catcode`\x=11\relax
    \if@skaking
        \expandafter\storegame\expandafter{\@roman{\the\@commlevel}comm-game}%
        \if@diagnostics\message{Stored position for comment at level 
            \the\@commlevel\ after \tm@tomainline.}\fi
    \else{} \fi
    \bgroup
    \renewcommand\@diagramtop{\analysistop}%
    \renewcommand\@diagrambottom{}%
    \if@delimited\else\@@turn\fi
    \@turn
    \if@white\else\advance\move-1\fi
    \advance\@commlevel1
    \if@skaking
        \expandafter\restoregame\expandafter{\@roman{\the\@commlevel}comm-game}%
        \if@diagnostics\message{Restored position for comment at level 
            \the\@commlevel}\fi
    \else{} \fi
    \csname\@roman{\the\@commlevel}font\endcsname
    \csname\@roman{\the\@commlevel}#1\endcsname
    \expandafter\let\expandafter\tm@var\csname tm@var#1\endcsname
    \expandafter\let\expandafter\tm@vars\csname tm@var#1s\endcsname
    \expandafter\let\expandafter\@preparevar\csname @preparevar#1\endcsname
    \expandafter\let\expandafter\@finvar\csname @finvar#1\endcsname
    \def\result##1{ ##1}%
    \@execute}
\def\@closecomm#1{\csname\@roman{\the\@commlevel}#1\endcsname
    \egroup
    \if@skaking
        \expandafter\restoregame\expandafter{\@roman{\the\@commlevel}comm-game}%
        \if@diagnostics\message{Restored position 
            after comment at level \the\@commlevel.}\fi
    \else{} \fi}
\def\steplevel{\advance\@commlevel1\relax\csname
    \@roman{\the\@commlevel}font\endcsname}
\def\backlevel{\advance\@commlevel-1\relax
    \ifnum\@commlevel<1\relax\@commlevel1\relax\fi
    \csname\@roman{\the\@commlevel}font\endcsname}
\def\@turn{\@delimitedtrue\if@white\@whitefalse\else\@whitetrue\fi}
\def\@@turn{%
    \tm@aftermove
    \global\let\tm@aftermove\relax
    \if@skaking
        \@tempcnta\@commlevel\advance\@tempcnta1\relax
        \expandafter\storegame\expandafter{\@roman{\the\@tempcnta}comm-game}%
        \@tempcnta\move\advance\@tempcnta-1\relax
        \edef\tm@tomainline{\noexpand{\if@white\the\move
            \else\the\@tempcnta..\fi.\the\@tmtoskak}}%
        \if@diagnostics\message{Stored position at level 
            \the\@commlevel+1, before \tm@tomainline}\fi
        \expandafter\hidemoves\tm@tomainline
        \@tmtoskak{}%
    \fi
    \if@delimited\else\@turn\fi}
%    \end{macrocode}
%
%\section{Fonts and contexts}
%
%    \begin{macrocode}
\let\ifont\bfseries
\let\iifont\normalfont
\let\iiifont\normalfont
\let\ivfont\itshape
\let\varfont\bfseries
\newcommand*\iopen{}\newcommand*\iclose{}
\newcommand\iiopen{[}\newcommand\iiclose{\leavevmode\unskip]}
\newcommand\iiiopen{(}\newcommand\iiiclose{\leavevmode\unskip)}
\newcommand\ivopen{(}\newcommand\ivclose{\leavevmode\unskip)}
\newcommand*\iiopent{}\newcommand*\iicloset{}
\newcommand*\iiiopent{}\newcommand*\iiicloset{}
\newcommand*\ivopent{}\newcommand*\ivcloset{}
\def\afterno{.~}
\def\afterw{\ }
\def\afterb{\ }
\def\beforeb{\the\move\dots}
\def\beforeno{}
%    \end{macrocode}
%\section{Commentary tools}
%
%    \begin{macrocode}
\long\def\dummy{\PackageWarning{TeXmate}{Ignoring moves for 
        skak after \string\dummy}%
    \@skakingfalse
    \@turn
    \if@white\advance\move1\relax\fi}
\long\def\ddummy{\PackageWarning{TeXmate}{Ignoring moves for 
        skak after \string\ddummy}%
        \@skakingfalse
        \advance\move1\relax}
\def\black{\if@skaking
    \PackageWarning{TeXmate}{Ignoring moves for skak. 
        \string\black command is now almost obsolete}%
        \@skakingfalse
    \fi
    \@whitefalse\@execute}
\def\white{\if@skaking
    \PackageWarning{TeXmate}{Ignoring moves for skak. 
        \string\white command is now almost obsolete}%
        \@skakingfalse
    \fi
    \@whitetrue\@execute}
\def\ahead{%
    \if@skaking
        \expandafter\hidemoves\tm@tomainline
    \fi
    \@turn
    \if@white\advance\move1\relax\fi    
    \@execute}
\def\threat#1{\bgroup\@skakingfalse\ifcase\@commlevel\or
    \iifont\or\iiifont\or\ivfont\or\ivfont\fi\
    \catcode`\>\active\withidea\@@piece}
\long\def\Threat#1{\bgroup
    \@skakingfalse
    \@resumingtrue
    \ifcase\@commlevel\or
    \iifont\or\iiifont\or\ivfont\or\ivfont\fi
    \catcode`\>\active
    \if@delimited\else\@turn\fi
    \@turn
    \if@white\advance\move1\relax\fi
    \@execute}
%    \end{macrocode}
%
%\section{Variations environments}
%
%   \begin{macrocode}
\newcommand*\var{\relax}
\newenvironment{variations}{%
    \renewcommand\var{\@ifstar{\tm@vars}{\tm@var}}%
    \if@skaking
        \null\expandafter\storegame\expandafter
            {\@roman{\the\@commlevel}comm-var}\leavevmode\unskip
        \if@diagnostics
            \message{Stored position for variations at level \the\@commlevel.}%
        \fi
    \fi
    \@preparevar\ignorespaces}%
    {\@finvar}
\newenvironment{variations*}{%
    \renewcommand\var{\tm@vars}%
    \if@skaking
        \null\expandafter\storegame\expandafter
            {\@roman{\the\@commlevel}comm-var}\leavevmode\unskip
        \if@diagnostics
            \message{Stored position for variations at level \the\@commlevel.}%
        \fi
    \fi
    \@preparevaropen\ignorespaces}
    {\@finvaropen}
\def\tm@varopen{\@semicolon\egroup
    \if@skaking
        \null\expandafter\restoregame\expandafter
            {\@roman{\the\@commlevel}comm-var}\leavevmode\unskip
        \if@diagnostics
            \message{Restored position for variation at level \the\@commlevel.}%
        \fi
    \fi
    \bgroup\varfont\def\tm@aftermove{\backlevel\steplevel}%
    \@execute}
\def\tm@varopens{\egroup
    \if@skaking
        \null\expandafter\restoregame\expandafter
            {\@roman{\the\@commlevel}comm-var}\leavevmode\unskip
        \if@diagnostics
            \message{Restored position for variation at level \the\@commlevel.}%
        \fi
    \fi
    \bgroup\def\tm@aftermove{\backlevel\steplevel}%
    \@execute}
\def\tm@varopent{\egroup
    \item
    \bgroup
    \if@skaking
        \expandafter\restoregame\expandafter{\@roman{\the\@commlevel}comm-var}%
        \if@diagnostics\message{Restored position for variation at level 
            \the\@commlevel.}\fi
    \fi
    \@execute}
\let\tm@varopents\tm@varopens
\let\tm@var\tm@varopen
\let\tm@vars\tm@varopens
\def\@preparevaropen{\bgroup\let\@semicolon\relax}
\let\@preparevar\@preparevaropen
\let\@finvaropen\egroup
\let\@finvar\@finvaropen
\newcommand\VariationsEnvironment[2]{%
    \def\@preparevaropent{#1\bgroup}\def\@finvaropent{\egroup#2}}
\VariationsEnvironment{\begin{itemize}}{\end{itemize}}
%    \end{macrocode}
%
%\section{Diagram information}
%
%    \begin{macrocode}
\newif\if@turnright
\newif\if@movebottom
\newif\if@turnleft
\newif\if@numbertop
\newif\if@defaultturnright
\newif\if@defaultmovebottom
\newif\if@defaultnumbertop
\newif\if@defaultturnleft
\newcommand*\TheDiagram{\textit{\small\bfseries\arabic{diagram}}}
\newcommand*\diagramnames{\renewcommand*\diagramtop{\textbf{\bname}}%
    \renewcommand*\diagrambottom{\@name}%
    \@restorediagramdefaults}
\newcommand*\topdiagramnames{\renewcommand*\diagramtop{\textbf{\wname--\bname}}%
    \renewcommand*\diagrambottom{}%
    \@restorediagramdefaults}
\newcommand*\bottomdiagramnames{\renewcommand*\diagramtop{}%
    \renewcommand*\diagrambottom{\textbf{\wname--\bname}}%
    \@restorediagramdefaults}
\newcommand*\nodiagramnames{%
    \global\let\@diagramtop\relax
    \global\let\@diagrambottom\relax}
\newcommand*\whiteturnmarker{\raisebox{.75\expandafter
    \ht\csname chessdiag\@roman\@tempcnta\endcsname}{\textsl{W}}\ }
\newcommand*\blackturnmarker{\raisebox{.75\expandafter
    \ht\csname chessdiag\@roman\@tempcnta\endcsname}{\textsl{B}}\ }
\newcommand*\diagramnumber{\@numbertoptrue}
\newcommand*\nodiagramnumber{\@numbertopfalse}
\newcommand*\leftdiagramturn{\@turnlefttrue\@turnrightfalse}
\newcommand*\rightdiagramturn{\@turnrighttrue\@turnleffalse}
\newcommand*\nodiagramturn{\@turnleftfalse\@turnrightfalse}
\newcommand*\diagrammove{\@movebottomtrue}
\newcommand*\nodiagrammove{\@movebottomfalse}
\newcommand*\nextdiagramtop[1]{\renewcommand*\@diagramtop{#1}}
\newcommand*\nextdiagrambottom[1]{\renewcommand*\@diagrambottom{#1}}
\newcommand\@restorediagramdefaults{%
    \global\let\@diagramtop\diagramtop
    \global\let\@diagrambottom\diagrambottom
    \global\let\if@turnright\if@defaultturnright
    \global\let\if@movebottom\if@defaultmovebottom
    \global\let\if@numbertop\if@defaultnumbertop
    \global\let\if@turnleft\if@defaultturnleft}
\@turnleftfalse
\@movebottomtrue
\@turnrightfalse
\@defaultmovebottomtrue
\@defaultturnrightfalse
\@defaultturnleftfalse
\@defaultnumbertopfalse
\@restorediagramdefaults
\let\makediagramsfont\small
\newcommand*\analysistop{Analysis}
\newcommand*\diagramtop{\textbf{\bname}}
\newcommand*\diagrambottom{\textbf{\wname}}
\newcommand*\diagramsign{~{\mdseries(\textit{D})}}
\let\@diagramtop\diagramtop
\let\@diagrambottom\diagrambottom
%    \end{macrocode}
%
%\section{Diagram handling}
%
%    \begin{macrocode}
\newcount\@diagramsbuilt
\newcommand*\DiagramCache[1]{%
    \@tempcnta#1
    \@whilenum\@tempcnta>\MaxDiagramCache\do{%
        \expandafter\newbox\csname chessdiag\@roman\@tempcnta\endcsname
        \expandafter\newbox\csname chessdiag\@roman\@tempcnta top\endcsname
        \expandafter\newbox\csname chessdiag\@roman\@tempcnta bottom\endcsname
        \expandafter\newbox\csname chessdiag\@roman\@tempcnta move\endcsname
        \@namedef{chessdiag\@roman\@tempcnta turn}{}%
        \advance\@tempcnta-1\relax}%
    \ifnum#1>\MaxDiagramCache
        \def\MaxDiagramCache{#1}%
    \fi
    }
\def\MaxDiagramCache{0}
\DiagramCache3
\newcounter{diagram}
\newcommand*\makediagrams[1][\@diagramsbuilt]{%
    \noindent\null\hfill
    {\makediagramsfont
    \@tempcnta0
    \@whilenum\@tempcnta<#1\do{%
        \advance\@tempcnta1\relax
        \refstepcounter{diagram}%
        \quad\shortstack{%
            \if@numbertop\TheDiagram\\\fi
            \ifnum\expandafter\wd\csname 
                    chessdiag\expandafter\@roman\@tempcnta top\endcsname>0\relax
                \makebox[\expandafter\wd\csname 
                    chessdiag\expandafter\@roman\@tempcnta\endcsname][c]{%
                    \expandafter\usebox\csname 
                        chessdiag\expandafter\@roman\@tempcnta top\endcsname}%
                \\
            \fi
            \if@turnleft
                \makebox[0pt][r]{\csname 
                    chessdiag\@roman\@tempcnta turn\endcsname}%
            \fi
            \expandafter\usebox\csname 
                chessdiag\expandafter\@roman\@tempcnta\endcsname
            \if@turnright
                \makebox[0pt][l]{\csname 
                    chessdiag\@roman\@tempcnta turn\endcsname}%
            \fi
            \\
            \expandafter\usebox
                \csname chessdiag\expandafter\@roman\@tempcnta bottom\endcsname
            \if@movebottom
            \ifnum\expandafter\wd\csname 
                    chessdiag\expandafter\@roman\@tempcnta move\endcsname>0
                \ifnum\expandafter\wd\csname 
                    chessdiag\expandafter\@roman\@tempcnta bottom\endcsname>0
                    \\
                \fi\expandafter\usebox
                    \csname chessdiag\expandafter\@roman\@tempcnta move\endcsname
            \fi\fi}%
        \hfill\quad}}%
    \@killdiagrams{#1}%
    }
\newcommand*\@killdiagrams[1]{%
    \global\advance\@diagramsbuilt-#1\relax
    \ifnum\@diagramsbuilt>0\relax
        \@tempcnta0\relax
        \@tempcntb#1\relax
        \@whilenum\@tempcnta<\@diagramsbuilt\do{%
            \advance\@tempcnta1\relax
            \advance\@tempcntb1\relax
            \expandafter\global\expandafter\sbox
                \csname chessdiag\expandafter\@roman\@tempcnta\endcsname{%
                    \expandafter\usebox\csname 
                        chessdiag\@roman\@tempcntb\endcsname}%
            \expandafter\global\expandafter\sbox
                \csname chessdiag\expandafter\@roman\@tempcnta top\endcsname{%
                    \expandafter\usebox\csname 
                        chessdiag\@roman\@tempcntb top\endcsname}%
            \expandafter\global\expandafter\sbox
                \csname chessdiag\expandafter\@roman\@tempcnta bottom\endcsname{%
                    \expandafter\usebox\csname 
                        chessdiag\@roman\@tempcntb bottom\endcsname}%
            \expandafter\global\expandafter\sbox
                \csname chessdiag\expandafter\@roman\@tempcnta move\endcsname{%
                    \expandafter\usebox\csname 
                        chessdiag\@roman\@tempcntb move\endcsname}%
            }%
    \fi
    }
\newcommand*\preparediagram[2]{%
    \global\advance\@diagramsbuilt1\relax
    \expandafter\global\expandafter
        \sbox\csname chessdiag\@roman\the\@diagramsbuilt\endcsname{\showboard}%
    \expandafter\global\expandafter
        \sbox\csname chessdiag\@roman\the\@diagramsbuilt 
            top\endcsname{\makediagramsfont#1}%
    \expandafter\global\expandafter
        \sbox\csname chessdiag\@roman\the\@diagramsbuilt 
            bottom\endcsname{\makediagramsfont#2}%
    \expandafter\global\expandafter
        \sbox\csname chessdiag\@roman\the\@diagramsbuilt 
            move\endcsname{}%
    \if@white 
        \expandafter\let\csname chessdiag\@roman\@diagramsbuilt 
            turn\endcsname\whiteturnmarker
    \else
        \expandafter\let\csname chessdiag\@roman\@diagramsbuilt 
            turn\endcsname\blackturnmarker
    \fi
    }
\newcommand*\@toD[1]{\if@delimited\else\@@turn\fi
    \global\advance\@diagramsbuilt1\relax
    \expandafter\global\expandafter
        \sbox\csname chessdiag\expandafter
            \@roman\@diagramsbuilt\endcsname{\showboard}%
    \expandafter\global\expandafter
        \sbox\csname chessdiag\expandafter\@roman\@diagramsbuilt 
            move\endcsname{%
        \mdseries\makediagramsfont\strut\@skakingfalse
            \@turn
            \if@white
                \the\move\afterno
            \else
                \advance\move-1\relax
                \beforeb
            \fi
            \@@piece#1\relax}%
    \expandafter\global\expandafter
        \sbox\csname chessdiag\expandafter\@roman\@diagramsbuilt 
            top\endcsname{\makediagramsfont\@diagramtop}%
    \expandafter\global\expandafter
        \sbox\csname chessdiag\expandafter\@roman\@diagramsbuilt 
            bottom\endcsname{\makediagramsfont\@diagrambottom}%
    \if@white 
        \expandafter\let\csname ifwfordiag\expandafter
            \@roman\@diagramsbuilt\endcsname\whiteturnmarker
    \else
        \expandafter\let\csname ifwfordiag\expandafter
            \@roman\@diagramsbuilt\endcsname\blackturnmarker
    \fi
    \@restorediagramdefaults
    }
%    \end{macrocode}
%
%\section{Position setup}
%
%    \begin{macrocode}
\newcount\@squarecount
\newif\if@blacksq
\newcommand\diagram[2][w 1]{%
    \position[#1]{#2}%
    \showboard}
\if@skakon
    \let\skak@fenboard\fenboard
    \renewcommand*\fenboard[1]{\tm@fenboard#1.}
    \newcommand*\toD{\if@delimited\else\@@turn\fi\@ifstar{\@toD}{\diagramsign\@toD}}
    \newcommand\position[2][w 1]{%
        \@squarecount8\relax
        \def\tm@tofen{}%
        \@convertdiagram#2.%
        \@completefen#1.%
        \expandafter\skak@fenboard\tm@tofen%
        }
    \def\@convertdiagram#1{%
        \let\next\@convertdiagram
        \ifx#1.
            \let\next\relax
            \ifnum\@squarecount>0 \edef\tm@tofen{\tm@tofen\the\@squarecount}\fi
        \else
        \ifx#1/%
            \edef\tm@tofen{\tm@tofen\ifnum\@squarecount>0 \the\@squarecount\fi/}%
            \@squarecount8\relax
        \else
            \ifcat1#1%
                \edef\tm@tofen{\tm@tofen#1}%
                \advance\@squarecount-#1%
            \else
                \edef\tm@tofen{\tm@tofen#1}%
                \advance\@squarecount-1\relax
            \fi
        \fi\fi
        \next}
    \def\@completefen#1 #2.{%
        \edef\tm@tofen{{\tm@tofen\space #1 KQkq - 0 #2}}%
        \move#2\relax
        \ifx#1w\@whitetrue\else\@whitefalse\fi}
    \def\tm@fenboard#1 #2 #3 #4 #5 #6.{%
        \@squarecount8\relax
        \def\tm@tofen{}%
        \@convertdiagram#1.%
        \edef\tm@tofen{{\tm@tofen\space #2 #3 #4 #5 #6}}%
        \expandafter\skak@fenboard\tm@tofen
        \move#6\relax
        \ifx#2w\@whitetrue\else\@whitefalse\fi
        }
\else
    \newcommand*\toD{\let\toD\relax
        \PackageError{TeXmate}{\string\toD\space requires skak. All occurrences ignored}{%
        Go on, diagrams will not be automatically generated.}}%
    \let\showboard\relax
    \newcommand\position[2][w 1]{%
        \@blacksqfalse
        \@squarecount8\relax
        \bgroup
        \nointerlineskip
        \boardfont
        \noindent
        \setlength\fboxsep{.6pt}%
        \expandafter\fbox{\parbox{8\len@cfss@squarewidth}{\@diagline#2.}}\egroup}
    \def\@diagpiece#1{\def\temp{#1}%
            \ifx\temp\@pawn\csname BlackPawnOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@rook\csname BlackRookOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@knight\csname BlackKnightOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@bishop\csname BlackBishopOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@queen\csname BlackQueenOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@king\csname BlackKingOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@Pawn\csname WhitePawnOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@Rook\csname WhiteRookOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@Knight\csname WhiteKnightOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@Bishop\csname WhiteBishopOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@Queen\csname WhiteQueenOn\if@blacksq Black\else White\fi\endcsname\else
            \ifx\temp\@King\csname WhiteKingOn\if@blacksq Black\else White\fi\endcsname\else
            \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
            }
    \def\@diagline#1{\@tempcnta0\relax\let\next\@diagline
        \ifx#1.\let\next\relax\@dospaces{\@squarecount}%
        \else\ifx#1/\def\next{\@dospaces{\@squarecount}%
            \newline\@togglesq\@squarecount8\relax
            \@diagline}%
        \else\ifcat1#1\@dospaces{#1}%
        \else\@diagpiece#1\advance\@squarecount-1\relax\@togglesq
        \fi\fi\fi
        \next}
    \def\@dospaces#1{\null\ifnum#1>0\relax
        \csname\if@blacksq Black\else White\fi EmptySquare\endcsname
        \@togglesq\@tempcntb#1\relax\advance\@tempcnta1\relax\advance\@squarecount-1\relax
        \ifnum\@tempcnta<\@tempcntb\let\@next\@dospaces\else
            \let\@next\@gobble\fi
        \@next\@tempcntb\fi}
    \def\@togglesq{\if@blacksq\@blacksqfalse\else\@blacksqtrue\fi}
    \newcommand*\fenboard[1]{\tm@fenboard#1.}
    \def\tm@fenboard#1 #2 #3 #4 #5 #6.{%
        \position[#2 #6]{#1}}
\fi
\let\fenposition\fenboard
\let\drawdiagram\preparediagram
%    \end{macrocode}
%
%\section{Game mark-up}
%
%    \begin{macrocode}
\def\wname{}
\def\bname{}
\let\@welo\relax
\let\@belo\relax
\let\@tourn\relax
\let\@opening\relax
\let\@eco\relax
\newcommand\whitename[1]{\def\wname{#1}}
\newcommand\blackname[1]{\def\bname{#1}}
\newcommand*\whiteelo[1]{\def\@welo{(#1)}}
\newcommand*\blackelo[1]{\def\@belo{(#1)}}
\newcommand*\chessevent[1]{\def\@tourn{#1}}
\newcommand*\chessopening[1]{\def\@opening{#1}}
\newcommand*\ECO[1]{\def\@eco{\ -- \textbf{#1}}}
\newcommand*\makegametitle{\bigskip\newgame
    \noindent\hrule\nopagebreak\smallskip
    \noindent\strut$\Box$\quad\textbf{\wname}\ \@welo\hfill\@tourn\nopagebreak\\
    \noindent\strut$\blacksquare$\quad\textbf{\bname}\ \@belo\hfill\@opening\@eco
    \noindent\hrule\nopagebreak\medskip\nopagebreak
    }
\newcommand*\result[1]{{\unskip\nobreak\hfil\penalty50
    \qquad\null\nobreak\hfill\textbf{#1}%
    \parfillskip0pt \finalhyphendemerits0 \par}}
\newcommand\whitewins{\result{1\,:\,0}}
\newcommand\blackwins{\result{0\,:\,1}}
\newcommand\drawn{\result{1/2\,:\,1/2}}
\newcommand\resigns{\if@delimited\else\@@turn\fi
    \if@white\blackwins\else\whitewins\fi}
\if@skakon
    \let\skak@newgame\newgame
\else
    \let\skak@newgame\relax
\fi
\def\newgame{\@whitetrue\@resumingtrue\@commlevel2\move1\@delimitedtrue
    \skak@newgame}
\newgame
%    \end{macrocode}
%
%\section{Miscellaneous}
%
%    \begin{macrocode}
\def\SkakOn{\global\@skakingtrue}
\def\SkakOff{\global\@skakingfalse} 
\setboardfontsize{12pt} 
\DeclareRobustCommand\BlackRookOnWhite{\cfss@boardsymbol{\cfss@BlackRookOnWhite}}
\let\checksign\checksymbol
\let\wBetter\wupperhand
\let\bBetter\bupperhand
\let\WBetter\wdecisive
\let\BBetter\bdecisive
\let\development\devadvantage
\let\spaceadv\moreroom
\let\attack\withattack
\let\initiative\withinit
\let\boardfile\file
\let\boarddiagonal\diagonal
\let\boardcenter\centre
\let\bishops\bishoppair
\let\oppositebishops\opposbishops
\let\separatedpawns\seppawns
\let\doubledpawns\doublepawns
\let\pawnsno\morepawns
\let\timetrouble\timelimit
\let\chessetc\etc
%    \end{macrocode}
%</package>
