% This macro source file is from the four volume series
% "TeX in Practice" by Stephan von Bechtolsheim, published
% 1993 by Springer-Verlag, New York.
% Copyright 1993 Stephan von Bechtolsheim.
% No warranty or liability is assumed.
% This macro may be copied freely if no fees other than
% media cost or shipping charges are charged and as long
% as this copyright and the following source code itself
% is not changed. Please see the series for further information.
%
% Version: 1.0
% Date: May 1, 1993
%
%
% This source code is documented in 30.8.6.3, p. III-571.
% Original source in file "pp1.TEX", starting line 1937.
\wlog{L: "ts-page2.tip" ["pp1.TEX," l. 1937, p. III-571]}%
% This file DOES belong to format "texip."
\InputD{part-cr.tip}
\InputD{rangetst.tip}
\InputD{ts-pager.tip}
\catcode`\@ = 11
\newcount\@PageRefLimitLow
\newcount\@PageRefLimitHigh
\newif\if@CloseByReference
\newcount\@PageCloseRefCount
\newcount\@PageNumberActual
\def\NoSpecialPageRef{%
   \global\@NoSpecialPageReftrue
}
\newif\if@NoSpecialPageRef
\@NoSpecialPageReffalse
\def\@PageRef #1{%
   \def\@PageRefResult{}%
   \global\@CloseByReferencefalse
   \StepCounter{CloseByReferenceCount}%
   \CounterToRegister{\global\@PageCloseRefCount}%
       {CloseByReferenceCount}%
   \@ref{1}{#1}%
   \if\StringsEqualConditional{\@TheExpandedLabel}{??}%
       \def\@PageRefResult{??}%
   \else
       \if\PrefixConditional
           {\@TheExpandedLabel}%
           {\TheCounter{VolumeNo}-}%
           \DropPrefix{\@TheExpandedLabel}{\TheCounter{VolumeNo}-}%
                      {\@TheShortenedLabel}%
           \def\@PageRefResult{\@TheShortenedLabel}%
           \Label{@READ@-%
               \@ThisLabel-\the\@PageCloseRefCount}%
           \@PageRefLimitLow = \@TheShortenedLabel
           \advance\@PageRefLimitLow by -1
           \@PageRefLimitHigh = \@TheShortenedLabel
           \advance\@PageRefLimitHigh by 1
           \edef\@Ref@Temp@Name{%
               \@LabelRefPrefix @READ@-%
               \@ThisLabel-\the\@PageCloseRefCount
           }%
           \if\NameDefinedConditional{\@Ref@Temp@Name}%
               \edef\@Ref@Temp@NameTwo{%
                   \NameUse{\@Ref@Temp@Name}%
               }%
               \wlog{@READ@: 1 \@Ref@Temp@Name /
                   \@Ref@Temp@NameTwo}%
                   \edef\@Ref@Temp@NameThree{%
                       \expandafter\PickSecondOfTwo
                           \@Ref@Temp@NameTwo
                   }%
               \wlog{@READ@ 1a: \@Ref@Temp@NameThree}%
               \DropPrefix
                   {\@Ref@Temp@NameThree}%
                   {\TheCounter{VolumeNo}-}%
                   {\@ResultLocalRef}%
%               \show\@ResultLocalRef
               \@PageNumberActual = \@ResultLocalRef
           \else
               \wlog{@READ@ 2}%
               \@PageNumberActual = \count0
               \@PageRefLimitLow = -10
               \@PageRefLimitHigh = -10
           \fi
           \if\InRangeConditional
               {\@PageNumberActual}%
               {\@PageRefLimitLow}%
               {\@PageRefLimitHigh}%
               \wlog{@READ@ 3}%
               \@CloseByReferencetrue
               \if@NoSpecialPageRef
                   \@CloseByReferencefalse
               \fi
           \else
               \wlog{@READ@ 4}%
               \@CloseByReferencefalse
           \fi
           \global\@NoSpecialPageReffalse
           \if@CloseByReference
               \ifnum\@PageNumberActual = \@PageRefLimitLow
                   \wlog{\string\PageRef: Reference "\@ThisLabel,"
                       on page \the\@PageNumberActual
                       \space refers to the next page.}%
                   \def\@PageRefResult{the next}%
               \fi
               \ifnum\@PageNumberActual = \@TheShortenedLabel
                   \wlog{\string\PageRef: Reference "\@ThisLabel"
                       on page \the\@PageNumberActual
                       \space refers to the current page.}%
                   \def\@PageRefResult{this}%
               \fi
               \ifnum\@PageNumberActual = \@PageRefLimitHigh
                   \wlog{\string\PageRef: Reference "\@ThisLabel"
                       on page \the\@PageNumberActual
                       \space refers to the preceding page.}%
                   \def\@PageRefResult{the previous}%
               \fi
           \fi
       \else
           \def\@PageRefResult{\@TheExpandedLabel}%
       \fi
   \fi
   \edef\@PageRefResult{\@PageRefResult}%
}
\catcode`\@ = 12