\def\SNP#1{{\write\XREFileOut{\string#1}%
\def\JUNKA{\SNChapterPrefix\the\SNChapter\space\the\SNUnit}%
{\edef\JUNKB{\write\XREFileOut{\JUNKA}}\JUNKB}%
\write\XREFileOut{\the\count0}}}%
%
% END Modified at 04:16:31 on Tuesday, December 29th, 2015.
%
% Define a table tag:
%
\def\SNT#1{{\global\advance\SNTable by1
\write\XREFileOut{\string#1}%
\def\JUNKA{\the\SNTable\space JUNK}%
{\edef\JUNKB{\write\XREFileOut{\JUNKA}}\JUNKB}%
\write\XREFileOut{\the\count0}%
\the\SNTable}}
%
% Define a figure tag:
%
\def\SNF#1{{\global\advance\SNFigure by1
\write\XREFileOut{\string#1}%
\def\JUNKA{\the\SNFigure\space JUNK}%
{\edef\JUNKB{\write\XREFileOut{\JUNKA}}\JUNKB}%
\write\XREFileOut{\the\count0}%
\the\SNFigure}}
%
% Get all the sequence number data from file \jobname.XRF:
%
\def\GetSN{{\openin\XREFileIn=\jobname.XRF
\pushCount\InChapter
\pushCount\InUnit
\pushCount\InPage
\pushCount\MoreFlag
\ifeof\XREFileIn\MoreFlag=0 \else\MoreFlag=1 \fi\relax % \jobname.XRF exists?
\ifnum\MoreFlag=1 \relax
\loop
\read\XREFileIn to\LineIn
\ifeof\XREFileIn\MoreFlag=0 \else\MoreFlag=1 \fi
\ifnum\MoreFlag=1
\let\InTag=\LineIn
\read\XREFileIn to\LineIn
\edef\InValues{\LineIn}
\read\XREFileIn to\LineIn
\InPage=\LineIn
\edef\JUNK{\InValues\the\InPage}%
\global\expandafter\let\InTag=\JUNK
\repeat
\fi
\closein\XREFileIn
}}
%
% Get cross-references on 'chapters':
%
\def\SNCGetChapter#1{\ifx#1\undefined
\edef#1{\SNUNone}\else\fi
\expandafter\First#1 }
%
\def\SNCGetPage#1{\ifx#1\undefined
\edef#1{\SNUNone}\else\fi
\expandafter\Third#1 }
%
% Get cross-references on 'units':
%
\def\SNUGetChapter#1{\ifx#1\undefined
\edef#1{\SNUNone}\else\fi
\expandafter\First#1 }
%
\def\SNUGetUnit#1{\ifx#1\undefined
\edef#1{\SNUNone}\else\fi
\expandafter\Second#1 }
%
\def\SNUGetPage#1{\ifx#1\undefined
\edef#1{\SNUNone}\else\fi
\expandafter\Third#1 }
%
% Get cross-references on tables:
%
\def\SNTGetTable#1{\ifx#1\undefined
\edef#1{\SNTNone}\else\fi
\expandafter\First#1 }
%
\def\SNTGetPage#1{\ifx#1\undefined
\edef#1{\SNTNone}\else\fi
\expandafter\Third#1 }
%
% Get cross-references on figures:
%
\def\SNFGetFigure#1{\ifx#1\undefined
\edef#1{\SNFNone}\else\fi
\expandafter\First#1 }
%
\def\SNFGetPage#1{\ifx#1\undefined
\edef#1{\SNFNone}\else\fi
\expandafter\Third#1 }
%
% Read file \jobname.XRF here, now, before anything else.
% Get this reading done before any hint of a need to write
% to that file.
%
% Modified at 08:13:46 on Monday, January 4th, 2016.
%
% If a TeX file has
%
% {\bf \SNU{} Theorem:}\ \
%
% instead of, say,
%
% {\bf \SNU{\SNTagDA} Theorem:}\ \
%
% the TeX can die here:
%
\GetSN
%
% Important: First read file \jobname.XRF. Then open it
% for writing. This open will give the file length 0 and,
% thus, destroy the data, if any, just read. If
% execute
%
% \write\XREFileOut{\string#1}
%
% etc. without doing an open, then output will go to
% console.
%
% From some fairly careful experiments, commands
%
% \newread\XREFileIn
% \newwrite\XREFileOut
% \openout\XREFileOut=\jobname.XRF
% \closein\XREFileIn
%
% seem to ignore TeX block nesting and to be fully 'global'
% across blocks.
%
\openout\XREFileOut=\jobname.XRF
%
% ======================================================================