%%
%% WARMreader.sty v1.2
%% 5 July 2001
%%
%% by Ross Moore, <ross@mpce.mq.edu.au> 
%%

\edef\wrmprevat{\the\catcode`\@}
\catcode`\@=11

%%
%%  If you have trouble finding files,
%%  then we may need to know the directory delimiter;
%%  so uncomment one of the following lines...
%%
\ifx\undefined\@currdir
  % ... get it from LaTeX's config file:
% \input texsys.cfg
 \ifx\undefined\@currdir
  % ... or specify it directly:
  \def\@currdir{}% ... or try winging-it without
% \def\@currdir{:}%  for Macintosh 
% \def\@currdir{:}%  for Unix
 \fi
\fi

\ifx\undefined\codeof {\catcode`\:=12
 \gdef\codeof#1{\expandafter\codeof@\meaning#1<-:}
 \gdef\codeof@#1:->#2<-:{#2}%
}\fi

\newread\wrmread
\newread\wrm@write
\let\wrm@next=\relax
\def\wrm@empty{}%
\def\maxwrmlines{20}
\let\wrm@tmp\relax
\def\EndLinePattern{!;!}

\let\wrm@quiet\relax
\def\wrmquiet{\def\wrm@quiet{}}
\def\wrmverbose{\let\wrm@quiet\relax}

\newcount\wrmcounter
\newcount\wrmdatacounter
\newif\ifmorelines\morelinestrue

\newwrite\wrm@tmpfile
\def\wrm@recovercode{%
  \immediate\openout\wrm@tmpfile=\jobname.wrm
  \relax\immediate\write\wrm@tmpfile{{\codeof\wrm@text}}\relax
  \immediate\closeout\wrm@tmpfile\relax
  \immediate\openin\wrmread=\jobname.wrm
  \read\wrmread to \wrm@text
  \immediate\closein\wrmread
  \expandafter\expandafter\expandafter\def
  \expandafter\expandafter\expandafter\wrm@text
  \expandafter\expandafter\expandafter{\expandafter
   \strip@endline\wrm@text \strip@endline}%
}

\let\WARM@PreDataString=\relax
\let\WARM@EndFileString=\relax

\def\wrm@defprefix{datapoint}
\let\wrm@prefix=\wrm@defprefix
\def\WARMprefix#1{\def\wrm@prefix{#1}\WARM@prefixMessage\ignorespaces}
\def\WARM@prefixMessage{%
 \wrm@message{Using prefix '\wrm@prefix' for unlabelled points.}}

\def\wrm@message#1{{\let\protect=\noexpand \let\\\relax \endlinechar=10
 \immediate\write16{#1}}}%

\def\WARMprocess#1#2#3{%
 \WARMprocessDir{\@currdir}{#1}{#2}{#3}}%

\def\WARMprocessDir#1#2#3#4#5{\begingroup
 \begingroup
  \wrmdatacounter=\z@ \let\wrm@llx=\undefined
% \def\CleanupAfterDir{\endgroup}%
 \def\fileprefix{#2}%
 \def\fileextn{#3}%
 \def\dataextn{#4}%
 \ifx\dataextn\wrm@empty
 \else
  \WARM@RelaxOpenFileMessage
  \edef\wrm@filename{#1\fileprefix.\dataextn}%
  \TryToOpenTheFile\relax
 \fi
 \ifnum\wrmdatacounter<\z@
  \aftergroup\WARM@RelaxOpenFileMessage
  \aftergroup\WARM@RelaxBBWrite
  \aftergroup\WARM@ReadDataFromFile
 \else
  \edef\wrm@filename{\@currdir\fileprefix.\fileextn}%
  \TryToOpenTheFile\relax
  \ifnum\wrmdatacounter<\z@
   \aftergroup\WARM@ReadDataToFile
  \else
   \aftergroup\WARM@NoFilesError
 \fi\fi
 \endgroup
 {#1#2}{#3}{#4}{#5}\CleanupAfterDir
 \def\wrm@filename{#1#2.#3}%
}
\def\CleanupAfterDir{%
 \ifx\undefined\wrm@llx\expandafter\endgroup
 \else\expandafter\saveBBafterFile\fi}

\def\WARM@NoFilesError#1#2#3#4{%
 \message{Cannot open file #1.#2 nor #1.#3 }}

\def\WARM@ReadDataFromFile#1#2#3#4{%
 \def\wrm@filename{#1.#3}%
 \expandafter\ProcessFile\expandafter{\wrm@filename}{#4}%
}

\def\WARM@ReadDataToFile#1#2#3#4{%
 \begingroup
  \def\wrm@filename{#1.#3}%
  \immediate\openout\wrm@write=\wrm@filename
  \def\wrm@filename{#1.#2}%
  \expandafter\ProcessFile\expandafter{\wrm@filename}{#4}%
  \immediate\closeout\wrm@write
 \endgroup
 \WARM@ReadDataFromFile{#1}{#2}{#3}{#4}%
}


\def\TryToOpenTheFile{%
 \immediate\openin\wrmread\wrm@filename\relax
 \ifeof\wrmread
  \expandafter\TryAgainToOpenTheFile
 \else
  \read\wrmread to\ThisFileLine
  \expandafter\FileIsOpen
 \fi
}

\def\ReadLineFromFile{%
 \ifeof\wrmread \morelinesfalse
 \else \read\wrmread to\ThisFileLine
 \fi
 {\ifx\ThisFileLine\wrm@empty
   \ifmorelines\aftergroup\ReadLineFromFile\fi\fi}%
}
\def\CloseTheFile#1{\immediate\closein\wrmread}

\def\FileIsOpen{\ResetDataCounter\FileIsOpenMessage}
\def\FileIsOpenMessage{\message{%
  opened file \wrm@filename\space successfully:\codeof\ThisFileLine}}
\def\WARM@RelaxOpenFileMessage{\let\FileIsOpenMessage\relax}


\def\TryAgainToOpenTheFile{%
 \immediate\closein\wrmread
 \immediate\openin\wrmread\wrm@filename\relax
 \ifeof\wrmread\expandafter\FileEmptyError
 \else\expandafter\FileIsOpen\fi
}

\def\FileEmptyError{%
 \message{file \wrm@filename\space could not be opened or is empty.}%
 \futurelet\next\CloseTheFile
}

{\obeylines
\gdef\PrepareToOpenFile{%
 \begingroup
 \catcode`\%=12
 \catcode`
=9
 \catcode`\{=12
 \catcode`\}=12
}
\gdef\EndLineAdjust{\def\strip@endline##1
\strip@endline{##1}}
\gdef\strip@endline#1 \strip@endline{#1}
%\gdef\strip@endline#1\strip@endline{#1}
}

\def\ProcessFile#1#2{%
 \def\wrm@filename{#1}%
 \PrepareToOpenFile
 \TryToOpenTheFile#2%
 \CleanupAfterFile
}
\def\CleanupAfterFile{\endgroup}

\def\TestIfEnd{%
 \ifx\ThisFileLine\WARM@EndFileString
\message{Finishing with `\WARM@EndFileString' after \number\wrmcounter\space lines.}%
  \morelinesfalse
  \wrmcounter\maxwrmlines\advance\wrmcounter\@ne
 \fi }

\def\TestMoreLines{%
 \ifnum\wrmcounter<\maxwrmlines\relax
 \else\morelinesfalse\fi}

\def\TestReadLine#1#2{\begingroup
 \def\DataPattern{#2}\def\Empty{}%
 \ifx\DataPattern\Empty
  \def\next{\endgroup\TestExactLine#1}%
 \else
  \def\protect{\noexpand}% for "robust" LaTeX commands
%  \edef\testdata{\ThisFileLine\space\StrippedDataPattern\EndLinePattern}%
  \edef\testdata{\StrippedDataPattern\EndLinePattern}%
  \expandafter\expandafter\expandafter\def
  \expandafter\expandafter\expandafter\testdata
  \expandafter\expandafter\expandafter{\expandafter\ThisFileLine\testdata}%
%\show\testdata
  \edef\tmp{\gdef\noexpand\ThisData\DataPattern\EndLinePattern}%
  \expandafter\tmp\expandafter{\the\WARM@ExpansionToks}%
  \expandafter\ThisData\testdata
  \relax \def\next{\endgroup}%
  \ifx\undefined\wrm@llx
   \ifdim\wrm@aspect\p@=\p@\else\SaveARvalues@\fi
  \else\SaveBBvalues@\fi
  \aftergroup\TestExactLine\aftergroup#1\relax
 \fi \next }

\def\declareWRMdata#1#2#3{{%\gdef\wrm@namespace{#2}%
 \DN@{#2}\ifx\next@\empty\let\wrm@namespace\relax
 \else\gdef\wrm@namespace{#2}\fi
 \def\ThisFileLine{#1}\TestDataLine\WRM@DataPattern\WRM@DataStripped\relax}}

\def\wrm@namespace{}
\def\wrm@savedata{% check for End of Data 
 \ifx\testdata\WARM@EndDataTestString\aftergroup\morelinesfalse
 \else\aftergroup\morelinestrue
  \ifx\testdata\WARM@PreDataString
   \aftergroup\AdvanceDataCount
   \ifx\wrm@namespace\relax
   \else
    \expandafter\expandafter\expandafter
    \def\expandafter\expandafter\expandafter\wrm@texstring
    \expandafter\expandafter\expandafter{\expandafter\wrm@namespace\wrm@texstring}%
   \fi
   \let\protect\noexpand
   \ifx\wrm@quiet\relax
    \wrm@message{Marked '\wrm@label' point at (\wrm@x,\wrm@y) for \codeof\wrm@texstring.}%
   \fi
   \edef\tmp@{\noexpand\def\noexpand\wrm@x{\wrm@x}%
    \noexpand\def\noexpand\wrm@y{\wrm@y}}%
   \expandafter\expandafter\expandafter\def
   \expandafter\expandafter\expandafter\tmp@
   \expandafter\expandafter\expandafter{%
   \expandafter\expandafter\expandafter\def
   \expandafter\expandafter\expandafter\wrm@text
   \expandafter\expandafter\expandafter{%
   \expandafter\wrm@texstring\expandafter}\tmp@}%
%
   \ifx\wrm@namespace\relax
    \expandafter\global\expandafter
     \let\csname WRM@\wrm@label\endcsname=\tmp@
    \advance\wrmdatacounter\@ne
    \expandafter\global\expandafter
     \let\csname WRM@\the\wrmdatacounter @\endcsname=\tmp@
   \else \ifx\wrm@namespace\wrm@empty
    \expandafter\global\expandafter
     \let\csname WRM@\wrm@label\endcsname=\tmp@
    \advance\wrmdatacounter\@ne
    \expandafter\global\expandafter
     \let\csname WRM@\the\wrmdatacounter @\endcsname=\tmp@
   \else
    \expandafter\global\expandafter
     \let\csname WRM@\wrm@label.\wrm@namespace\endcsname=\tmp@
    \advance\wrmdatacounter\@ne
    \expandafter\global\expandafter
     \let\csname WRM@\the\wrmdatacounter @\endcsname=\tmp@
    \expandafter\ifx\csname WRM@\wrm@label\endcsname\relax
     \expandafter\global\expandafter
      \let\csname WRM@\wrm@label\endcsname=\tmp@
    \else
     \begingroup
      \def\wrm@message##1{\def\prev@name{##1}}%
      \csname WRM@\wrm@label\endcsname
      \ifx\wrm@text\wrm@empty
       \def\tmp@{\endgroup \edef\wrm@text{\wrm@namespace}}%
      \else \ifx\wrm@text\relax
       \edef\tmp@{\endgroup\def\noexpand\prev@name{\prev@name\space}%
         \let\noexpand\twoalready\relax}%
      \else
%\show\wrm@text %\aftergroup\tracingall
       \edef\tmp@{\endgroup\def\noexpand\prev@name{\wrm@text\space}}%
%         \def\noexpand\wrm@text{\noexpand\wrm@namespace}}%
      \fi\fi \tmp@
      \ifx\undefined\prev@name
      \else \ifx\twoalready\relax
       \edef\tmp@{\def\noexpand\wrm@x{\wrm@x}\def\noexpand\wrm@y{\wrm@y}%
%	 \def\noexpand\wrm@text{\wrm@text}%
	 \let\noexpand\wrm@text\relax
           \noexpand\wrm@message{\prev@name or \codeof\wrm@texstring}}%
      \else
       \edef\tmp@{\def\noexpand\wrm@x{\wrm@x}\def\noexpand\wrm@y{\wrm@y}%
	 \let\noexpand\wrm@text\relax\noexpand\wrm@message
%	 \def\noexpand\wrm@text{\wrm@text}\noexpand\wrm@message
	   {\wrm@label\space could be \prev@name or \codeof\wrm@texstring}}%
      \fi\fi
%\show\tmp@
      \expandafter\global\expandafter
       \let\csname WRM@\wrm@label\endcsname=\tmp@
    \fi
   \fi\fi
 \else
   %% all data occurs in a block --- unnecessary
%   \ifnum\wrmdatacounter>\z@\aftergroup\morelinesfalse\fi
 \fi\fi
}

\def\SaveBBvalues@{%
 \ifx\undefined\wrm@llx
  \def\next{\endgroup}%
 \else
  \edef\next{\endgroup \wrmdatacounter=\the\wrmdatacounter\relax
   \def\noexpand\wrm@llx{\wrm@llx}\def\noexpand\wrm@lly{\wrm@lly}%
   \def\noexpand\wrm@urx{\wrm@urx}\def\noexpand\wrm@ury{\wrm@ury}%
   \def\noexpand\wrm@aspect{\wrm@aspect}%
   \def\noexpand\wrm@offset{\wrm@offset}%
   \def\noexpand\wrm@filename{\wrm@filename}}%
 \fi }
\def\saveBBafterFile{\SaveBBvalues@\next
 \def\CleanupAfterFile{\endgroup}}

\def\SaveARvalues@{%
 \ifdim\wrm@aspect\p@=\p@
  \def\next{\endgroup}%
 \else
  \edef\next{\endgroup
   \def\noexpand\wrm@aspect{\wrm@aspect}%
   \def\noexpand\wrm@offset{\wrm@offset}}%
 \fi }
\def\saveARafterFile{\SaveARvalues@\next
 \def\CleanupAfterFile{\endgroup}}

\def\TestExactLine#1{\ifx\ThisFileLine#1\morelinesfalse\fi}
\def\TestDataString{%
% \edef\testdata{\ThisFileLine\space\DataStripped\EndLinePattern}%
  \edef\testdata{\DataStripped\EndLinePattern}%
  \expandafter\expandafter\expandafter\def
  \expandafter\expandafter\expandafter\testdata
  \expandafter\expandafter\expandafter{\expandafter\ThisFileLine\testdata}%
 \edef\tmp{\def\noexpand\ThisData@Test\DataPattern\EndLinePattern}%
 \expandafter\tmp\expandafter{\the\WARM@ExpansionToks}%
 \expandafter\ThisData@Test\testdata}

\def\TestDataLine#1#2#3{{%
 \def\DataPattern{#1}\def\Empty{}\def\DataStripped{#2}%
 \ifx\DataPattern\Empty
  \aftergroup\TestExactLine\aftergroup\WARM@PreDataString\relax
 \else
  \TestIfEnd\ifmorelines
   \ifnum\wrmdatacounter<\z@
    \aftergroup\wrmdatacounter\aftergroup\z@
   \else\expandafter\TestDataString\fi
  \else\morelinesfalse\fi
  \ifmorelines
   % found valid data
   #3%
  \else
   % no more data -- pass boolean to next level
   \aftergroup\morelinesfalse
  \fi \fi}}

\def\PrintDataLine{%
 \wrm@message{\wrm@filename\space data: \codeof\ThisFileLine}}
\def\PrintInfoLine{%
 \wrm@message{Found \codeof\ThisFileLine\space on line \number\wrmcounter}}
\def\PrintNumberOfDataPoints{%
 \wrm@message{Found \number\wrmdatacounter\space data points.}}
\def\WriteToBBfile{%
 \immediate\write\wrm@write{\codeof\ThisFileLine}}
\def\WARM@RelaxBBWrite{\let\WriteToBBfile\relax}

\def\AdvanceLineCount{\advance\wrmcounter\@ne}
\def\RevertLineCount{\advance\wrmcounter\m@ne}
\def\AdvanceDataCount{\advance\wrmdatacounter\@ne\RevertLineCount}
\def\ResetDataCounter{\wrmdatacounter=\m@ne}

\def\ReadSomeLines{\wrmcounter=\@ne \morelinestrue
 \loop \TestIfEnd \TestMoreLines
 \ifmorelines \AdvanceLineCount \ReadLineFromFile
 \repeat
}

\def\ReadToLine#1#2#3{\ifnum\wrmcounter=\z@\wrmcounter=\@ne\fi
 \ifnum\wrmcounter<\maxwrmlines\relax\morelinestrue\fi
 \loop %\PrintDataLine
  \TestReadLine #1{#2}\TestMoreLines
 \ifmorelines \advance\wrmcounter\@ne \ReadLineFromFile
 \repeat
 \ifnum\wrmcounter>0
  \ifnum\maxwrmlines<\wrmcounter \message{Failed to find: #1}%
  \else \expandafter #3\fi
 \else \message{Failed to find: #1}\fi
}

\def\ReadDataLines#1#2#3{%
 \ResetDataCounter
 \edef\WARM@EndDataTestString{\WARM@EndDataString\space\ParenEmpty}%
 \ifnum\wrmcounter<\maxwrmlines\relax\morelinestrue\fi
 \loop
  \TestDataLine{#1}{#2}{#3}\TestIfEnd \TestMoreLines
 \ifmorelines \advance\wrmcounter\@ne \ReadLineFromFile
 \repeat
 \PrintNumberOfDataPoints
}

\newtoks\BBexpansiontoks
\def\BBpattern{####1 ####2 ####3 ####4 ####5 ####6}
\def\BBstripped{ , , , , }
\BBexpansiontoks={\def\testcomment{#1}%
 \ifx\testcomment\AdobeBoundingBoxString
  \def\wrm@llx{#2}\def\wrm@lly{#3}%
  \def\wrm@urx{#4}\def\wrm@ury{#5}%
  % there can be only one
  \aftergroup\morelinesfalse
\wrm@message{file: \wrm@filename}%
\wrm@message{Bounding Box is (\wrm@llx,\wrm@lly)->(\wrm@urx,\wrm@ury)}%
  \WriteToBBfile %{\ThisFileLine}%
 \fi
}

\def\WRM@DataPattern{}
\def\WRM@DataStripped{}
\def\wrm@filename{}

%
% These are now defined later, for use with Adobe EPS files:
%
%\newtoks\ParenPointToks
%\ParenPointToks={\def\wrm@x{#2}\def\wrm@y{#3}%
% \def\afterdata{#7}%
% \ifx\afterdata\ParenEmpty
%%  \edef\wrm@label{datapoint\the\wrmdatacounter}%
%  \edef\wrm@label{\wrm@prefix\the\wrmdatacounter}%
%  \def\wrm@texstring{#4}%
% \else
%  \def\wrm@label{#4}\def\wrm@texstring{#5}%
% \fi
% \def\testdata{#1}\wrm@savedata }
%
%\def\ParenPointData{%
% \let\WRM@DataPattern\ParenPointPattern
% \let\WRM@DataStripped\ParenPointStripped
% \def\SetExpansionToks{\let\WARM@ExpansionToks\ParenPointToks}%
%}

%%
%%  \RecoverData{<which>}{<what to do>}{<fallback>}{<mode>}
%%
%%  <which> : symbolic label for a marked-point
%%  <what to do> : macro to expand for the given label
%%  <fallback> : action to take if <which> is invalid
%%  <mode> :  @ = numeric  {}= symbolic
%%
%%  this also sets a macro \wrm@name to expand to an identifying
%%  string  WRM@<which> , as a name for the point itself.
%%  e.g. useful in Xy-pic to return to the marked point as a <pos>
%%
\def\RecoverData#1#2#3#4{%
 \expandafter\ifx\expandafter\relax\csname WRM@#1#4\endcsname
  \message{There is no data for label: #1}%
  \let\wrm@name\wrm@none \def\wrm@next{#3}%
 \else
  \def\wrm@name{WRM#1}\def\wrm@next{#2}%
  \csname WRM@#1#4\endcsname
 \fi
 \wrm@next }
\newif\iftesting\global\testingfalse

%% default value for \wrm@name when no valid marked-point.
\def\wrm@none{@WRM@none}
\let\wrm@name\wrm@none

%% show information associated with a symbolic label:
\def\ShowMarkedData#1{\RecoverData{#1}{\ShowMarkedData@{#1}}\relax{}}
\def\ShowMarkedData@#1{%
 \wrm@message{'#1' (\wrm@x,\wrm@y) \codeof\wrm@name: \codeof\wrm@text}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%     for use with  Xy-pic
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% allow for top-to-bottom coordinates
\def\Mac@orig{}
\def\MacintoshOrigin{\def\Mac@orig{-}}

%%
%% This is the main macro for establishing a marked-point
%% as the current <pos>, for placing a label, or whatever.
%% It establishes "WRM@<name>" as an easy recovery method.
%%
\def\xyMarkedPos#1{\RecoverData{#1}\xyMarkedPos@\POS{}}
\def\xyMarkedPos@{\def\wrmtext{\wrm@text}%
 \edef\wrm@next{(\wrm@x,\Mac@orig\wrm@y)}%
 \ifx\wrm@quiet\relax\wrm@message{\wrm@name:\codeof\wrm@text: \wrm@next}\fi
 \let\protect\relax % for "robust" LaTeX commands
 \expandafter\expandafter\expandafter\POS
 \expandafter\wrm@next\expandafter=\expandafter"\wrm@name"}

%% Default positioning of the label,
%% with respect to the marked-points
%%  text: bottom-left corner, including a small margin
%%  math: centered
%%
\def\labelmathmodifiers{!C}
\def\labeltextmodifiers{+!LD}
\def\labelmathstyle{}
\def\labeltextstyle{}
\def\markobject{*\dir{x},*+<3pt>!UL{\scriptscriptstyle\the\wrmcounter}}
\def\markobjectlabel#1{*\dir{x},*+<3pt>!U{\scriptscriptstyle#1}}
\def\markedempty@@{}

%%
%%  \xyShowMark {<pos>*<object>}{<which>}
%%  \xyShowMarkPoints  {<pos>*<object>}{<which>}
%%  \xyShowMarksExcept {<pos>*<object>}{<which>}
%%
%%  allows any valid Xy-pic code to be interpreted
%%  at the specified marked-point.
%%  defaults to use \markobject when the 1st argument is empty.
%%

\def\xyShowMark{\xyShowMarks@i\z@}
\def\xyShowMarkPoints{\xyShowMarks@i\markedempty@@}
\def\xyShowMarksExcept{\xyShowMarks@i\relax}

\def\xyShowMarks@i#1#2#3{%
  \def\xymarkobject@{#2}%
  \ifx\xymarkobject@\markedempty@@
    \let\xymarkobject@\markobject
  \fi
 \ifx#1\z@
  \def\xymarkobject@{\markobjectlabel{#3}}%
  \DN@{\RecoverDataLabel{#3}}%
 \else \DN@{\RecoverDataPoints{#3}#1}%
 \fi \next@\xyShowMark@x\POS }

\def\xyShowMark@x{%
 \expandafter\xyMarkedPos@\xymarkobject@\relax}

%%
%%   \xyMarkedTxt <modifiers> {<label>}
%%   \xyMarkedTxtPoints <modifiers> {<which>}
%%   \xyMarkedTxtExcept <modifiers> {<which>}
%%    and \xyMarkedText, \xyMarkedTextPoints, \xyMarkedTextExcept
%%
%%   \xyMarkedStyledTxt <modifiers> {<styles>} {<label>}
%%   \xyMarkedStyledTxtPoints <modifiers> {<styles>} {<which>}
%%   \xyMarkedStyledTxtExcept <modifiers> {<styles>} {<which>}
%%
%%  Use the default label, in text-mode,
%%  with optional modifiers, default: \labeltextmodifiers
%%  and styles, default: \labeltextstyle

\def\xyMarkedTxt{\xyMarkedTxt@i\z@}
\def\xyMarkedTxtPoints{\xyMarkedTxt@i\markedempty@@}
\def\xyMarkedTxtExcept{\xyMarkedTxt@i\relax}
\def\xyMarkedStyledTxt{\xyMarkedStyledTxt@i\z@}
\def\xyMarkedStyledTxtPoints{\xyMarkedStyledTxt@i\markedempty@@}
\def\xyMarkedStyledTxtExcept{\xyMarkedStyledTxt@i\relax}

%% Recognise alternate names for the above user-macros
\let\xyMarkedText\xyMarkedTxt
\let\xyMarkedTextPoints\xyMarkedTxtPoints
\let\xyMarkedStyledText\xyMarkedStyledTxt
\let\xyMarkedTextExcept\xyMarkedTxtExcept
\let\xyMarkedStyledTextPoints\xyMarkedStyledTxtPoints
\let\xyMarkedStyledTextExcept\xyMarkedStyledTxtExcept


% containers for user-supplied data
\def\xyMarkedTxtMods@{}
\def\xyMarkedTxtStyles@{}

\def\xyMarkedTxt@i#1#2#{\def\xyMarkedTxtMods@{#2}%
 \ifx\xyMarkedTxtMods@\markedempty@@
  \let\xyMarkedTxtMods@\labeltextmodifiers
 \fi
 \let\xyMarkedTxtStyles@\labeltextstyle
 \xyMarkedTxt@ii#1}

\def\xyMarkedTxt@ii#1#2{%
 \ifx#1\z@ \DN@{\RecoverDataLabel{#2}}%
 \else \DN@{\RecoverDataPoints{#2}#1}%
 \fi \next@\xyMarkedTxt@x\POS }


\def\xyMarkedStyledTxt@i#1#2#{\def\xyMarkedTxtMods@{#2}%
 \ifx\xyMarkedTxtMods@\markedempty@@
  \let\xyMarkedTxtMods@\labeltextmodifiers
 \fi \xyMarkedStyledTxt@ii#1}

\def\xyMarkedStyledTxt@ii#1#2#3{%
 \def\xyMarkedTxtStyles@{#2}%
 \ifx\xyMarkedTxtStyles@\markedempty@@
  \let\xyMarkedTxtMods@\labeltextmodifiers
 \fi
 \ifx#1\z@ \DN@{\RecoverDataLabel{#3}}%
 \else \DN@{\RecoverDataPoints{#3}#1}%
 \fi \next@\xyMarkedTxt@x\POS }


\def\xyMarkedTxt@x{%
 \wrm@recovercode \xyMarkedPos@\relax
 \ifnum\wrmcounter > \z@
   \edef\next@{="WRM\the\wrmcounter t"}%
 \else \def\next@{}\fi
 \def\tmp@{%
  \expandafter\expandafter\expandafter\drop
  \expandafter\xyMarkedTxtMods@
  \expandafter\txt\xyMarkedTxtStyles@}%
%\wrm@message{\codeof\tmp@{\codeof\wrm@text}}%
 \expandafter\cfromid@\expandafter{\wrm@name}%
 \expandafter\tmp@\expandafter{\wrm@text}%
 \ifnum\wrmcounter > \z@
  \edef\next@{="WRM\the\wrmcounter t"}%
 \else \def\next@{}\fi
 \expandafter\POS\next@
}


%%
%%   \xyMarkedMath <modifiers> {<which>}
%%   \xyMarkedMathPoints <modifiers> {<list>}
%%   \xyMarkedMathExcept <modifiers> {<list>}
%%     and 
%%   \xyMarkedStyledMath <modifiers> {<styles>} {<which>}
%%   \xyMarkedStyledPoints <modifiers> {<styles>} {<list>}
%%   \xyMarkedStyledMathExcept <modifiers> {<styles>} {<list>}
%%
%%  Use the default label, in math-mode,
%%  with optional modifiers, default: \labelmathmodifiers
%%  and styles, default: \labelmathstyle

\def\xyMarkedMath{\xyMarkedMath@i\z@}
\def\xyMarkedMathPoints{\xyMarkedMath@i\markedempty@@}
\def\xyMarkedMathExcept{\xyMarkedMath@i\relax}

% containers for user-supplied data
\def\xyMarkedMathMods@{ }
\def\xyMarkedMathStyles@{ }

\def\xyMarkedMath@i#1#2#{%
 \def\xyMarkedMathMods@{#2}%
 \ifx\xyMarkedMathMods@\markedempty@@
  \let\xyMarkedMathMods@\labelmathmodifiers
 \fi 
 \expandafter\def\expandafter\xyMarkedMathStyles@\expandafter
  {\labelmathstyle{}}%
 \xyMarkedMath@ii#1}
\def\xyMarkedMath@ii#1#2{%
 \ifx#1\z@ \DN@{\RecoverDataLabel{#2}}%
 \else \DN@{\RecoverDataPoints{#2}#1}%
 \fi \next@\xyMarkedMath@x\POS}

\def\xyMarkedStyledMath{\xyMarkedStyledMath@i\z@}
\def\xyMarkedStyledMathPoints{\xyMarkedStyledMath@i\markedempty@@}
\def\xyMarkedStyledMathExcept{\xyMarkedStyledMath@i\relax}

\def\xyMarkedStyledMath@i#1#2#{%
 \def\xyMarkedMathMods@{#2}%
 \ifx\xyMarkedMathMods@\markedempty@@
  \let\xyMarkedMathMods@\labelmathmodifiers
 \fi \xyMarkedStyledMath@ii#1}
\def\xyMarkedStyledMath@ii#1#2#3{%
 \def\xyMarkedMathStyles@{#2}%
 \ifx\xyMarkedMathStyles@\markedempty@@
  \expandafter\def\expandafter\xyMarkedMathStyles@\expandafter
   {\labelmathstyle{}}%
 \fi
 \ifx#1\z@ \DN@{\RecoverDataLabel{#3}}%
 \else \DN@{\RecoverDataPoints{#3}#1}%
 \fi \next@\xyMarkedMath@x\POS}

\def\xyMarkedMath@x{%
 \wrm@recovercode \xyMarkedPos@ \relax
 \expandafter\def\expandafter\tmp@\expandafter{%
 \expandafter\drop\xyMarkedMathMods@}%
%\wrm@message{\codeof\tmp@{\codeof\xyMarkedMathStyles@ \codeof\wrm@text}}%
% shouldn't need \tmp@ to be expanded yet
 \expandafter\expandafter\expandafter\tmp@
  \expandafter\expandafter\expandafter{%
   \expandafter\xyMarkedMathStyles@\wrm@text}%
 \ifnum\wrmcounter >\z@
  \edef\next@{="WRM\the\wrmcounter m"}%
 \else \def\next@{}\fi
 \expandafter\POS\next@
}

\def\RecoverDataLabel#1#2#3{\RecoverData{#1}#2#3{@}}
\def\RecoverDataPoints#1#2#3#4{%
  \ParsePointsList@{#1}%
  \ifx#2\markedempty@@ \reverse@WRMflags \fi
  \xyAllMarkedPoints@{#3}{#4}}


%\def\xyMarkedPoints#1{%
% \xyAllMarkedPoints@i\xyShowMark@x\relax}

\def\ParsePointsList@#1{\parseLIST@#1 0\relax\endlist@}
\def\parseLIST@#1{\expandafter\ifx \space#1\relax
   \expandafter\DN@\space{\parseLIST@}%
 \else\ifx \relax#1\relax \DN@{\parseLIST@}%
 \else\ifx \endlist@#1\relax \DN@{}%
 \else\ifcat \noexpand#1\relax \DN@{\expandafter\parseLIST@#1}%
 \else\ifx ,#1\relax \DN@{\wrm@setflag}%
 \else\addDASH@\ifx #1\relax
   \DN@{\afterassignment\wrm@endlist\count@=}%
% \else \DN@{\parselisterror{#1}\FN@\parseLIST@}%
 \else \DN@{\afterassignment\wrm@startlist\count@=#1}%
 \fi\fi\fi\fi\fi\fi \next@ }

\def\wrm@start{0}
\def\wrm@end{0}

\def\parselisterror#1{%
 \xyerror@{Token #1 is not part of a <num> or range <num>-<num>.}}

\def\wrm@startlist{%
% \errorcontextlines=40 \showthe\count@
 \ifnum\count@<\z@
   \multiply\count@\m@ne \expandafter\wrm@endlist
 \else \edef\wrm@start{\the\count@}\fi
% \parseLIST@ }
 \DN@##1{\addDASH@\ifx##1\DN@{\parseLIST@##1}%
  \else \DN@{\wrm@setflag ##1}\fi\next@ }%
 \next@ }

\def\wrm@setflag{%
 \ifnum\wrm@start=\z@\else
  \expandafter\expandafter\expandafter\let
   \expandafter\csname\wrm@start @WRM\endcsname\markedempty@@
  \def\wrm@start{0}%
 \fi \parseLIST@ }
% \DN@{\parseLIST@}%
% \DN@##1{\show##1\relax\parseLIST@}%
% \next@ }

\def\wrm@endlist{%
 \ifnum\count@=\z@ \edef\wrm@end{\the\wrmdatacounter}%
 \else \edef\wrm@end{\the\count@}\fi
 \ifnum\wrm@start=\z@ \def\wrm@start{1}\fi
 \ifnum\wrm@end<\wrm@start \relax\else
  \wrmcounter=\wrm@start\relax
  \loop
   \expandafter\expandafter\expandafter\let
    \expandafter\csname\the\wrmcounter @WRM\endcsname\markedempty@@
   \ifnum\wrmcounter<\wrm@end \advance\wrmcounter\@ne
  \repeat
 \fi \def\wrm@start{0}\def\wrm@end{0}\parseLIST@ }


\def\xyAllMarkedPoints#1{%
  \def\xymarkobject@{#1}%
  \ifx\xymarkobject@\markedempty@@
    \let\xymarkobject@\markobject
  \fi
  \xyShowAllMarkedPoints@ }
\let\xyShowAllMarkedPoints\xyAllMarkedPoints

\def\xyShowAllMarkedPoints@#1#2{%
 \xyWARMprocess{#1}{#2}%
 \def\wrm@filename{#1.#2}%
 \xyAllLabelsImport@{#1.#2}%
 \ifnum\wrmdatacounter>0
    \DN@{\xyAllMarkedPoints@\xyShowMark@x\relax}%
 \else \DN@{\POS}\fi 
 \next@ }

\def\xyAllMarkedMath#1#{%
  \expandafter\def\expandafter\xyMarkedMathStyles@
   \expandafter{\labelmathstyle{}}%
  \def\xyMarkedMathMods@{#1}%
  \ifx\xyMarkedMathMods@\markedempty@@
   \let\xyMarkedMathMods@\labelmathmodifiers
  \fi
 \xyAllMarkedPoints@y\xyMarkedMath@x\relax}

\def\xyAllMarkedText#1#{%
  \let\xyMarkedTxtStyles@\labeltextstyle
  \def\xyMarkedTextMods@{#1}%
  \ifx\xyMarkedTextMods@\markedempty@@
   \let\xyMarkedTextMods@\labeltextmodifiers
  \fi
 \xyAllMarkedPoints@y\xyMarkedTxt@x\relax}%

\def\xyAllMarkedPoints@y#1#2#3#4{%
  \xyWARMprocess{#3}{#4}%
  \def\wrm@filename{#3.#4}%
  \xyAllLabelsImport@{#3.#4}%
  \xyAllMarkedPoints@i#1#2}


\def\xyAllMarkedPoints@i#1#2{%
 \ifnum\wrmdatacounter>0 
  \DN@{\xyAllMarkedPoints@#1#2}%
 \else \DN@{\POS}\fi \next@ }

\def\xyMarkedImport#1{\def\tmp{#1}%
  \ifx\tmp\markedempty@@
   \ifx\wrm@filename\markedempty@@
    \DN@{\xyAllImportError@}%
   \else
    \DN@{\xyAllLabelsImport@{\wrm@filename}}%
   \fi
  \else
   \DN@{\xyAllLabelsImport@{\tmp}}%
  \fi \next@}

\def\xyAllImportError@{%
  \xyerror@{There is no graphic file to import.}}%
\def\xyAllImportBBError@#1{%
  \xyerror@{There is no Marked-Point information for image #1.}}%

\def\xyAllLabelsImport@{\ifx\undefined\wrm@urx
 \expandafter\xyAllImportBBError@
 \else\expandafter\xyAllLabelsImport@i\fi}
\def\xyAllLabelsImport@i#1{\bgroup
  \L@c=\wrm@llx\p@ \L@c=-\L@c \D@c=\wrm@lly\p@ \D@c=-\D@c
  \R@c=\wrm@urx\p@ \advance\R@c\L@c
  \U@c=\wrm@ury\p@ \advance\U@c\D@c
  \ifdim\wrm@aspect\p@=\p@\else
   \ifdim\wrm@aspect\p@<\p@
    \quotient@\tmp@\U@c{\wrm@aspect\p@}%
    \dimen@=\tmp@\p@\advance\dimen@-\U@c \divide\dimen@\tw@
    \U@c=\tmp@\p@ \advance\D@c\dimen@
    % \dimen@=\wrm@offset\p@ \divide\dimen@\tw@
    % \advance\U@c-\wrm@offset\p@
    \dimen@=\wrm@offset\p@ \divide\dimen@\tw@ \advance\D@c\dimen@
   \else
    \dimen@=\R@c \R@c=\wrm@aspect\R@c
    \advance\dimen@-\R@c \divide\dimen@\tw@
    \advance\L@c-\dimen@
    \dimen@=\wrm@offset\p@ \divide\dimen@\tw@ \advance\L@c\dimen@
   \fi
  \fi
  \edef\next@{\egroup \def\noexpand\xyimportparams@ %
   {(\expandafter\removePT@\the\R@c,\expandafter\removePT@\the\U@c)%
    (\expandafter\removePT@\the\L@c,\expandafter\removePT@\the\D@c)}%
   }\next@  
 \expandafter\xyimport\xyimportparams@{\xyWARMinclude{#1}}%
}
\def\wrm@aspect{1}
\def\wrm@offset{0}

\def\xyAllMarkedPoints@#1#2{%
 \wrmcounter=\z@
% \ifx\wrm@prefix\wrm@defprefix\def\wrm@prefix{}\fi
 \loop
  \ifnum\wrmcounter<\wrmdatacounter\relax
   \advance\wrmcounter\@ne
   \expandafter\expandafter\expandafter\ifx\expandafter
   \csname\the\wrmcounter @WRM\endcsname\relax
   \expandafter\DN@\expandafter{\expandafter
    \RecoverData\expandafter{%\expandafter\wrm@prefix
     \the\wrmcounter}#1#2{@}\relax}%
  \else
   \expandafter\expandafter\expandafter\let
    \expandafter\csname\the\wrmcounter @WRM\endcsname\relax
   \DN@{\relax}%
  \fi \next@
 \repeat \POS }

\def\reverse@WRMflags{%
 \wrmcounter=\z@
 \loop
  \ifnum\wrmcounter<\wrmdatacounter
  \advance\wrmcounter\@ne
  \expandafter\expandafter\expandafter\ifx\expandafter
   \csname\the\wrmcounter @WRM\endcsname\relax
   \expandafter\expandafter\expandafter\let\expandafter
    \csname\the\wrmcounter @WRM\endcsname\markedempty@@
  \else
   \expandafter\expandafter\expandafter\let\expandafter
    \csname\the\wrmcounter @WRM\endcsname\relax
  \fi
 \repeat }

% allows a constant offset to be added to each marked-point's
% coordinate position --- in case the BoundingBox is wrong
%
\def\xyWARMoffset[#1,#2]{%usage:  \xyWARMoffset[<num>,<num>]
 \def\wrm@xoffset{#1}%
 \def\wrm@yoffset{#2}%
}
\def\wrm@checkoffset{%
 \ifx\wrm@xoffset\wrm@empty\else
  \bgroup 
   \errhelp={WARM error: invalid offset in X-direction}%
   \dimen@\wrm@x\p@ \advance\dimen@\wrm@xoffset\p@
   \edef\next{\egroup
    \def\noexpand\wrm@x{\expandafter\removePT@\the\dimen@}}%
  \next
 \fi
 \ifx\wrm@yoffset\wrm@empty\else
  \bgroup 
   \errhelp={WARM error: invalid offset in Y-direction}%
   \dimen@\wrm@y\p@ \advance\dimen@\wrm@yoffset\p@
   \edef\next{\egroup
    \def\noexpand\wrm@y{\expandafter\removePT@\the\dimen@}}%
  \next
 \fi }
\def\wrm@xoffset{}%
\def\wrm@yoffset{}%

%%
%%  default file-type method is  \WARMprocessEPS
%%  assuming the graphics file has an associated .bb file
%%
\def\xyWARMprocessEPS#1#2{\WARMprocessEPS{#1}{#2}{bb}}
\def\xyWARMprocessMoEPS#1#2{\WARMprocessMoEPS{#1}{#2}{bb}}
\let\xyWARMprocess=\xyWARMprocessEPS
\let\xyWARMprocessMo=\xyWARMprocessMoEPS

%%
%%  default graphics file inclusion  \includegraphics
%%  (assumes LaTeX-2e with \usepackage{graphics})
%%
\def\xyWARMinclude#1{\includegraphics{#1}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  implementations for specific image-types
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newtoks\WARM@ExpansionToks

%%
%% Implementation for Adobe Illustrator files, v8.x and earlier
%%
{\catcode`\%=12
\gdef\ParenPointPattern{####1 (####2,####3) ####4 %####5 %####6%####7}
\gdef\ParenPointStripped{ % (,) , % %%}
\gdef\ParenEmpty{%}
\gdef\AdobeBoundingBoxString{%%BoundingBox:}
\gdef\AdobeStartDataString{%%StartMarkedPoints}
\gdef\AdobePreDataString{%%MarkedPoint:}
\gdef\AdobeEndDataString{%%EndMarkedPoints}
\gdef\AdobeEndFileString{%%EndComments}
}
\def\FindAdobeMarkedPoints{%
 \let\WARM@EndFileString=\AdobeEndFileString
 \let\WARM@PreDataString=\AdobePreDataString
 \let\WARM@EndDataString=\AdobeEndDataString
 \let\DataPattern=\BBpattern
 \let\StrippedDataPattern=\BBstripped
 \let\WARM@ExpansionToks=\BBexpansiontoks
 \let\CleanupAfterFile=\saveBBafterFile
 \let\CleanupAfterDir=\saveBBafterFile
 \let\wrm@llx=\undefined
 \def\wrm@aspect{1}%
 \def\wrm@offset{0}%
 \ReadToLine\AdobeBoundingBoxString\BBpattern\relax %\PrintInfoLine
 \ReadToLine\AdobeStartDataString{}\relax %\PrintInfoLine
 \SetExpansionToks
 \ReadDataLines\WRM@DataPattern\WRM@DataStripped\WriteToBBfile
 \ReadToLine\AdobeEndDataString{}\relax %\PrintInfoLine
 \ReadToLine\WARM@EndFileString{}\relax %\PrintInfoLine
}

\newtoks\ParenPointToks
\ParenPointToks={\def\wrm@x{#2}\def\wrm@y{#3}%
 \def\afterdata{#7}%
 \ifx\afterdata\ParenEmpty
%  \edef\wrm@label{datapoint\the\wrmdatacounter}%
  \edef\wrm@label{\wrm@prefix\the\wrmdatacounter}%
  \def\wrm@texstring{#4}%
 \else
  \def\wrm@label{#4}\def\wrm@texstring{#5}%
 \fi
 \def\testdata{#1}\wrm@savedata }
\def\ParenPointData{%
 \let\WRM@DataPattern\ParenPointPattern
 \let\WRM@DataStripped\ParenPointStripped
 \def\SetExpansionToks{\let\WARM@ExpansionToks\ParenPointToks}%
}
\def\FindAdobeParenPoints{\ParenPointData\FindAdobeMarkedPoints}

\def\WARMprocessEPS#1#2#3{\WARMprocess{#1}{#2}{#3}\FindAdobeParenPoints}


%%
%% Implementation for Adobe Illustrator files,  v9.x  with Mo-tool
%%
{\catcode`\%=12
\gdef\AdobeMoPointPattern{####1(####2,####3) :####4 :####5 %####6 %####7%####8}
\gdef\AdobeMoPointStripped{ %(,) : : % %%}
\gdef\AdobeMoEmpty{%}
\gdef\AdobeMoBoundingBoxString{%%BoundingBox:}
\gdef\AdobeMoStartDataString{%%StartMarkedPoints}
\gdef\AdobeMoPreDataString{%%MarkedPoint:}
\gdef\AdobeMoEndDataString{%%EndMarkedPoints}
\gdef\AdobeMoEndFileString{%%EndComments}
}
\def\FindAdobeMoMarkedPoints{%
 \let\WARM@EndFileString=\AdobeMoEndFileString
 \let\WARM@PreDataString=\AdobeMoPreDataString
 \let\WARM@EndDataString=\AdobeMoEndDataString
 \let\DataPattern=\BBpattern
 \let\StrippedDataPattern=\BBstripped
 \let\WARM@ExpansionToks=\BBexpansiontoks
 \let\CleanupAfterFile=\saveBBafterFile
 \let\CleanupAfterDir=\saveBBafterFile
 \let\wrm@llx=\undefined
 \def\wrm@aspect{1}%
 \def\wrm@offset{0}%
 \ReadToLine\AdobeMoBoundingBoxString\BBpattern\relax %\PrintInfoLine
 \ReadToLine\AdobeMoStartDataString{}\relax %\PrintInfoLine
 \SetExpansionToks
 \ReadDataLines\WRM@DataPattern\WRM@DataStripped\WriteToBBfile
 \ReadToLine\AdobeMoEndDataString{}\relax %\PrintInfoLine
 \ReadToLine\WARM@EndFileString{}\relax %\PrintInfoLine
}

\def\WRM@stripspace#1#2\endstrip{\expandafter\ifx\space#1\else#1\fi#2}
\newtoks\AdobeMoPointToks
\AdobeMoPointToks={\def\wrm@x{#2}\def\wrm@y{#3}%
 \wrm@checkoffset
 \def\afterdata{#8}%
 \ifx\afterdata\ParenEmpty
  \edef\wrm@label{\wrm@prefix\the\wrmdatacounter}%
  \def\wrm@texstring{#5}%
 \else
  \def\wrm@label{#5}%
  \ifx\wrm@label\wrm@empty\else
   \edef\wrm@label{\WRM@stripspace#5\endstrip}\fi
  \def\wrm@texstring{#6}%
 \fi
 \def\testdata{#1}\wrm@savedata }
\def\AdobeMoPointData{%
 \let\WRM@DataPattern\AdobeMoPointPattern
 \let\WRM@DataStripped\AdobeMoPointStripped
 \def\SetExpansionToks{\let\WARM@ExpansionToks\AdobeMoPointToks}%
}
\def\FindAdobeMoPoints{\AdobeMoPointData\FindAdobeMoMarkedPoints}

\def\WARMprocessMoEPS#1#2#3{\WARMprocess{#1}{#2}{#3}\FindAdobeMoPoints}


%%
%% Implementation for Mathematica Digitised files (.mbb suffix)
%%

{\catcode`[=1 \catcode`]=2 \catcode`\{=12 \catcode`\}=12 \catcode`\%=12
\gdef\MmaPointPattern[####1{####2, ####3, ####4}####5]
\gdef\MmaPointStripped[{, , }]
\gdef\MmaBBpattern[####1{####2, ####3, ####4, ####5}####6]
\gdef\MmaBBstripped[{, , , }]
\gdef\MmaPointEmpty[]
\gdef\MmaBoundingBoxString[LDRU:]
\gdef\MmaAspectRatioString[%%AspectRatio]
\gdef\MmaARpattern[####1: ####2:####3:####4]
\gdef\MmaARstripped[: ::]
\gdef\MmaStartDataString[StartData]
\gdef\MmaPreDataString[,]
\gdef\MmaEndDataString[EndData]
\gdef\MmaEndFileString[]
]

\newtoks\MmaBBexpansiontoks
\MmaBBexpansiontoks={\def\testcomment{#1}%
 \ifx\testcomment\MmaBoundingBoxString
  \def\wrm@llx{#2}\def\wrm@lly{#3}%
  \def\wrm@urx{#4}\def\wrm@ury{#5}%
  % there can be only one
  \aftergroup\morelinesfalse
\wrm@message{file: \wrm@filename}%
\wrm@message{Bounding Box is (\wrm@llx,\wrm@lly)->(\wrm@urx,\wrm@ury)}%
 \fi
}

\newtoks\MmaARexpansiontoks
\MmaARexpansiontoks={\def\testcomment{#1}%
 \ifx\testcomment\MmaAspectRatioString
  \aftergroup\morelinesfalse
  \def\wrm@aspect{#2}%
  \dimen@=#3\p@\ifdim\dimen@=\z@\else\def\wrm@offset{#3}\fi
  \ifx\wrm@offset\wrm@empty\def\wrm@offset{0}\else
  \def\tmp@{ }\ifx\wrm@offset\tmp@\def\wrm@offset{0}\fi\fi
\wrm@message{AspectRatio is: \wrm@aspect , Offset: \wrm@offset}%
 \fi
}

\def\FindMmaMarkedPoints{%
 \FindMmaMarkedPoints@i
 \FindMmaMarkedPoints@ii
 \FindMmaMarkedPoints@x
}%
\def\FindMmaARMarkedPoints{%
 \FindMmaMarkedPoints@i
 \FindMmaMarkedPoints@r
 \FindMmaMarkedPoints@ii
 \FindMmaMarkedPoints@x
}%

\def\FindMmaMarkedPoints@i{%
 \let\WARM@EndFileString=\MmaEndFileString
 \let\WARM@PreDataString=\MmaPreDataString
 \let\WARM@EndDataString=\MmaEndDataString
 \let\DataPattern=\MmaPointPattern
 \let\wrm@llx=\undefined
 \def\wrm@aspect{1}%
}

\def\FindMmaMarkedPoints@r{%
 \let\StrippedDataPattern=\MmaARstripped
 \let\WARM@ExpansionToks=\MmaARexpansiontoks
 \let\CleanupAfterFile=\saveARafterFile
 \let\CleanupAfterDir=\saveARafterFile
 \ReadToLine\MmaAspectRatioString\MmaARpattern\relax 
}

\def\FindMmaMarkedPoints@ii{%
 \let\StrippedDataPattern=\MmaBBstripped
 \let\WARM@ExpansionToks=\MmaBBexpansiontoks
 \let\CleanupAfterFile=\saveBBafterFile
 \let\CleanupAfterDir=\saveBBafterFile
 \ReadToLine\MmaBoundingBoxString\MmaBBpattern\relax 
}

\def\FindMmaMarkedPoints@x{%
 \ReadToLine\MmaStartDataString{}\relax 
 \SetExpansionToks
 \ReadDataLines\WRM@DataPattern\WRM@DataStripped\relax
 \ReadToLine\MmaEndDataString{}\relax
 \ReadToLine\MmaEndFileString{}\relax
}

\newtoks\MmaPointToks
\MmaPointToks={\def\wrm@x{#2}\def\wrm@y{#3}%
 \def\afterdata{#5}%
% \advance\wrmdatacounter\@ne\relax
 \ifx\afterdata\MmaPointStripped
%  \edef\wrm@label{\wrm@prefix\the\wrmdatacounter}%
  \def\wrm@label{#4}%
  \def\wrm@texstring{#4}%
 \else
  \def\wrm@label{#4}\def\wrm@texstring{#5}%
 \fi \def\testdata{#1}\wrm@savedata }

\def\MmaPointData{%
 \let\WRM@DataPattern\MmaPointPattern
 \let\WRM@DataStripped\MmaPointStripped
 \def\SetExpansionToks{\let\WARM@ExpansionToks\MmaPointToks}%
}

\def\FindBracedPoints{\MmaPointData\FindMmaMarkedPoints}
\def\WARMprocessMMA#1#2#3{\WARMprocess{#1}{#2}{#3}\FindBracedPoints}

% incorporate the Aspect-Ratio correction
\def\FindBracedARPoints{\MmaPointData\FindMmaARMarkedPoints}
\def\WARMprocessMMAR#1#2#3{\WARMprocess{#1}{#2}{#3}\FindBracedARPoints}


%%
%% Implementation for Mathematica CityData file (CityData.m)
%%

{\catcode`<=1 \catcode`>=2 \catcode`"=12 \catcode`\{=12 \catcode`\}=12 \catcode`\%=12
\gdef\MmaCityPattern<####1["####2"] = {{####3, ####4}, {####5, ####6}}####7>
\gdef\MmaCityStripped<[""] = {{, }, {, }}>
\gdef\MmaMinPattern<####1, ####2}####3>
\gdef\MmaMinStripped<, 0}>
\gdef\MmaPointEmpty<>
\gdef\MmaStartCityString<(* City Information *)>
\gdef\MmaPreCityString<CityPosition>
\gdef\MmaEndDataString<End[]>
\gdef\MmaEndFileString<EndPackage[]>
\gdef\ContentsBoxString<%%ContentsBox:>
\gdef\MapTypeString<%%MapProjection>
\gdef\MTpattern<####1: ####2 (####3,####4)####5>
\gdef\MTstripped<: , (,)>
\gdef\getMmaMinSecs@#1, #2}#3!!@!<\dimen@=#2\p@ \dimen@=.016667\dimen@
 \advance\dimen@ #1\p@ \dimen@=.016667\dimen@ >
>
% end of \catcode changes

\newtoks\MmaCityToks
\MmaCityToks={\def\wrm@y{#3}\def\wrm@x{#5}\def\afterdata{#7}%
 \edef\tmp@{#6\MmaMinStripped}\expandafter\getMmaMinSecs@\tmp@!!@!%
 \advance\dimen@\wrm@x\p@ \edef\wrm@x{\expandafter\removePT@\the\dimen@}%
 \edef\tmp@{#4\MmaMinStripped}\expandafter\getMmaMinSecs@\tmp@!!@!%
 \advance\dimen@\wrm@y\p@ \edef\wrm@y{\expandafter\removePT@\the\dimen@}%
% \advance\wrmdatacounter\@ne\relax
 \ifx\MapFilter\relax\else\MapFilter\fi
 \ifx\afterdata\MmaCityStripped
  \def\wrm@label{#2}\def\wrm@texstring{#2}%
 \else
  \def\wrm@label{#7}\def\wrm@texstring{#2}%
 \fi \def\testdata{#1}\wrm@savedata }

\def\FindMmaCityData{%
 \FindMmaCityData@i
 \FindMmaCityData@ii
 \FindMmaCityData@x
}%
\def\FindMmaCityData@i{%
 \CloseTheFile\relax
 \edef\wrm@filename{\CityDataFile}\TryToOpenTheFile
 \relax
}
\def\FindMmaCityData@ii{%
 \let\WARM@EndFileString=\MmaEndFileString
 \let\WARM@PreDataString=\MmaPreCityString
 \let\WARM@EndDataString=\MmaEndDataString
 \let\DataPattern=\MmaCityPattern
}


%%
%% Implementation for LatitudeLongitude files 
%%  (catcode changes needed, for �� and ' and % )
%%

{\catcode`�=12 \catcode`\'=12 \catcode`�=12 \catcode`\%=12
\gdef\LatLongPattern{####1 : ####2� ####3' ####4  ####5� ####6' ####7####8}
\gdef\LatLongStripped{ : � ' , � ' , }
\gdef\LatLongCountryPattern{ ####1}
\gdef\LatLongCountryStripped{ }
\gdef\LatLongPointEmpty{}
\gdef\LatLongStartString{Look-up Latitude and Longitude}
\gdef\LatLongPreCityString{}
\gdef\LatLongEndDataString{No More Data}
\gdef\LatLongEndFileString{No More Data}
\gdef\ContentsBoxString{%%ContentsBox:}
\gdef\MapTypeString{%%MapProjection}
\gdef\MTpattern{####1: ####2 (####3,####4)####5}
\gdef\MTstripped{: , (,)}
}
% end of \catcode changes

\newtoks\LatLongToks
\LatLongToks={\def\wrm@y{#2}\def\wrm@x{#5}\def\afterdata{#8}%
 \dimen@=#6\p@ \divide\dimen@ by60\relax \advance\dimen@\wrm@x\p@ 
 \DN@{#7}\def\tmp@{W}\ifx\tmp@\next@\multiply\dimen@\m@ne\fi
 \edef\wrm@x{\expandafter\removePT@\the\dimen@}%
 \dimen@=#3\p@ \divide\dimen@ by60\relax \advance\dimen@\wrm@y\p@
 \DN@{#4}\def\tmp@{S}\ifx\tmp@\next@\multiply\dimen@\m@ne\fi
 \edef\wrm@y{\expandafter\removePT@\the\dimen@}%
 \ifx\MapFilter\relax\else\MapFilter\fi
 \ifx\afterdata\LatLongStripped
  \def\wrm@label{#1}\def\wrm@texstring{#1}%
  \def\testdata{}%
 \else
  \ifx\wrm@quiet\relax\wrm@message{DATALine:<#1>}\fi
  \def\wrm@label{}\def\wrm@texstring{}%
  \def\testdata{#1}\gdef\wrm@namespace{#1}%
 \fi \wrm@savedata }

\def\FindLatLongCityData{%
 \FindLatLongCityData@i
 \FindLatLongCityData@ii
 \FindLatLongCityData@x
}%

\def\FindLatLongCityData@i{%
 \CloseTheFile\relax
 \edef\wrm@filename{\CityDataFile}\TryToOpenTheFile
 \relax
}

\def\FindLatLongCityData@ii{%
 \let\WARM@EndFileString=\LatLongEndFileString
 \let\WARM@PreDataString=\LatLongPreCityString
 \let\WARM@EndDataString=\LatLongEndDataString
 \let\DataPattern=\LatLongCityPattern
}

\def\DeclareExtraCityData{\begingroup 
 \LatLongCityData \FindLatLongCityData@ii \SetExpansionToks }

\def\EndDeclaredData{\endgroup}



\def\FindMapData{%
 \def\wrm@aspect{1}\let\wrm@llx=\undefined
 \let\SaveBBvalues@=\SaveMDvalues@
 \let\CleanupAfterFile=\saveMDafterFile
 \let\CleanupAfterDir=\saveMDafterFile
 %
 % read the BoundingBox
 %
 \let\DataPattern=\BBpattern
 \let\StrippedDataPattern=\BBstripped
 \let\WARM@ExpansionToks=\BBexpansiontoks
 \ReadToLine\AdobeBoundingBoxString\BBpattern\PrintInfoLine
%
% read the Map Contents area
%
 \let\DataPattern=\BBpattern
 \let\StrippedDataPattern=\BBstripped
 \let\WARM@ExpansionToks=\CBexpansiontoks
 \ReadToLine\ContentsBoxString\BBpattern\PrintInfoLine 
%
% read the Map Projection-type and center-offset
%
 \let\DataPattern=\MTpattern
 \let\StrippedDataPattern=\MTstripped
 \let\WARM@ExpansionToks=\MTexpansiontoks
 \ReadToLine\MapTypeString\MTpattern\PrintInfoLine 
}

\newtoks\CBexpansiontoks
\CBexpansiontoks={\def\testcomment{#1}%
 \ifx\testcomment\ContentsBoxString
  \def\wrm@cllx{#2}\def\wrm@clly{#3}%
  \def\wrm@curx{#4}\def\wrm@cury{#5}%
%
  \dimen@=\wrm@curx\p@\advance\dimen@-\wrm@cllx\p@ % inner width in pts
  \quotient@\s@dX{360\p@}\dimen@ % convert pts --> degrees
  \divide\dimen@\tw@ \advance\dimen@\wrm@cllx\p@ % centre to left edge
  \dimen@ii=\wrm@urx\p@\advance\dimen@ii-\wrm@llx\p@ % outer width in pts
  \advance\dimen@ii-\dimen@ % centre to right edge
  \dimen@=-\s@dX\dimen@
  \edef\wrm@llx{\expandafter\removePT@\the\dimen@}% left edge in degrees
  \dimen@ii=\s@dX\dimen@ii
  \edef\wrm@urx{\expandafter\removePT@\the\dimen@ii}% right edge in degrees
% 
  \dimen@=\wrm@cury\p@\advance\dimen@-\wrm@clly\p@ % inner height in pts
  \quotient@\s@dY{180\p@}\dimen@                 % convert pts --> degrees
  \divide\dimen@\tw@ \advance\dimen@\wrm@clly\p@ % centre to bottom edge
  \dimen@ii=\wrm@ury\p@\advance\dimen@ii-\wrm@lly\p@ % outer height in pts
  \advance\dimen@ii-\dimen@ % centre to top edge
  \dimen@=-\s@dY\dimen@
  \edef\wrm@lly{\expandafter\removePT@\the\dimen@}% bottom edge in degrees
  \dimen@ii=\s@dY\dimen@ii
  \edef\wrm@ury{\expandafter\removePT@\the\dimen@ii}% top edge in degrees
%  
\wrm@message{file: \wrm@filename}%
\wrm@message{Contents Box is (\wrm@cllx,\wrm@clly)->(\wrm@curx,\wrm@cury)}%
\wrm@message{Rescaled BBox is (\wrm@llx,\wrm@lly)->(\wrm@urx,\wrm@ury)}%
  \aftergroup\morelinesfalse
 \fi
}
\newtoks\MTexpansiontoks
\MTexpansiontoks={\def\testcomment{#1}%
 \ifx\testcomment\MapTypeString
  \gdef\wrm@mtype{#2}%
  \gdef\wrm@mox{#3}\gdef\wrm@moy{#4}%
\wrm@message{file: \wrm@filename}%
\wrm@message{Map Projection: \wrm@mtype, centered on (\wrm@mox,\wrm@moy)}%
  \aftergroup\morelinesfalse
 \fi
}
\def\SaveMDvalues@{%
 \ifx\undefined\wrm@llx
  \def\next{\endgroup}%
 \else
  \edef\next{\endgroup \wrmdatacounter=\the\wrmdatacounter\relax
   \def\noexpand\wrm@llx{\wrm@llx}\def\noexpand\wrm@lly{\wrm@lly}%
   \def\noexpand\wrm@urx{\wrm@urx}\def\noexpand\wrm@ury{\wrm@ury}%
   \ifx\undefined\wrm@cllx\else
    \def\noexpand\wrm@cllx{\wrm@cllx}\def\noexpand\wrm@clly{\wrm@clly}%
    \def\noexpand\wrm@curx{\wrm@curx}\def\noexpand\wrm@cury{\wrm@cury}%
   \fi
   \ifx\undefined\wrm@mtype\else
    \def\noexpand\wrm@mox{\wrm@mox}%
    \def\noexpand\wrm@moy{\wrm@moy}%
    \def\noexpand\wrm@mtype{\wrm@mtype}%
    \noexpand\checkMapFilters
   \fi
   \def\noexpand\wrm@filename{\wrm@filename}}%
 \fi }
\def\saveMDafterFile{\SaveMDvalues@\next
 \def\CleanupAfterFile{\endgroup}}


\def\FindMmaCityData@x{%
 \def\maxwrmlines{120}%
 \ReadToLine\MmaStartCityString{}\relax
 \SetExpansionToks
 \ReadDataLines\WRM@DataPattern\WRM@DataStripped\relax
 \ReadToLine\MmaEndDataString{}\relax
 \ReadToLine\MmaEndFileString{}\relax
}

\def\MmaCityData{%
 \let\WRM@DataPattern\MmaCityPattern
 \let\WRM@DataStripped\MmaCityStripped
 \def\SetExpansionToks{\let\WARM@ExpansionToks\MmaCityToks}%
}

\def\WARMprocessMAP#1#2#3#4{\gdef\CityDataFile{#1}%
 \WARMprocess{#2}{#3}{#4}\FindCityLocations}

\def\FindCityLocations{%
 \FindMapData \MmaCityData \FindMmaCityData}


\def\FindLatLongCityData@x{%
 \def\maxwrmlines{120}%
 \ReadToLine\LatLongStartString{}\relax
 \SetExpansionToks
 \ReadDataLines\WRM@DataPattern\WRM@DataStripped\relax
 \ReadToLine\LatLongEndDataString{}\relax
}

\def\LatLongCityData{%
 \let\WRM@DataPattern\LatLongPattern
 \let\WRM@DataStripped\LatLongStripped
 \def\SetExpansionToks{\let\WARM@ExpansionToks\LatLongToks}%
}

\def\FindLatLongLocations{%
 \FindMapData \LatLongCityData \FindLatLongCityData}

\def\WARMprocessLL#1#2#3#4{\relax\gdef\CityDataFile{#1}%
 \DN@{#2}\ifx\next@\wrm@empty
  \DN@{\let\ResetDataCounter=\relax \wrmcounter=0 
    \PrepareToOpenFile  \let\CleanupAfterFile=\saveMDafterFile
    \LatLongCityData \FindLatLongCityData \saveMDafterFile }%
 \else
  \DN@{\WARMprocess{#2}{#3}{#4}\FindLatLongLocations}%
 \fi\next@ }


%% Map Filters

\global\let\MapFilter=\relax
\def\checkMapFilters{%
\typeout{*** Checking for \wrm@mtype Filter ***}%
  \def\tmp@{Robinson}\ifx\tmp@\wrm@mtype
   \global\let\MapFilter=\RobinsonFilter
  \else
  \def\tmp@{Rectangular}\ifx\tmp@\wrm@mtype
   \global\let\MapFilter=\RectangularFilter
  \else
   \global\let\MapFilter=\relax
  \fi\fi
}

%
% allows centering at (long,lat) = (\wrm@mox,\wrm@moy)
%
\def\RectangularFilter{%
  \dimen@=\wrm@x\p@\advance\dimen@180\p@ \dimen@ii=\wrm@mox\p@
  \ifdim\dimen@ii=\z@\else\advance\dimen@-\dimen@ii\fi
  \advance\dimen@\ifdim\dimen@<\z@\else-\fi 180\p@
  \edef\wrm@x{\expandafter\removePT@\the\dimen@}%
  \dimen@=\wrm@y\p@\advance\dimen@90\p@ \dimen@ii=\wrm@moy\p@
  \ifdim\dimen@ii=\z@\else\advance\dimen@-\dimen@ii\fi
  \advance\dimen@\ifdim\dimen@<\z@\else-\fi 90\p@
  \edef\wrm@y{\expandafter\removePT@\the\dimen@}%
}

%
% allows centering at (long, 0) = (\wrm@mox, 0)
%
\def\RobinsonFilter{%
  \dimen@=\wrm@x\p@\advance\dimen@180\p@ \dimen@ii=\wrm@mox\p@
  \ifdim\dimen@ii=\z@\else\advance\dimen@-\dimen@ii\fi
  \advance\dimen@\ifdim\dimen@<\z@\else-\fi 180\p@
%% these lines should never be needed ...
%  \edef\wrm@x{\expandafter\removePT@\the\dimen@}%
%  \dimen@=\wrm@y\p@ \dimen@ii=\wrm@moy\p@ 
%  \ifdim\dimen@ii=\z@\else\advance\dimen@-\dimen@ii\fi
%  \edef\wrm@y{\expandafter\removePT@\the\dimen@}%
%  \edef\next{\noexpand\robinson@rect{\wrm@y\p@}{\wrm@x\p@}}%
%% ...if they are, then comment-out this next line
  \edef\next{\noexpand\robinson@rect{\wrm@y\p@}{\the\dimen@}}%
%
  \next \multiply\dimen@ii 90\relax
  \edef\wrm@x{\expandafter\removePT@\the\dimen@}%
  \edef\wrm@y{\expandafter\removePT@\the\dimen@ii}%
}

%% Robinson Projection

\def\latitude@robinson{\\-0.062\\0\\0.062\\0.1240\\0.1860\\0.2480\\0.3100
 \\0.3720\\0.4340\\0.4958\\0.5571\\0.6176\\0.6769\\0.7346\\0.7903\\0.8435
  \\0.8936\\0.9394\\0.9761\\1.000\\1.0239\\}
\def\longitude@robinson{\\1.0014\\1.0\\.9986\\.9954\\.9900\\.9822\\.9730
 \\.9600\\.9427\\.9216\\.8962\\.8679\\.8350\\.7986\\.7597\\.7186\\.6732
  \\.6213\\.5722\\.5322\\.5\\}

% \robinson@lat {#1}
%  calculates rectangular coordinate for given latitude
%  using look-up table with 5-degree increments
%  provide the latitude as a <dimen> for #1
%  value is returned as \dimen@
%
\def\robinson@lat#1{\bgroup
 \X@c=\#1\relax \def\sd@X{}%
 \ifdim\X@c<\z@ \def\sd@X{-}\X@c=-\X@c \fi
 \interpolate@quadratic{#1}\latitude@robinson{5}%
 \X@c=\sd@X\X@c
 \edef\next@{\egroup \dimen@=\the\X@c}\next@
}
\def\robinson@rect#1#2{\bgroup
 \X@c=#1\relax \def\sd@Y{}%
 \ifdim\X@c<\z@ \def\sd@Y{-}\X@c=-\X@c \fi
 \interpolate@quadratic{\X@c}\latitude@robinson{5}%
 \Y@p=\Y@c \Y@p=\sd@Y\Y@p
 \X@p=#2\relax \def\sd@X{}\ifdim\X@p<\z@ \def\sd@X{-}\fi
 \interpolate@quadratic{\X@c}\longitude@robinson{5}%
 \edef\tmp@{\expandafter\removePT@\the\Y@c}\X@p=\tmp@\X@p
 \edef\next@{\egroup \dimen@=\the\X@p \dimen@ii=\the\Y@p}%
 \next@
}

\newcount\interp@counter

% Quadratic interpolation
% data-set starts at -1, to determine slope at 0
% #1 = number
% #2 = list-data
% #3 = integer scale-factor, x-increment between data units
\def\interpolate@quadratic#1#2#3{\bgroup
 \X@c=#1\relax \def\@increment{#3}%
 \interp@counter=\m@ne
 \def\empty@{}\ifx\empty@\@increment\def\@increment{1}\fi
 \let\\=\quad@interval\relax #2\\\\!!@!%
 \edef\next@{\egroup \Y@c=\the\Y@c}\next@
 }

\def\quad@interval#1\\{\DN@{#1}%
 \ifx\next@\empty@\DN@{\end@interp}%
 \else \def\tmp@{\\}\ifx\next@\tmp@ \DN@{\end@interp}%
 \else \dimen@\@increment\p@
  \multiply\dimen@\interp@counter\relax
  \ifdim\dimen@<\X@c
   \advance\interp@counter\@ne \X@p=\dimen@
   \D@p=\Y@p \Y@p=#1\p@ \DN@{\\}%
  \else \Y@c=#1\p@ \DN@{\quad@interp}%
  \fi 
 \fi\fi \next@
}
\def\end@interp#1!!@!{\relax}

\def\quad@interp#1\\{\U@c=#1\p@
 \advance\X@c-\X@p \divide\X@c\@increment
 \B@=\Y@p \advance\B@-\D@p
 \A@=\U@c \advance\A@-\Y@c
 \advance\A@-\B@ \divide\A@\tw@
 \edef\tmp@{\expandafter\removePT@\the\X@c}%
 \Y@c=\tmp@\A@  \advance\Y@c\B@
 \Y@c=\tmp@\Y@c \advance\Y@c\Y@p
 \let\\=\end@interp\end@interp
}
 



\catcode`\@=\wrmprevat


