%   These are two TeX macros which may be useful for writing about
% chess.  \bore creates a chess diagram from Forsyth notation, with
% Z used as a terminator.  For example,
% 
%   $$\bore r1bqkbnr pppp1ppp 2n5 1B2p3 4P3 5N2 PPPP1PPP RNBQK2R Z$$
%
% shows the Ruy Lopez after white's third move.
%
% \VAR (number) (moves separated by spaces) Z may be used to write
% a variation.  The macro inserts the move numbers, starting with 
% (number).  For example,
%
%     Continuing from the diagram above, \VAR 3 \dots{} a6 Ba4 Nf6 O--O
%     Be7 Re1 b5 Bb3 O-O c3 d5 Z is the Marshall gambit.
%
%  Note the \dots{} to indicate that Black made the first move.
%
%  The diagrams created by \bore have pieces made up of a small number
% of rectangles.  Much nicer diagrams and more powerful macros were
% created by Piet Tutelaers, available  by anonymous ftp
% at sol.cs.ruu.nl (131.211.80.5).  However, the ones offered here
% do not require additional fonts, and may be easier to use.
%
%   I authorize circulation of these macros by ftp sites and similar
% nonprofit media.  Improvements are welcome.  An earlier version of
% the macros (with letters for pieces) was done several years ago, 
% with substantial modifications by Steve Cochran.
%
%   Charles Blair  (ceblair@ux1.cso.uiuc.edu)
%    macros start below--- you may cut here --------------------------
%
% \fun is the fundamental length unit for the diagrams.  If you
% replace 1.2pt by something else, the scale is changed.
% The box rf is not used, except to provide something with which to
% refer to \fun.
%
\newdimen\fun \dimen\fun=1.2pt
\newbox\rf \setbox\rf=\vbox to \dimen\fun{
\hrule height \dimen\fun width \dimen\fun}
%
\def\oil{\offinterlineskip} % I needed to use this in all my vboxes
%
%   Black pieces are made from solid boxes of specified height and
% width (the \filbox macro).  For White pieces, \filbox is replaced by a
% macro (\blbox) which draws the lines around the border of such a box.
%   (the white knight is an exception to this)
% The macro \tbo is defined as \blbox or \filbox as needed.
%
\def\filbox#1#2{\vbox{\oil \hrule height #1\ht\rf width #2\ht\rf}}
%
\newdimen\adjust 
\def\blbox#1#2{\dimen\adjust=#1\ht\rf \advance\dimen\adjust by-0.4\ht\rf
\vbox{\oil \hrule height 0.2\ht\rf width #2\wd\rf
\hbox to #2\wd\rf{\vrule height \dimen\adjust width 0.2\wd\rf\hfil
\vrule height \dimen\adjust width 0.2\wd\rf}
\hrule height 0.2\ht\rf width #2\wd\rf}}
%
%  Each piece is 12 units wide.  \scen produces an hbox with this width
%  and either a \blbox or a \filbox in the center.
%
\def\scen#1#2{\hbox to 12\wd\rf{\hfil\tbo{#1}{#2}\hfil}}
%
%  Definitions (color independent) of each piece
%
\def\pawn{\vbox{\oil \scen{2.5}{4}\scen{3}{8}\scen{4.5}{4}\scen{3}{10}}}
%
\def\bishop{\vbox{\oil \scen{2}{2.5}\scen{3}{8}\scen{4}{10}%
\scen{3}{6}\scen{1.5}{12}}}
%
\def\rook{\vbox{\oil
\hbox to 12\wd\rf{\tbo2{2.5}\hfil\tbo2{2.5}\hfil\tbo2{2.5}}
\scen{3}{12}\scen{5.5}{7}\scen{3}{12}}}
%
\def\queen{\vbox{\oil \hbox to 12\wd\rf{\tbo{6}{2}\hfil\tbo{8}{2}%
\hfil\tbo{8}{2}\hfil\tbo{6}{2}}\scen{4}{10}}}
%
\def\king{\vbox{\oil%  Next 4 lines are the cross part (improvement wanted)
\hbox to 12\wd\rf{\rlap%
{\hbox to 12\wd\rf{\hfil\vrule height 3\ht\rf width 0.2\wd\rf\hfil}}%
\hfil\raise 1.5\ht\rf%
\hbox{\vrule width 3\ht\rf height 0.2\ht\rf}\hfil}
\hbox to 12\wd\rf{\tbo{6}{4.5}\hfil\tbo{6}{4.5}}
\scen{4}{10}}}
%
\def\knight{\vbox{\oil \hbox{\hskip 7\wd\rf\tbo{1}{1}}
\hbox{\hskip2\wd\rf\tbo{2.5}{7}}\hbox{\hskip 2\wd\rf
\vbox to  3\ht\rf{\tbo{1}{1.5}\vfil}\hskip3\wd\rf\tbo{3}{2.5}}
\hbox{\hskip 3.4\wd\rf\tbo{4}{5.6}}
\tbo{2.5}{12}}}
%
%    Creation of the boxes for the pieces
%
\newbox\WP \def\tbo#1#2{\blbox{#1}{#2}} \setbox\WP=\pawn
\newbox\BP \def\tbo#1#2{\filbox{#1}{#2}} \setbox\BP=\pawn
\newbox\WB \def\tbo#1#2{\blbox{#1}{#2}} \setbox\WB=\bishop
\newbox\BB \def\tbo#1#2{\filbox{#1}{#2}} \setbox\BB=\bishop
\newbox\WR \def\tbo#1#2{\blbox{#1}{#2}} \setbox\WR=\rook
\newbox\BR \def\tbo#1#2{\filbox{#1}{#2}} \setbox\BR=\rook
\newbox\WQ \def\tbo#1#2{\blbox{#1}{#2}} \setbox\WQ=\queen
\newbox\BQ \def\tbo#1#2{\filbox{#1}{#2}} \setbox\BQ=\queen
\newbox\WK \def\tbo#1#2{\blbox{#1}{#2}} \setbox\WK=\king
\newbox\BK \def\tbo#1#2{\filbox{#1}{#2}} \setbox\BK=\king
\newbox\BN \def\tbo#1#2{\filbox{#1}{#2}} \setbox\BN=\knight
%
%  The exceptional white knight (a lot of work to avoid a line at neck!)
%
\newbox\WN \def\tbo#1#2{\blbox{#1}{#2}} \setbox\WN=
\vbox{\oil \hbox{\hskip 7\wd\rf\tbo{1}{1}}
\hbox{\hskip2\wd\rf\vrule height 2.5\ht\rf width 0.2\ht\rf
\vbox to 2.5\ht\rf{\hrule height 0.2\ht\rf width 6.6\wd\rf\vfil
\hrule height 0.2\ht\rf width 4.5\wd\rf}%
\vrule height 2.5\ht\rf width 0.2\wd\rf}
\hbox{\hskip 2\wd\rf
\vbox to  3\ht\rf{\tbo{1}{1.5}\vfil}\hskip3\wd\rf
\hbox to 2.5\wd\rf{\vrule height 3\ht\rf width 0.2\wd\rf\hfil
\vrule height 3\ht\rf width 0.2\wd\rf}}
\hbox{\hskip 3.4\wd\rf\tbo{4}{5.6}}
\tbo{2.5}{12}}
%
%   \ver is the line used for square boundaries on the board
%   \nver is thinner line used to shade dark squares (if no piece on them)
%   \hor draws horizontal lines for each rank
%
\def\ver{\vrule height 15\ht\rf width 0.4\wd\rf}
\def\nver{\vrule height 15\ht\rf width 0.1\wd\rf}
\def\hor{\hrule height 0.4\wd\rf width 120\wd\rf}
%
 \newcount\color \newcount\blanks
\def\blanksquare{\ifodd\color \hbox to 15\wd\rf{\hfil\nver%
\hfil\nver\hfil\ver}\else\hbox to 15\wd\rf{\hfil\ver}\fi
\advance\color by 1}
%
% Each piece is centered inside a 15-unit square
%
\def\cen#1{\hbox to 15\wd\rf{\hfil\vbox to 15\ht\rf{\vfil\copy#1\vfil}%
\hfil\ver}}
%
% This processes the characters specifying a row of the diagram.  It
% is patterned after the \length example at the end of chapter 20
% of The TeXbook
\def\nextchar#1{\let\nex=\nextchar%
\ifcat7#1%
\blanks=#1\loop\ifnum\blanks>0 \blanksquare%
\advance\blanks by -1\repeat\else
\advance\color by 1%
\ifx#1P\cen{\WP}\else\ifx#1p\cen{\BP}\else%
\ifx#1N\cen{\WN}\else\ifx#1n\cen{\BN}\else%
\ifx#1B\cen{\WB}\else\ifx#1b\cen{\BB}\else%
\ifx#1Q\cen{\WQ}\else\ifx#1q\cen{\BQ}\else%
\ifx#1R\cen{\WR}\else\ifx#1r\cen{\BR}%
\else\ifx#1K\cen{\WK}\else\ifx#1k\cen{\BK}\else\let\nex=\relax%
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\nex}
%
%  \bore carries out preliminary steps for making the diagram.
%  \rgc reads in a string of non-blank characters, checks that the
%  first character is not the terminator Z, gives the string to \nextchar,
%  and then processes the next string.  (again patterned after \length)
%
\def\bore#1Z{\color=1\vbox{\oil \hor\rgc#1 Z }}
\def\rgc#1#2 {\advance\color by 1\let\nx=\rgc\ifx#1Z\let\nx=\relax
\else\hbox{\ver\nextchar#1#2Z}\hor\fi\nx}
%
%  This macro prints a variation.  It is supposed to use the ideas of
% \length and seems to work, but there is something I don't understand:
%  if the {} following the \else is deleted, something goes wrong.  Why?
%
\newcount\move \newcount\iswhite
\def\VAR#1 #2Z{\move=#1\iswhite=1\listem #2 Z }
\def\listem#1#2 {\ifx#1Z\let\nx=\relax\else{}%
\ifodd\iswhite{} \number\move~#1#2\else%
~#1#2\advance\move by 1\fi\advance\iswhite by 1\let\nx=\listem\fi\nx}
